问题描述
我无法让 R/KnitR 为图形创建 LaTeX label{}
语句.manual 似乎表明 label{}
语句将由将 fig.lp 中的字符串(默认为fig:")与 R 代码块的标签连接.但是,我无法让它发挥作用.没有为通过编织下面的 MWE 创建的第一个图形创建 label{}
语句.第二个图的标签添加了我刚刚发现的解决方法,将 R 块放在图环境中,并将 label
标签放在 caption
之后或内部标记.
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}
egin{document}
Figure
ef{fig:plot} doesn't have it's label.
<<plot>>=
plot(x=0, y=0)
@
Figure
ef{fig:plot2} has its label.
egin{figure}
caption{label{fig:plot2}}
<<>>=
plot(x=1,y=1)
@
end{figure}
end{document}
好的,我通过将 R 块放在 egin{figure} 中找到了解决方法..LaTeX 中的 .end{figure}
环境.我可以在相同的环境中创建标签.不过,我还是想了解 Yihui 打算如何用 KnitR 处理这个问题.
Okay, I've found a workaround by putting the R chunk in a egin{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 = ''
(或任何你想要的)以确保在乳胶
文件.(您可能已经注意到 egin{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 egin{figure} ... end{figure}
is missing along with the label{}
component
例如
documentclass[12pt, english, oneside]{amsart}
egin{document}
See Figure
ef{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') 用于数字的 LaTeX 环境,例如设置 fig.env='marginfigure' 得到 egin{marginfigure}
fig.cap: (NULL; character) 在 LaTeX 中的图形环境中使用的图形标题(在 caption{} 中);如果为 NULL 或 NA,它将是忽略,否则图形环境将用于块(在 egin{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 egin{figure} and end{figure})
虽然图形手册很清楚,推理也有道理
如果块选项 fig.cap 不为 NULL 或 NA,则当输出格式为LATEX,这个选项用来在这个环境下写一个标题使用 caption{}.其他两个相关选项是 fig.scap 和fig.lp 为图形设置短标题和前缀字符串标签.默认短标题是从标题中提取的在第一个句号或冒号或分号处截断它.标签是fig.lp 和块标签的组合.因为 figure 是一个浮点数环境,它可以从块输出浮动到其他地方例如编译 TEX 文档时页面的顶部或底部.
如果您希望复制一个 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 图形标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!