Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX figure label from R plot using KnitR?

Tags:

r

latex

knitr

I can't get R/KnitR to create the LaTeX \label{} statement for a figure. The manual seems to indicate that a \label{} statement will be created by concatenating the string in fig.lp ("fig:" by default) with the label for the R-code chunk. I haven't been able to get this to work, however. No \label{} statement is created for the first figure created by knitting the MWE below. The second figure has it's label added with a workaround that I just discovered, putting the R chunk in a figure environment, and putting the \label tag after or inside the \caption tag.

\documentclass[12pt, english, oneside]{amsart}
\begin{document}

Figure \ref{fig:plot} doesn't have it's label.

<<plot>>=
plot(x=0, y=0)
@

Figure \ref{fig:plot2} has its label.

\begin{figure}
\caption{\label{fig:plot2}}
<<>>=
plot(x=1,y=1)
@
\end{figure}

\end{document}

Okay, I've found a workaround by putting the R chunk in a \begin{figure} . . .\end{figure} environment in LaTeX. I can create the label in that same environment. Still, I'd like to understand how Yihui intends for this to be handled with KnitR.

like image 596
Gregory Avatar asked Feb 20 '13 23:02

Gregory


1 Answers

You need to set fig.cap = '' (or whatever you wish) to ensure that a figure environment is used in the latex document. (you may have noticed that the \begin{figure} ... \end{figure} is missing along with the \label{} component

eg

\documentclass[12pt, english, oneside]{amsart}
\begin{document}
See Figure \ref{fig:plot}.
<<plot, fig.lp="fig:", fig.cap = ''>>=
plot(x=0, y=0)
@
\end{document}

I would agree that the description from the website is less than clear as to this being necessary.

  • fig.env: ('figure') the LaTeX environment for figures, e.g. set fig.env='marginfigure' to get \begin{marginfigure}

  • fig.cap: (NULL; character) figure caption to be used in a figure environment in LaTeX (in \caption{}); if NULL or NA, it will be ignored, otherwise a figure environment will be used for the plots in the chunk (output in \begin{figure} and \end{figure})

Although the graphics manual is clear, and the reasoning makes sense

Figure Caption

If the chunk option fig.cap is not NULL or NA, the plots will be put in a figure environment when the output format is LATEX, and this option is used to write a caption in this environment using \caption{}. The other two related options are fig.scap and fig.lp which set the short caption and a prefix string for the figure label. The default short caption is extracted from the caption by truncating it at the first period or colon or semi-colon. The label is a combination of fig.lp and the chunk label. Because figure is a float environment, it can float away from the chunk output to other places such as the top or bottom of a page when the TEX document is compiled.

If you were wishing to replicate a R session output, you wouldn't want the figures to float away from the line of code which defines how they were created.

like image 72
mnel Avatar answered Oct 21 '22 00:10

mnel