Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

! 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. Here is the R Markdown file:

---
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))
```

This above example, however, works if I set echo=FALSE in the header. 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:

  • R version 3.3.1 (2016-06-21)
  • Platform: x86_64-pc-linux-gnu (64-bit),
  • running Ubuntu 16:10
  • I made sure I have the texlive-latex-base, texlive-latex-recommended, and texlive-latex-extra package installed, but still no luck
  • I also tried generating a pdf from the intermediate .tex file, as suggested here, but I get the same error.
  • I considered this approach, but it didn't work, I still got an error (maybe I need to tweak to work in my context, but not sure how)
like image 694
HAVB Avatar asked Dec 09 '16 03:12

HAVB


5 Answers

So, the issue here is the rticles templates sometimes omit a means for pandoc to inject code highlighting environments, e.g. Shaded. To get around this, you would have to insert into the template.tex preamble:

$if(highlighting-macros)$
$highlighting-macros$
$endif$
like image 190
coatless Avatar answered Oct 18 '22 19:10

coatless


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. Obviously, I played around with the wrong files before I came across a related question on rticles and how to embed (missing) LATEX.

The longer answer can be found in my "another question". For reference here are the key steps:

  1. locate the template.tex file in your R package rticles library. If you do not know where you have your package library use .libPaths() in your RStudio console. Then work your way to the resources subfolder of your rticle template. In my case: R-3.5.0/library/rticles/rmarkdown/templates/ieee_article/resources.

  2. add the fix proposed by coatless above in the preamble of the template.tex. The preamble is anything before the line \begin{document}:

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

  3. save template.tex and go back to R/RStudio and hit the knit button. R code chunks are now nicely displayed, if you do not suppress their printing with echo = FALSE.

like image 41
Ray Avatar answered Oct 18 '22 18:10

Ray


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. However the environment only exists if you are using R chunks in your pdf. So if you don't have any r chunks in the pdf, it tries to redefine something which was never defined - causing the error.

The solution is to modify the \renewenvironment{Shaded} command in the latex template.

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

like image 43
nickasaurous Avatar answered Oct 18 '22 18:10

nickasaurous


I found a work around. If I hide the code with chunk option echo = FALSE, a PDF is generated with no issues.

Reproducible example 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))
```

Knit to PDF fails with ! LaTeX Error: Environment Shaded undefined.

Example 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))
```

Knit to PDF works!.

The only difference between the two examples is adding echo=FALSE to the code chunk header. 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.

like image 35
HAVB Avatar answered Oct 18 '22 17:10

HAVB


The rticles templates typically do not directly allow shading of code. As a result, the required $highlighting-macros$ which is used to control the shading is not included.

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:

\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

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.

like image 43
Michael Harper Avatar answered Oct 18 '22 19:10

Michael Harper