Rstudio pdf knit 失败并出现 "Environment Shaded undefined" 错误

Rstudio pdf knit fails with "Environment Shaded undefined" error

尝试使用包 rticles output: rticles::acm_article 中的模板编织 PDF 时,出现以下错误:

! LaTeX Error: Environment Shaded undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.76 \begin{Shaded}

这似乎只有在我在输出文档中包含代码块时才会发生。

可重现的例子

您将需要使用 New Document -> From Template -> Association for Computing Machinery 开始一个新的 R Markdown 文档。这是 R Markdown 文件:

---
title: Short Paper
author:
  - name: I Am Me
    email: me@email.com
    affiliation: Fictional University
abstract: |
  This is the abstract.
  It consists of two paragraphs.
output: 
  rticles::acm_article:
    keep_tex: true
---


## Simple test

Code chuck follows:

```{r}
plot(rnorm(10))
```

但是,如果我在 header 中设置 echo=FALSE,则上面的示例有效。您不会在输出中获得代码,但对于学术论文,您可能根本不需要它,如果需要,您可以以不同的方式显示它。


备注:

我找到了解决方法。如果我使用块选项 echo = FALSE 隐藏代码,则生成的 PDF 没有问题。

可重现示例 1:

title: Short Paper
author:
  - name: I Am Me
    email: me@email.com
    affiliation: Fictional University
abstract: |
  This is the abstract.

  It consists of two paragraphs.
bibliography: sigproc.bib
output: 
  rticles::acm_article:
    keep_tex: true
---


## Simple test

Code chuck follows:

```{r}
plot(rnorm(10))
```

编织成 PDF 失败 ! LaTeX Error: Environment Shaded undefined.

示例 2:

---
title: Short Paper
author:
  - name: I Am Me
    email: me@email.com
    affiliation: Fictional University
abstract: |
  This is the abstract.

  It consists of two paragraphs.
bibliography: sigproc.bib
output: 
  rticles::acm_article:
    keep_tex: true
---


## Simple test

Code chuck follows:

```{r echo=FALSE}
plot(rnorm(10))
```

编织成 PDF 作品!。

两个示例之间的唯一区别是将 echo=FALSE 添加到代码块 header。您不会在输出中获得代码,但对于学术论文,您可能根本不需要它,如果需要,您可以以不同的方式显示它。

所以,这里的问题是 rticles 模板有时会忽略 pandoc 注入代码高亮环境的方法,例如Shaded。要解决这个问题,您必须在 template.tex 序言中插入:

$if(highlighting-macros)$
$highlighting-macros$
$endif$

我知道post是旧的,但我会把它放在这里供有同样问题的其他人参考。

原因是在 Latex 模板中试图重新定义 Shaded 环境。但是,仅当您在 pdf 中使用 R 块时,环境才存在。因此,如果您在 pdf 中没有任何 r 块,它会尝试重新定义从未定义的内容 - 导致错误。

解决办法是修改latex模板中的\renewenvironment{Shaded}命令

https://github.com/yihui/bookdown-chinese/commit/a3e392593b464ba31a7eceb0cd60f7e0bd112798

我努力修复错误,实际上提出了 ,经过相当多的实验和搜索其他相关的 questions/answers.

,我能够回答这个问题

简而言之,如果您知道 template.tex 文件的位置,coatless 提供的上述解决方案非常完美。显然,在我遇到关于文章和如何嵌入(丢失)LATEX 的 之前,我使用了错误的文件。

可以在 中找到更长的答案。以下是关键步骤供参考:

  1. 在您的 R 包 rticles 库中找到 template.tex 文件。如果您不知道包库在哪里,请在 RStudio 控制台中使用 .libPaths()。然后转到您的文章模板的资源子文件夹。就我而言: R-3.5.0/library/rticles/rmarkdown/templates/ieee_article/resources.

  2. template.tex 的序言中添加上面由 coatless 提出的修复。序言是 \begin{document} 行之前的任何内容:

    $if(highlighting-macros)$ $highlighting-macros$ $endif$

  3. 保存 template.tex 并返回 R/RStudio 并点击编织按钮。如果您不使用 echo = FALSE.

  4. 抑制它们的打印,R 代码块现在可以很好地显示

文章 模板通常不允许直接对代码进行着色。因此, 不包括用于控制阴影的必需 $highlighting-macros$

如果出于任何原因,您不能(或不想)按照 @coatless 的建议直接编辑 template.tex 文件,您也可以考虑将代码直接包含在 acm_proc_article-sp.cls 文件:

\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}

You may also wish to see my answer to a , whereby the same code is put into a header.tex file in the same directory as your project.