35
votes

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.

1
Have you tried <<plot>>= instead of <<plot, fig.lp="fig:">>= ?N8TRO
I actually tried <<plot>>= first, just forgot to correct it in the MWE. Fixed now. Thanks.Gregory
I think you can also include your custom label by putting the \label{mylabel} directly into the KnitR fig.cap chunk option. It should then dump that into the output. But you may have to beware of escaping the \ character by using something like this: <<plot, fig.cap="My caption text.\\label{mylabel}">>=. I can't test, but if it works, you're welcome to repost as a solution.Kalin

1 Answers

35
votes

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.