当尝试使用包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
,则上面的示例有效。您不会在输出中获得代码,但对于学术论文,您可能无论如何都不需要它,如果您这样做,您可以以不同的方式显示它。
笔记:
因此,这里的问题是rticles
模板有时会省略pandoc注入代码突出显示环境的方法,例如Shaded
。要解决这个问题,你必须插入template.tex
序言:
$if(highlighting-macros)$
$highlighting-macros$
$endif$
我找到了一个解决方法。如果我使用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
添加到代码块头。您不会在输出中获得代码,但对于学术论文,您可能无论如何都不需要它,如果您这样做,您可以以不同的方式显示它。
我知道这篇文章很老了,但是我会把它放在这里供其他有同样问题的人参考。
原因是在乳胶模板中,Shaded的环境正在尝试重新定义。但是,如果您在pdf中使用R块,则仅存在环境。因此,如果您在pdf中没有任何r块,它会尝试重新定义从未定义的内容 - 导致错误。
解决方案是修改latex模板中的\ renewenvironment {Shaded}命令。
https://github.com/yihui/bookdown-chinese/commit/a3e392593b464ba31a7eceb0cd60f7e0bd112798
我努力修复错误,并实际上提出了another question,经过相当多的实验和搜索其他相关的问题/答案,我能够回答。
简而言之,由于您知道template.tex
文件的位置,上面提出的无外壳工作的解决方案是完美的。显然,我遇到了错误的文件,然后我遇到了关于rticles的related question以及如何嵌入(丢失)LATEX。
更长的答案可以在my "another question"找到。以下是关键步骤:
.libPaths()
。然后按照您的方式进入rticle模板的resources子文件夹。在我的情况下:R-3.5.0/library/rticles/rmarkdown/templates/ieee_article/resources
。template.tex
的序言中添加上面提到的无外壳修复。序言是\begin{document}
之前的任何内容:
$if(highlighting-macros)$
$highlighting-macros$
$endif$
template.tex
并返回R / RStudio并点击编织按钮。如果您不使用echo = FALSE
抑制打印,则现在可以很好地显示R代码块。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}
您可能还希望看到我对similar question的答案,即将相同的代码放入与项目相同的目录中的
header.tex
文件中。