knitr:在fig.cap块选项中使用内联表达式 [英] knitr: Use an inline expression in fig.cap chunk option
问题描述
我的问题是使用LaTeX时有关knitr选项fig.cap的问题.是否可以包含\ rinline或\ Sexpr 在fig.cap字符串中?
My question is about the knitr option fig.cap, when using LaTeX. Is it possible to include an \rinline or \Sexpr in the fig.cap character string?
例如,我想要一个类似的东西(我正在使用.Rtex文件):
For example, I would like to have something like (I'm using an .Rtex file):
\documentclass{article}
\begin{document}
%% begin.rcode fig.cap="x is \\rinline{x}"
% x <- 5
% p <- seq(0,5)
% q <- x*p
% plot(p,q)
%% end.rcode
\end{document}
我真的很想让那个块在我的.tex文档中生成一个图,其标题为"x is 5".而是,它在pdflatex编译时引发未定义的控制序列"错误.
I'd really like for that chunk to produce a plot in my .tex document, with a caption reading "x is 5". Instead, it throws an "undefined control sequence" error on pdflatex compilation.
如果我不逃避rinline(即仅使用\ rinline {x}),那么它将编译,但标题为"x is inlinex".
If I don't escape the rinline (i.e. use just \rinline{x}), then it compiles, but the caption is "x is inlinex".
我要问的可能吗?
这是我的第一个SO问题(不过,在这里多次使用了答案.谢谢!),因此,我希望能收到有关如何提出更好问题的反馈.
This is my first SO question (used the answers here many times, though. Thanks!), so I'd appreciate any feedback on how to ask better questions.
感谢您的帮助!
推荐答案
fig.cap
被评估为R表达式,因此您可以使用\rinline
(而不是使用knitr
再次解析标题)来代替只需在R中创建标题字符串即可.
fig.cap
is evaluated as an R expression, so instead of using \rinline
(and thus having the caption again parsed by knitr
), you can just create the caption string in R.
%% begin.rcode fig.cap=paste("x is", x)
,但是由于默认情况下在创建x
评估了fig.cap
,因此您需要推迟对fig.cap
的评估.为此,您可以在文档的开头添加如下代码:
but because fig.cap
is evaluated before x
is created by default, you will need to postpone the evaluation of fig.cap
; to do that, you can include a chunk like this in the beginning of your document:
%% begin.rcode setup, include=FALSE
%% opts_knit$set(eval.after = 'fig.cap')
%% end.rcode
它指定在评估代码块之后要评估的fig.cap
,即当x
可用于图形标题时.请参阅文档中的 eval.after
.
It specifies fig.cap
to be evaluated after the code chunk is evaluated, i.e. when x
is available for you to use in the figure caption. See eval.after
in the documentation.
另一种方法是在上一个块中创建x
,然后在下一个块中使用fig.cap=paste("x is", x)
.
The other way to do this is to create x
in a previous chunk, and use fig.cap=paste("x is", x)
in the next chunk.
这篇关于knitr:在fig.cap块选项中使用内联表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!