繁体   English   中英

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

[英]Rstudio pdf knit fails with “Environment Shaded undefined” 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}

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

可重复的例子

您需要使用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))
```

但是,如果我在标头中设置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,如此处所示 ,但我得到了同样的错误。
  • 我考虑过这种方法 ,但它没有用,我仍然有错误(也许我需要调整才能在我的上下文中工作,但不知道如何)

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

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

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

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

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

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

我找到了一个解决方法。 如果我使用chunk选项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. ! 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添加到代码块头。 您不会在输出中获得代码,但对于学术论文,您可能无论如何都不需要它,如果您这样做,您可以以不同的方式显示它。

我努力修复错误并实际提出了另一个问题 ,经过相当多的实验和搜索其他相关问题/答案后我才能回答。

简而言之,由于您知道template.tex文件的位置,因此无涂层工作的上述解决方案非常完美。 显然,在我遇到关于rticles的相关问题以及如何嵌入(丢失)LATEX之前,我使用了错误的文件。

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

  1. 在您的R包rticles库中找到template.tex文件。 如果您不知道软件包库的.libPaths() ,请在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代码块。

rticles模板通常不直接允许着色代码。 因此,不包括用于控制着色的所需$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}

在此输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM