本文介绍了使用KnitR从R图绘制LaTeX图形标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让R/KnitR创建图形的LaTeX \label{}语句. 手册似乎表明,将通过将fig.lp中的字符串连接起来来创建\label{}语句( "fig:"(默认情况下),带有R代码块的标签.但是,我无法使它正常工作.对于通过编织以下MWE创建的第一个图形,不会创建\label{}语句.第二个图形在其标签上添加了我刚刚发现的解决方法,将R块放置在图形环境中,并将\label标签放置在标签之后或内部.

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}

好的,我发现了一种解决方法,即将R块放在LaTeX的\begin{figure} . . .\end{figure}环境中.我可以在相同的环境中创建标签.不过,我想了解Yihui打算如何使用KnitR处理此问题.

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.

推荐答案

您需要设置fig.cap = ''(或所需的任何值)以确保在latex文档中使用了图形环境. (您可能已经注意到\begin{figure} ... \end{figure}\label{}组件一起丢失

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

例如

\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.cap:(NULL;字符)要在LaTeX的图形环境中使用的图形标题(在\ caption {}中);如果为NULL或NA,则为 忽略,否则图形环境将用于 块(在\ begin {figure}和\ end {figure}中输出)

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

如果您希望复制R会话输出,则不希望这些图形脱离定义其创建方式的代码行.

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.

这篇关于使用KnitR从R图绘制LaTeX图形标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:49
查看更多