用\$替换$; knitr block 选项 : echo=TRUE, 结果 ='markup;$=\\$'

标签 r knitr pdflatex

我正在尝试使用 knitr 在 Latex 文档中打印 R 代码。

我有以下简单代码:

  \begin{frame}{Plot Code}
  <<plotcode,eval=FALSE,echo=TRUE,results='markup;$=\\$'>>=

...来自随机抽取数据的简单 lm ...

  yhat.fit <- yhat$fit[,"fit"]

...更简单的代码...

 \end{frame}

这将运行,但是,没有替换 $ 符号。相反,我得到了数学模式 fit[,"fit"] 在幻灯片中(以及来自 pdflatex 编译的“missing $ inserted”错误。

我宁愿没有数学模式;相反,我希望看到 $ 符号,因为它是 R 代码不可或缺的一部分。

作为我的解决方法,我在生成的 .tex 文件中用 \$ 替换了 $,但我希望有人可以修复我的问题看似简单的问题。我想我没有在选项中正确指定标记和字符结构,但我尝试了大量变体但没有成功。

最佳答案

我不明白 results='markup;$=\\$' 的真正含义。 results option不采用这样的值;目前,它只接受 markupasishidehold

我也不明白 $ 怎么会成为 beamer 中的问题。您可能忘记了 fragile 选项。一个最小的例子:

\documentclass{beamer}
\begin{document}
\title{A Minimal Demo of knitr}
\author{Yihui Xie}

\maketitle

% very important to use option [fragile] for frames containing code output!

\begin{frame}[fragile]

You can test if \textbf{knitr} works with this minimal demo. OK, let's
get started with some boring random numbers:

<<boring-chunk>>=
fit = lm(dist ~ speed, data = cars)
fit$coefficients
print(sessionInfo(), locale = FALSE)
@

\end{frame}

\end{document}

输出:

knitr with beamer

关于用\$替换$; knitr block 选项 : echo=TRUE, 结果 ='markup;$=\\$',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21533017/

相关文章:

r - 从重复表中创建 id 变量

r - ggbiplot - 改变点的大小

r - 控制两个 knitr 并排图之间的距离

pdf - 如何将两个PDF页面拼接成一个大页面?

python-sphinx - 如何在 (pdflatex) sphinx 文档中的 TOC 之前添加前言或前言?

r - 'order' 的输出对我来说没有意义

r - 使用 `.value` 和 `pivot_longer()` 时如何将后缀附加到 `names_pattern`

r - 简单的手动 RMarkdown 表格,在 HTML、PDF 和 DOCX 中看起来不错

Rmarkdown 图像跳过文本

r - RMarkdown 中的 TinyTex 错误(本地 TeX Live (2021) 比远程存储库 (2022) 旧)