【问题标题】:how to use LaTeX grid-system package in r markdown (and put r chunks within LaTeX environments)?如何在 r markdown 中使用 LaTeX 网格系统包(并将 r 块放在 LaTeX 环境中)?
【发布时间】:2017-03-26 12:28:10
【问题描述】:

我正在尝试在 r markdown 中使用 LaTeX grid-system 包。有谁知道这是怎么做到的吗?当制作包含纯文本的单元格时一切顺利,但当尝试包含 r 块时,我收到此错误:

output file: testtest.knit.md

    ! You can't use `macro parameter character #' in horizontal mode.
\gridsystem@cellcontent0 ...(cars) ``` \par ``` ##
                                                  ## speed dist #### Min. : ...
l.106 \end{Row}

pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS testtest.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output testtest.pdf --template "C:\Users\pc\Documents\R\win-library\3.3\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine xelatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43 
Execution halted

我使用了这个代码:

---
output:
  pdf_document: 
    latex_engine: xelatex
header-includes:
   - \usepackage{grid-system}
---

\begin{Row}%
    \begin{Cell}{2}
```{r cars}
summary(cars)
```
    \end{Cell}
    \begin{Cell}{1}
    Some text using 1/3 of the width.
    \end{Cell}
\end{Row}

更新:

cmets 中提到的解决方案comment=">" 适用于上面的代码,但在尝试包含绘图时,如下所示:

---
output:
  pdf_document: 
    latex_engine: xelatex
header-includes:
   - \usepackage{grid-system}
---


\begin{Row}
    \begin{Cell}{5}
```{r, comment='>'}
plot(pressure)
```
    \end{Cell}
    \begin{Cell}{1}
    Some text using 1/6 of the width.
    \end{Cell}
\end{Row}

它会产生这个错误:

! Missing $ inserted.
<inserted text> 
                $
l.98 \end{Row}

【问题讨论】:

  • 最简单的方法是避免散列。尝试使用 comment = '&gt;' 为您的块使用另一个字符作为 cmets。
  • 感谢您的回复。我不确定我是否明白你的意思。作为一个块选项?
  • {r cars, comment='&gt;'} 。或者您通过调用opts_chunk$set(comment='&gt;') 全局设置它(适用于所有块)
  • 它有效!再次感谢。如果您将此作为答案,我会接受。如果我用plot(pressure 替换summary(cars),我仍然得到一个错误。关于那个解决方案的任何想法?

标签: r latex r-markdown grid-system


【解决方案1】:

有几个符号在 Tex 中具有特殊含义。哈希用于创建宏。因此,当您使用散列而不转义它时,LaTex 会感到困惑。 将块选项 comment 设置为另一个符号,如 &gt; 或根本没有符号 ('') 将改变这一点。

关于您的第二个问题,我不确定为什么将代码翻译以包含最终 pdf 中的情节失败。一种解决方法是生成绘图并直接使用 LaTeX 调用来包含文件:

```{r cars, engine='R', echo = F, include=F, fig.path='plots/'}
plot(pressure, col = 'blue', pch = 16)
```
\includegraphics{plots/cars-1.pdf} 

您可以使用fig.path 控制文件夹的名称。使用块名称命名地块。

【讨论】:

    猜你喜欢
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多