Rstudio pdf knit失败,出现“Environment Shaded undefined”错误

问题描述 投票:3回答:5

当尝试使用包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开始一个新的Markdown文档。这是R Markdown文件:

---
title: Short Paper
author:
  - name: I Am Me
    email: [email protected]
    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,如here建议的那样,但是我得到了同样的错误。
  • 我考虑过this approach,但它不起作用,我仍然有一个错误(也许我需要调整以在我的上下文中工作,但不知道如何)
r rstudio r-markdown pdflatex
5个回答
10
投票

因此,这里的问题是rticles模板有时会省略pandoc注入代码突出显示环境的方法,例如Shaded。要解决这个问题,你必须插入template.tex序言:

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

2
投票

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

可重复的示例1:

title: Short Paper
author:
  - name: I Am Me
    email: [email protected]
    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: [email protected]
    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添加到代码块头。您不会在输出中获得代码,但对于学术论文,您可能无论如何都不需要它,如果您这样做,您可以以不同的方式显示它。


2
投票

我知道这篇文章很老了,但是我会把它放在这里供其他有同样问题的人参考。

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

解决方案是修改latex模板中的\ renewenvironment {Shaded}命令。

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


2
投票

我努力修复错误,并实际上提出了another question,经过相当多的实验和搜索其他相关的问题/答案,我能够回答。

简而言之,由于您知道template.tex文件的位置,上面提出的无外壳工作的解决方案是完美的。显然,我遇到了错误的文件,然后我遇到了关于rticles的related question以及如何嵌入(丢失)LATEX。

更长的答案可以在my "another question"找到。以下是关键步骤:

  1. 在您的R包rticles库中找到template.tex文件。如果您不知道软件包库的位置,请在RStudio控制台中使用.libPaths()。然后按照您的方式进入rticle模板的resources子文件夹。在我的情况下:R-3.5.0/library/rticles/rmarkdown/templates/ieee_article/resources
  2. template.tex的序言中添加上面提到的无外壳修复。序言是\begin{document}之前的任何内容: $if(highlighting-macros)$ $highlighting-macros$ $endif$
  3. 保存template.tex并返回R / RStudio并点击编织按钮。如果您不使用echo = FALSE抑制打印,则现在可以很好地显示R代码块。

1
投票

rticles模板通常不直接允许着色代码。因此,不包括用于控制阴影的所需$highlighting-macros$

如果出于任何原因,您不能(或不想)按照template.tex的建议直接编辑@coatless文件,您也可以考虑直接在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

您可能还希望看到我对similar question的答案,即将相同的代码放入与项目相同的目录中的header.tex文件中。

© www.soinside.com 2019 - 2024. All rights reserved.