An .EPS to .PDF converter (using LaTeX!)

I am about to go on a short holiday, so I was tidying the code lines I had scattered around before leaving… And I found this: a minimal EPS to PDF converter, which is barely a LaTeX template.

It is intended for transforming an .EPS graph to the .PDF format. You can copy & paste this whole code into a blank text file (but with .TEX extension) and run it with a TeX editor. To install and use LaTeX, here it is a previous post about it.

When you have compiled it, you can search in the same file’s directory for the newly created PDF graph!

%%%%%%%%%%%%%%%%%%%%%%%%
%% EPS TO PDF CONVERTER %%
%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Mareviv
% Under GNU General Public License
% Paste this document entirely into a file with .TEX extension (.tex). Open it with TeXworks
% Tips for starting with LaTeX: https://talesofr.wordpress.com/2013/05/02/learning-latex-from-scratch/
\documentclass{article}
\usepackage{graphicx} % support the \includegraphics command and options
\usepackage{epstopdf} % Included EPS files automatically converted to PDF to include with pdflatex
\begin{document}
This is an .EPS to .PDF converter, using a minimal {\LaTeX} document.
Place the .EPS file in the same folder as this converter.
Insert the .EPS figure name inside the curly brackets (in this case rnetwork2).
\begin{figure}
\centering
\includegraphics[width=1.1\linewidth]{rnetwork2}
\caption{A network graph.}
\end{figure}
Copy and paste the following code to convert more images at the same time:
\begin{figure}
\centering
\includegraphics[width=1.1\linewidth]{rnetwork2}
\caption{Another network graph.}
\end{figure}
\end{document}

…learning LaTeX, from scratch!

» LaTeX is a high-quality typesetting system; it includes features designed for the production of technical and scientific documentation, and is the de facto standard for the communication and publication of scientific documents.»

It is also… Free and Open. Specially designed for beautiful documents even when they include special language: mathematic formulae, phonetic symbols, musical notation…

The mix of normal text with special languages is indeed much better arranged in LaTeX than in any other text editor, so maybe it can be interesting for you!. If you want to start using LaTeX, first you have to install it. Here is a previous post with something about LaTeX installation and useful links. In particular, I like the LaTeX editor «TeXworks» for having a simpler GUI -graphic interface-, so that’s what I’m gonna use for this tutorial.

Once installed a LaTeX distribution and a TeX editor (you have both with MiKTeX), you can open TeXworks. Here’s what it looks like:

LaTeXtemplate0

To start making documents, I recommend you to choose one of the templates available:

click: File >> New from Template >> choose Article

LaTeXtemplate

Then, a template will appear. You can highlight the code going to:

Format >> Syntax coloring >> LaTeX

It should look like this:

LaTeXtemplate2

Read carefully this «source file», that is, the original .tex file, for it provides basic instructions and tips for writing.

Then, you can simply press the green, round button and see what happens… then change something in the source file… then press the button again… and so on.

If you wish to learn to insert things like images, tables, etc… then… Wikibooks is for you, take a look. I hope this tutorial has been useful!

…start using Sweave, from scratch!

INTRODUCTION

Sweave is nothing more, nothing less than one of the best ways R can connect with a text editor, in this case LaTeX.

So you don’t know anyting about LaTeX? neither did I 8 months ago… The hyperlinks in this post will take you to some great pages, to learn different things in an organised way. Here’s how I did it (with lots of help from google though!).

It’s important to take it step by step…

Second, you need to install R, from here. Once you have installed it, you will need to install a LaTeX distribution (my preferred LaTeX editor is TeXworks, and I have MiKTeX in MS Windows).

  • If you are a MS Windows user, you will find useful this one: MiKTeX
  • For Linux users, here can be Kile (from KDE), TeXworks, or other alternatives. First of all you should install LaTeX (e.g. TeX Live), and configure it with these tips.
  • For Mac users, you can follow this link which provides instructions to install TeXmaker. You will be able to install TeXworks from here.
  • NOTE: Both, MiKTeX and TeXmaker have portable versions, easy to carry in a pendrive!

What TeX editor did you install? If you have TeXworks, the way to configure Sweave is pretty quick and easy. If you are a beginner, this may be the best option.

MacTeX installation screen

CONFIGURING TEXWORKS

If you have TeXworks, all you have to do is go to:

Edit >> Preferences >> Typesetting

Then, you will see this screen:

TexworksSweave-1

If you click the «+» button (highlighted in green) to add a new tool, another screen will appear:

TexworksSweaveCommands-1

You only have to fill in these fields with the name of the new tool, «Sweave» and adding its «arguments» clicking in the «+» sign.

The field «Program» is filled with the path to your R executable, which can be similar to that in the picture. Just be careful to type «slashes» ( / ) instead of «backslashes» ( \ ).

The field «Arguments» will contain all the commands that Sweave needs to connect to the R console, to interpret R’s code and to produce a .TEX file and a .PDF.

In (K)Ubuntu is configured in a similar way:

KubuntuTeXWorksSweave

And so in Mac OS X (note that we have the same Sweave commands as in Ubuntu):

SweaveinMacOS

WRITING A .RNW FILE

Let’s write something in a new Sweave file!. To this, you must create a new text file (e.g. myfirstsweave.txt) , and change its extension to .Rnw (see this for more help for MSWin users):

myfirstsweave.Rnw

This is a blank file, but you can paste the following code to construct a template:


% This is a Sweave template from TalesofR.wordpress.com.
% When a line is preceeded by a "%" sign, it will be ignored by the program.
% You can write what you want in this way. It usually serves for making invisible comments.
% If you want to make a comment inside the chunk of R code, the symbol to start the line must be "#", instead of "%"
% Let's start with the template:
\documentclass{article}
\begin{document}
% chunks of R code are initiated by "<>=", and ended by "@"
% with <<echo=FALSE>>=, the code is not printed, only the result.
% <<echo=FALSE, message=FALSE, etc…>>= % These are chunk options
You have to enter first the path to your R libraries, often located in your own user.
<<>>=
.libPaths("C:/Users/……/Documents/R/win-library/2.15")
@
The path is set. Let's begin with commands:
<<>>=
# this is a comment inside a chunk, it is not executed as code.
dat<- data.frame(t=seq(0, 2*pi, by=0.1) )
xhrt <- function(t) 16*sin(t)^3
yhrt <- function(t) 13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)
dat$y=yhrt(dat$t)
dat$x=xhrt(dat$t)
@
You can write what you want outside the chunks. \\
The next chunk is a plot:
<<label=fig1plot, fig=TRUE>>=
with(dat, plot(x,y, type="l"))
with(dat, polygon(x,y, col="hotpink"))
@
\end{document}

Once you have your template, you must… «compile» it. First, you can highlight the code with nice colours clicking:

Format >> Syntax Coloring >> LaTeX

To compile the LaTeX/Sweave file, you have to click in the pull-down menu, and select:

Sweave

Sweave_compilation

Great! the last step is to click in the green, round «play» button…

Voilà!

sweave_output

EDIT FOR TEXWORKS IN UBUNTU

The installation of TeXworks in my Kubuntu 12.04 has been direct from the console -at least it worked for me-, without following further instructions:

sudo apt-get update
# sudo apt-get upgrade
sudo apt-get install texworks

After the configuration of Sweave typesetting -see above-, I was trying to compile my own template, and this error appeared:

Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :

Running ‘texi2dvi’ on ‘untitled-1.tex’ failed.

Messages:

sh: 1: /usr/bin/texi2dvi: not found

Texi2dvi appears to be a script that allows direct compilation of a .pdf from a .Rnw file.

KubuntuTeXWorksIssue

Maybe it’s not the cleanest solution, but I copied the source code of texi2dvi and created a script with it: (sudo gedit if you are running Ubuntu, sudo kate if Kubuntu)

Open up a Terminal and type:

 sudo kate /usr/bin/texi2dvi

Then you can paste the code into this script, and save the changes. To make it executable, type in the Terminal:

 sudo chmod 755 /usr/bin/texi2dvi

 

FURTHER READINGS