Knitr如何在R代码中转义Latex特殊字符(例如~、$)

5

当我在这个knitr文档的输出上运行LaTeX时,LaTeX会崩溃,因为LaTeX特殊字符没有被正确转义。有什么提示如何解决这个问题?

\documentclass{beamer} 
\begin{document}
\begin{frame}{Unescaped dollar signs and tildes}

In this example, neither the tilde nor the dollar sign
will appear in the pdf document, and the dollar sign
will cause a \LaTeX error.

<<xtable, results="asis">>=
n <- 100

x <- rnorm(n)

y <- 2*x + rnorm(n)

out <- lm(y ~ x)

library(xtable)

xtable(summary(out)$coef, digits=c(0, 2, 2, 1, 2))
@

\end{frame}
\end{document}
1个回答

3

如果您的帧包含具有特殊latex字符的knitr块,则需要添加[fragile]选项:

\begin{frame}[fragile]

来源: knitr 网页.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接