2015-04-23 65 views
6

我有一個.Rnw文件,我想從多圖塊引用圖塊。我該怎麼做呢?knitr:如何引用多圖塊的圖塊

實施例:

\documentclass{article} 
\begin{document} 

<<single_chunk, fig.cap="hi">>= 
plot(1:5) 
@ 

I can reference this single chunk fine! See \ref{fig:single_chunk}. 

<<multichunk, fig.cap="hello">>= 
plot(1:10) 
plot(10:1) 
@ 

The first figure is great, but \ref{fig:multichunk}. Try again \ref{fig:multichunk-1}. 

\end{document} 

這兩種嘗試導致??

回答

3

只需看看生成的* .tex文件!下面是相關的部分(我把自由多一點的好聽knit不對齊):

\begin{knitrout} 
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe} 
\begin{alltt} 
\hlkwd{plot}\hlstd{(}\hlnum{1}\hlopt{:}\hlnum{10}\hlstd{)} 
\end{alltt} 
\end{kframe} 
\begin{figure} 
    \includegraphics[width=\maxwidth]{figure/multichunk-1} 
    \caption[hello]{hello} 
    \label{fig:multichunk1} 
\end{figure} 

\begin{kframe}\begin{alltt} 
\hlkwd{plot}\hlstd{(}\hlnum{10}\hlopt{:}\hlnum{1}\hlstd{)} 
\end{alltt} 
\end{kframe} 
\begin{figure} 
    \includegraphics[width=\maxwidth]{figure/multichunk-2} 
    \caption[hello]{hello} 
    \label{fig:multichunk2} 
\end{figure} 

所以,如果你仔細觀察,你會注意到,數字的multiplot被命名爲fig:multichunk1fig:multichunk2。事實上,如果你參考這些(\ref{fig:multichunk1},...),一切工作正常。