fig.lp
is not the label of the figure, it is the prefix that is used to create the label. From the description of knitr
options
fig.lp
: ('fig:'; character) label prefix for the figure label to be used in \label{}
; the actual label is made by concatenating this prefix and the chunk label, e.g. the figure label for <<foo-plot>>=
will be fig:foo-plot
by default
If you don't give your chunks names, they will get a default name such as unnamed-chunk-1
where the number depends on where it is in the file (and so would make for a bad label to target).
Additionally, to get the graphics into a figure environment, you need to give it a caption. Again from the options description
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}
)
Without this there is no figure environment and therefore no \label{}
created.
Putting this together, here is a MWE:
\documentclass{article}
\begin{document}
<<myfig, fig.cap=''>>=
plot(1:10)
@
As you can see the figure \ref{fig:myfig} shows that...
\end{document}
This creates a .tex
file, the relevant (but incomplete) excerpt of which is
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlfunctioncall{plot}(1:10)
\end{alltt}
\end{kframe}\begin{figure}[]
\includegraphics[width=\maxwidth]{figure/myfig} \caption[]{\label{fig:myfig}}
\end{figure}
\end{knitrout}
As you can see the figure \ref{fig:myfig} shows that...
You can see that the \label{}
and \ref{}
match up now.
See also more discussion about this in a related Stack Overflow question.
TeX
had a chance to read the.aux
file that contains the reference information?