[英]Rstudio pdf knit fails with “Environment Shaded undefined” error
When trying to knit a PDF using a template from package rticles output: rticles::acm_article
I get the following error: 当尝试使用包rticles
output: rticles::acm_article
的模板编织PDF时output: rticles::acm_article
我收到以下错误:
! LaTeX Error: Environment Shaded undefined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.76 \begin{Shaded}
This appears to only happen when I include code chunks within the output document. 这似乎只有在输出文档中包含代码块时才会发生。
Reproducible example : 可重复的例子 :
You will need to start a new R Markdown document using the New Document -> From Template -> Association for Computing Machinery
. 您需要使用
New Document -> From Template -> Association for Computing Machinery
开始新的R Markdown文档。 Here is the R Markdown file: 这是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))
```
This above example, however, works if I set echo=FALSE
in the header. 但是,如果我在标头中设置
echo=FALSE
,则上面的示例有效。 You won't get code in the output, but for an academic paper you probably don't need it anyway, an if you do you can display it in a different manner. 您不会在输出中获得代码,但对于学术论文,您可能无论如何都不需要它,如果您这样做,您可以以不同的方式显示它。
Notes: 笔记:
So, the issue here is the rticles
templates sometimes omit a means for pandoc to inject code highlighting environments, eg Shaded
. 因此,这里的问题是
rticles
模板有时会省略pandoc注入代码突出显示环境的方法,例如Shaded
。 To get around this, you would have to insert into the template.tex
preamble: 要解决这个问题,您必须插入
template.tex
前言:
$if(highlighting-macros)$
$highlighting-macros$
$endif$
I know the post is old, but I'll put this here for reference for others who have the same problem. 我知道这篇文章很老了,但是我会把它放在这里供其他有同样问题的人参考。
The reason is that in the latex template the environment Shaded is trying to be redefined. 原因是在乳胶模板中,Shaded的环境正在尝试重新定义。 However the environment only exists if you are using R chunks in your pdf.
但是,如果您在pdf中使用R块,则仅存在环境。 So if you don't have any r chunks in the pdf, it tries to redefine something which was never defined - causing the error.
因此,如果您在pdf中没有任何r块,它会尝试重新定义从未定义的内容 - 导致错误。
The solution is to modify the \\renewenvironment{Shaded} command in the latex template. 解决方案是修改latex模板中的\\ renewenvironment {Shaded}命令。
https://github.com/yihui/bookdown-chinese/commit/a3e392593b464ba31a7eceb0cd60f7e0bd112798 https://github.com/yihui/bookdown-chinese/commit/a3e392593b464ba31a7eceb0cd60f7e0bd112798
I found a work around. 我找到了一个解决方法。 If I hide the code with chunk option echo = FALSE, a PDF is generated with no issues.
如果我使用chunk选项echo = FALSE隐藏代码,则会生成一个没有问题的PDF。
Reproducible example 1: 可重复的示例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 fails with ! LaTeX Error: Environment Shaded undefined.
编织到PDF失败
! LaTeX Error: Environment Shaded undefined.
! LaTeX Error: Environment Shaded undefined.
Example 2: 例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))
```
Knit to PDF works!. 针织PDF作品!
The only difference between the two examples is adding echo=FALSE
to the code chunk header. 两个示例之间的唯一区别是将
echo=FALSE
添加到代码块头。 You won't get code in the output, but for an academic paper you probably don't need it anyway, an if you do you can display it in a different manner. 您不会在输出中获得代码,但对于学术论文,您可能无论如何都不需要它,如果您这样做,您可以以不同的方式显示它。
I struggled with fixing the error and actually raised another question which I was able to answer after quite some experimentation and searching for other related questions/answers. 我努力修复错误并实际提出了另一个问题 ,经过相当多的实验和搜索其他相关问题/答案后我才能回答。
In short the solution presented above by coatless works perfect given you know the location of the template.tex
file. 简而言之,由于您知道
template.tex
文件的位置,因此无涂层工作的上述解决方案非常完美。 Obviously, I played around with the wrong files before I came across a related question on rticles and how to embed (missing) LATEX. 显然,在我遇到关于rticles的相关问题以及如何嵌入(丢失)LATEX之前,我使用了错误的文件。
The longer answer can be found in my "another question" . 更长的答案可以在我的“另一个问题”中找到 。 For reference here are the key steps:
以下是关键步骤:
locate the template.tex file in your R package rticles library. 在您的R包rticles库中找到template.tex文件。 If you do not know where you have your package library use
.libPaths()
in your RStudio console. 如果您不知道软件包库的
.libPaths()
,请在RStudio控制台中使用.libPaths()
。 Then work your way to the resources subfolder of your rticle template. 然后按照您的方式进入rticle模板的resources子文件夹。 In my case:
R-3.5.0/library/rticles/rmarkdown/templates/ieee_article/resources
. 就我而言:
R-3.5.0/library/rticles/rmarkdown/templates/ieee_article/resources
。
add the fix proposed by coatless above in the preamble of the template.tex
. 在
template.tex
的前言中添加上面无涂层提出的修复。 The preamble is anything before the line \\begin{document}
: 前导码是行
\\begin{document}
之前的任何内容:
$if(highlighting-macros)$ $highlighting-macros$ $endif$
save template.tex
and go back to R/RStudio and hit the knit button. 保存
template.tex
并返回R / RStudio并点击编织按钮。 R code chunks are now nicely displayed, if you do not suppress their printing with echo = FALSE
. 如果不使用
echo = FALSE
抑制打印,则现在可以很好地显示R代码块。
The rticles templates typically do not directly allow shading of code. rticles模板通常不直接允许着色代码。 As a result, the required
$highlighting-macros$
which is used to control the shading is not included. 因此,不包括用于控制着色的所需
$highlighting-macros$
。
If for any reason, you cannot (or do not) want to directly edit the template.tex
file as suggested by @coatless
, you can alternatively consider include the code directly in the acm_proc_article-sp.cls
file: 如果出于任何原因,您不能(或不想)按照
@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 similar question , whereby the same code is put into a
header.tex
file in the same directory as your project.您可能还希望看到我对类似问题的回答,即将相同的代码放入与项目相同的目录中的
header.tex
文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.