[英]How to print a latex section title from the R code using knitr?
I am trying to produce a simulation report in Latex with knitR. 我正在尝试使用knitR在Latex中生成模拟报告。 My R code has a loop on products and generate a graph for each products.
我的R代码在产品上有一个循环,并为每个产品生成一个图形。 I want to include a section title for each loop iteration.
我想为每个循环迭代都包含一个部分标题。 I used resuls='asis' and tried to print the section title in the loop as in the code chunk below:
我使用resuls ='asis'并尝试在循环中打印节标题,如下面的代码块所示:
<<looptest, echo=FALSE, results='asis', warning=FALSE>>=
for (product in c("prod 1","prod 2")){
print(paste("\\section{",product,"}", sep=""))
}
@
The issue is that I get this in the latex output: 问题是我在乳胶输出中得到了这个:
[1] "\\section{prod 1}"
[1] "\\section{prod 2}"
Thomas suggested me to post this as an answer. 托马斯建议我将其发布为答案。 The solution is to use cat() instead of print()
解决方案是使用cat()而不是print()
<<looptest, echo=FALSE, results='asis', warning=FALSE>>=
for (product in c("prod 1","prod 2")){
cat(paste("\\section{",product,"}", sep="")) }
@
Has the correct latex output: 具有正确的乳胶输出:
\section{prod 1}
\section{prod 2}
If you are looping through something to generate sections/chapters dynamically, I suggest use the loop functionality by knit_child
. 如果要遍历某些内容以动态生成节/章,建议使用
knit_child
的循环功能。 See the code from knitr-examples on GitHub. 请参阅GitHub上的knitr-examples中的代码 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.