Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX and R bundle?

Tags:

r

latex

knitr

tex

I am using R to analyse statistical data and plot histograms, scatter plots etc.

And then I have to export all plots as PDFs to manually include them in LaTeX report.

I wonder if there is any way for simplification of this process?

I would be happy to write something like:

\chapter{One}
\begin{r}
    qplot(...)
\end{r}

So that the code between \begin{r} and \end{r} would generate a plot, save it somewhere as PDF and produce TeX like this:

\begin{figure}[ht!]
    \includegraphics[width=1\textwidth,height=1\textheight]{/path/to/plot.pdf}
\end{figure}
like image 618
Edward Ruchevits Avatar asked Dec 07 '12 05:12

Edward Ruchevits


2 Answers

See if you can be convinced by the 5-min video on knitr's homepage: http://yihui.name/knitr/ If you only care about LaTeX, start from 2:54.

Your source code would be like this:

\chapter{One}
<<plot, out.width='1\textwidth', out.height='1\textheight', fig.pos='!ht', fig.cap='your caption'>>=
    qplot(...)
@
like image 74
Yihui Xie Avatar answered Nov 20 '22 22:11

Yihui Xie


What you want is knitr.

The website has lots of examples

within your document you can do something like

<<boring-plots, fig.width=4, fig.height=4, out.width='.4\\linewidth'>>=
## two plots side by side (option fig.show='hold')
par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3,las=1)
boxplot(x)
hist(x,main='')
@

Or even set it up so your

\begin{r}

\end{r}

syntax would work.

The pdf output of the minimal example from which the example above comes

like image 6
mnel Avatar answered Nov 20 '22 21:11

mnel