gpt4 book ai didi

r - 如何使用 knitr 从 R 代码中打印 latex 部分标题?

转载 作者:行者123 更新时间:2023-12-04 06:36:59 25 4
gpt4 key购买 nike

我正在尝试使用 knitR 在 Latex 中生成模拟报告。我的 R 代码在产品上有一个循环,并为每个产品生成一个图表。我想为每个循环迭代包含一个部分标题。我使用了 resuls='asis' 并尝试在循环中打印部分标题,如下面的代码块所示:

<<looptest, echo=FALSE, results='asis', warning=FALSE>>=
for (product in c("prod 1","prod 2")){
print(paste("\\section{",product,"}", sep=""))
}
@

问题是我在 latex 输出中得到了这个:
[1] "\\section{prod 1}"
[1] "\\section{prod 2}"

最佳答案

托马斯建议我将其发布为答案。
解决方案是使用 cat() 而不是 print()

<<looptest, echo=FALSE, results='asis', warning=FALSE>>= 
for (product in c("prod 1","prod 2")){
cat(paste("\\section{",product,"}", sep="")) }
@

具有正确的 latex 输出:
\section{prod 1} 
\section{prod 2}

关于r - 如何使用 knitr 从 R 代码中打印 latex 部分标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17992846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com