3

I'm compiling a report using Papaja and Rmarkdown, and I want to highlight various text throughout. Doing so with latex or pure Rmarkdown is easy, but I'm receiving an "undefined control sequence" error when I compile the report to PDF using Papaja's application of Knitr.

A similar question about text highlighting within a single Rmarkdown file was answered here: RMarkdown / pandoc fails to knit Pdf with latex color commands. I'd like to know if an answer exists for multiple Rmarkdown files using Papaja.

I'll include a minimal example below.

1) File called papaja_file.Rmd

---

# https://crsh.github.io/papaja_man/index.html

title             : "Some Title"
shorttitle        : "HEADER"

author: 
  - name          : "Name R Here"
    affiliation   : "1"
    corresponding : yes    # Define only one corresponding author
    address       : "Include addresss"
    email         : "randomemail@usa.edu"

affiliation:
  - id            : "1"
    institution   : "Any University"



author_note: |
  ....

abstract: |
  Text here for abstract. 

keywords          : "Keyword1, keyword2, keyword3"
bibliography      : ["references.bib"]

figsintext        : no
figurelist        : no
tablelist         : no
footnotelist      : no
lineno            : yes

lang              : "english"
class             : "man"
output            : papaja::apa6_pdf
---

```{r load_packages, include = FALSE}
library("papaja")
```

```{r analysis_preferences}
# Seed for random number generation
set.seed(42)
```

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.path = 'figures/', echo = TRUE, warning = FALSE, message = FALSE)
```


```{r child = 'child_document.Rmd'}
```


\newpage

# References
```{r create_references, echo = F}
r_refs(file = "references.bib")
```

\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}

Notice that it references a single child Rmarkdown document.

2) The child document with text, called child_document.Rmd

---
output: 
  pdf_document:
      includes:
          in_header: preamble.tex
---

One sentence without highlighting. \hl{Another with highlighting.}

3) The preamble, called preamble.tex

\usepackage{color}
\usepackage{soul}
\definecolor{lightblue}{rgb}{0.90, 0.95, 1}
\sethlcolor{lightblue}

Knitting the main papaja file produces the error. Removing the \hl command within the child document allows the pdf to compile without issue.

Result after putting YAML header within the main papaja document rather than the child:

enter image description here

Topher
  • 65
  • 8

1 Answers1

2

The YAML header in your child document is not evaluated, c.f.

The master document, for example, consists of the YAML front matter and includes the children, which are themselves R Markdown documents without a YAML front matter.

https://crsh.github.io/papaja_man/tips-and-tricks.html#splitting-an-r-markdown-document

But you can include your preamble.tex in you master file using:

output: 
  papaja::apa6_pdf:
    includes:
      in_header: preamble.tex

c.f. https://github.com/rstub/stackoverflow/commit/a92a67d4721ee9c06e995b08adbf8cb89daebcb4

Result:

enter image description here

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • Awesome. That works for commands such as `\textcolor{blue}{A sentence with colored text}` but only highlights the first bracket using: `\hl{Sentence with color}` rather than the entire sentence... – Topher Jul 16 '19 at 19:35
  • @Topher That's odd. I have added a screen shot of the result of the example document on GitHub. What do you get with that document? – Ralf Stubner Jul 16 '19 at 20:22
  • see the result above. For some reason it incorporates the brackets and highlights the first. – Topher Jul 16 '19 at 20:35
  • @Topher Which pandoc version do you use? `rmarkdown::pandoc_version()` can tell you. I am using version 2.3.1 installed with RStudio 1.2.1335. – Ralf Stubner Jul 16 '19 at 20:59
  • It looks like I am using pandoc version 2.2.3.2 with RStudio 1.1.456 – Topher Jul 16 '19 at 21:01
  • @Topher An update to current RStudio might be worth a try. IIRC there have been various changes in pandoc w.r.t. translating `{` to `\{`, which seems to be happening in your case. – Ralf Stubner Jul 16 '19 at 21:15