[英]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
,則上面的示例有效。 您不會在輸出中獲得代碼,但對於學術論文,您可能無論如何都不需要它,如果您這樣做,您可以以不同的方式顯示它。
筆記:
因此,這里的問題是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之前,我使用了錯誤的文件。
更長的答案可以在我的“另一個問題”中找到 。 以下是關鍵步驟:
在您的R包rticles庫中找到template.tex文件。 如果您不知道軟件包庫的.libPaths()
,請在RStudio控制台中使用.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$
。
如果出於任何原因,您不能(或不想)按照@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.