2

I have a character vector containing ampersand (&) that displays correctly when used outside LaTeX environment, such as \begin{itemize}.

This works:

---
output:
  pdf_document
---

`r "Guns & Roses"`

The above code runs without error, meaning that either knitr or rmarkdown escapes the ampersand automatically.

Now, when the same R character vector with ampersand is specified inside LaTeX environment, following error is thrown: ! Misplaced alignment tab character &.:

---
output:
  pdf_document
---

\begin{itemize}
\item `r "Guns & Roses"`
\end{itemize}
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
  • 1
    The `&` character has a special meaning in LaTeX. To use it literally, you need to escape it: `\&` which is `\\&` in R. I think this question should be closed because it is not about `knitr` but about LaTeX and in the latter case a duplicate of [this question](http://tex.stackexchange.com/questions/95270/how-do-i-use-literally-in-latex). – CL. Nov 15 '15 at 15:46
  • 1
    Well aware of need to escape ampersand in LaTeX `\&`. Question is different though: about `knitr` or `rmarkdown` way of handling the ampersand differently within environment vs outside the environment. First example (minimal and reproducible, if you actually run it) demonstrates that either `knitr` or `rmarkdown` somehow escapes ampersand automatically. – Daniel Krizian Nov 15 '15 at 15:56
  • 1
    I see, thank you for the clarification. Not tested yet, but I think the difference is due to `pandoc`. It escapes special LaTeX characters only *outside* of LaTeX environments. [This answer](http://stackoverflow.com/a/33689497/2706569) explains a similar case where `pandoc` escapes some characters but not others. – CL. Nov 15 '15 at 16:00
  • Thank you, that is additive info regarding the `pandoc`. If that's true, maybe I should re-tag from `knitr` to `pandoc` and leave `rmarkdown`. `rmarkdown` depends on `knitr` anyway, AFAIK. Does `pandoc` have option/extension to escape also *inside* LaTeX environments? – Daniel Krizian Nov 15 '15 at 16:04
  • I don't know how and I don't think so, sorry. You can disable the [raw_tex](http://pandoc.org/README.html#raw-tex) extension but then *all* LaTeX is escaped. The problem is, `pandoc` cannot "know" what to escape and what not, AFAIK. – CL. Nov 15 '15 at 16:16
  • Inspired by [this SO](http://stackoverflow.com/questions/9896173/way-to-automatically-escape-characters-etc-using-knitr), I think the way to go is wrapping the `R` inliner into `\verb`atim environment: `\verb| ``r "Guns & Roses"``|`. Broader solution than messing with each `R` string by adding backslash. – Daniel Krizian Nov 15 '15 at 16:50
  • 1
    If that works for you (despite the ugly formatting) you may want to use `knitr::knit_hooks$set(inline = function(x) {return(paste("\\verb|", x, "|"))})`. – CL. Nov 15 '15 at 16:58
  • Thanks, interesting. I've tried inserting that hook line as first chunk of the `Rmd` file, but still get `! Misplaced alignment tab character &`. Would you mind posting the complete reproducible code as an answer? – Daniel Krizian Nov 15 '15 at 17:15

2 Answers2

5

The reason why the first snippet in the question runs without errors but the second does not is that the & needs to be escaped in LaTeX and pandoc escapes it in the first case but not in the second.

This is because pandoc (with the extension raw_tex) doesn't escape "material between the begin and end tags".

knitr's output hook inline can be used to solve the problem.

One solution—that works for the OP, according to the comments—is to enclose output from R inline expressions in a verbatim environment where the & characer is allowed.

---
output:
  pdf_document
---

```{r, echo = FALSE}
knitr::knit_hooks$set(inline = function(x) {
  return(paste("\\verb|", x, "|"))
})
```

\begin{itemize}
\item `r "Guns & Roses"`
\end{itemize}

Alternatively, the following output hook for inline expressions could be used to replace all & by \&:

knitr::knit_hooks$set(inline = function(x) {
  return(gsub(pattern = "&", 
              replacement = "\\&", 
              x = x, 
              fixed = TRUE))
})
CL.
  • 14,577
  • 5
  • 46
  • 73
  • Thanks @CL. I used the output hook for inline expressions you shared above, and wrapped it within an `if` to only run if `latex` output, and this worked beautifully for my purposes! – JHawkins Oct 05 '20 at 21:27
-1

I had a similar problem in Rmarkdown, where the Ampersand was supposed to show up in the labels of a ggplot. Using the output hooks CL suggested did not work for me. However, what did help was to put this specific ggplot into its own code chunk (note that I did not escape the ampersand! It is only part of a string variable)– Perhaps a strange workaround for Rmarkdown hickups but seems to work.

IzzyBizzy
  • 43
  • 1
  • 9