Knitr escape latex special characters (e.g., ~, $) in R code

£可爱£侵袭症+ 提交于 2019-12-10 15:11:49

问题


LaTeX crashes when I run it on the output of this knitr document because the LaTeX special characters aren't properly escaped. Any hints how to fix this?

\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:


You need to add [fragile] option if your frame contains a knitr chunk with special latex characters:

\begin{frame}[fragile]

Source: knitr webpage.



来源:https://stackoverflow.com/questions/25800985/knitr-escape-latex-special-characters-e-g-in-r-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!