Rstudio PDF Knit出现“Environment Shaded undefined”错误

6
当我尝试使用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}

当我在输出文档中包含代码块时,似乎只会出现这种情况。

可重现的示例:

您需要使用新建文档 -> 从模板 -> 计算机协会来启动一个新的 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))
```

然而,在头文件中设置echo=FALSE后,上述示例是有效的。您将不会在输出中得到代码,但对于学术论文,您可能不需要它,如果需要,可以以不同的方式显示它。


注释:

  • R版本3.3.1(2016-06-21)
  • 平台:x86_64-pc-linux-gnu(64位),
  • 运行Ubuntu 16:10
  • 我确保安装了texlive-latex-base,texlive-latex-recommended和texlive-latex-extra软件包,但仍然没有成功
  • 我也尝试从中间的.tex文件生成pdf,如这里所建议的,但我得到相同的错误。
  • 我考虑了这种方法,但它没有起作用,我仍然得到错误(也许我需要调整以使其适合我的环境,但不确定如何调整)

1
你尝试过这个方法吗?https://dev59.com/XZHea4cB1Zd3GeqPvOFo。 - J_F
如果仍然无法正常工作,请提供一个最小的示例。 - Christoph
我无法尝试那种方法,因为它需要一个 .md 文件来提供给 pandoc 命令。在我的情况下,编织过程在输出 .md 文件之前失败了。 - HAVB
我找到了一个满足我的需求的替代方案。如果我使用代码块选项 echo = FALSE 隐藏代码,就可以生成没有问题的 PDF 文件。由于我在写论文时真的不需要显示任何代码,这个解决方法已经足够好了。 - HAVB
5个回答

17

所以,这里的问题是rticles模板有时会省略一种让pandoc注入代码高亮环境(例如Shaded)的方法。为了解决这个问题,您需要在template.tex导言区中插入以下内容:

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

1
修复已经被纳入rticles软件包的开发版本中(下一个CRAN版本应该是0.6)。谢谢! - Yihui Xie

7

我在修复错误时遇到了困难,并提出了另一个问题,经过一番实验和搜索其他相关问题/答案后,我终于得以回答。

简而言之,上面由coatless提供的解决方案非常完美,只要你知道template.tex文件的位置即可。显然,在找到与rticles相关的相关问题并了解如何嵌入(缺失的)LATEX之前,我尝试了错误的文件。

更详细的答案可以在我的“另一个问题”中找到。以下是关键步骤:

  1. 在R包rticles库中找到template.tex文件。如果您不知道自己的软件包库在哪里,请在RStudio控制台中使用.libPaths()。然后进入rticle模板的资源子文件夹。在我的情况下是: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并单击knit按钮。现在,如果您不使用echo = FALSE抑制其打印,R代码块将被很好地显示。


1
我们需要在哪里添加这个?我很难确定要放置它的代码块。 - Aditya Peshave
2
您可以在“template.tex”文件的“preamble”中的任何位置插入这个三行修复程序,即在\begin{document}行之前的任何位置。正如您在我的其他问题中所看到的那样。 - Ray

4
我知道这篇文章已经有点老了,但我会把这里的内容放在这里,供那些遇到同样问题的人参考。原因是在latex模板中尝试重新定义环境Shaded。然而,只有当您在pdf中使用R代码块时,该环境才存在。所以,如果您的pdf中没有任何r块,它会尝试重新定义从未定义过的内容,导致错误。解决方法是修改latex模板中的\renewenvironment{Shaded}命令。 https://github.com/yihui/bookdown-chinese/commit/a3e392593b464ba31a7eceb0cd60f7e0bd112798

2
文章模板通常不允许直接对代码进行阴影处理。因此,控制阴影的必要的 $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}

enter image description here

您可能还希望查看我对一个类似问题的回答(链接),其中相同的代码被放置在与您的项目相同目录下的header.tex文件中。

大学实验室对“程序文件”更改的访问权限有限,因此这似乎是我最好的解决方案。 - pal

2

我找到了一个解决方法。如果我使用代码块选项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))
```

使用Knit to PDF时出现错误:!LaTeX错误:未定义环境Shaded。

示例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。您将不会在输出中得到代码,但对于学术论文,您可能不需要它,如果需要,可以以不同的方式显示它。


感谢您指出这一点。想法是展示代码块并设置echo = FALSE(隐藏代码块,但有效),但这不是期望的输出。然而,如果意图不是展示代码块,则此“解决方案”可行。 - Ray

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接