Knitr在R代码中转义乳胶特殊字符(例如〜,$)

Mar*_*son 5 latex r beamer knitr

当我在这个knitr文档的输出上运行时,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}
Run Code Online (Sandbox Code Playgroud)

ton*_*nov 3

[fragile]如果您的框架包含带有特殊乳胶字符的针织块,您需要添加选项:

\begin{frame}[fragile]
Run Code Online (Sandbox Code Playgroud)

来源:knitr 网页