Stéphane Laurent Stéphane Laurent - 26 days ago 12
R Question

Rmarkdown with pdf output and plain LaTeX code block

How to do a plain code block with

rmarkdown
when this code block contains some
tex
code and one wants the pdf output ?

For example, this does not work:

---
title: "Untitled"
output:
pdf_document:
keep_tex: yes
---

Hello !

```
\begin{verbatim}
This is verbatim text
\end{verbatim}
```


This generated this error: ! LaTeX Error: \begin{document} ended by \end{verbatim}.

Of course, I can do:

```{r, eval=FALSE}
\begin{verbatim}
This is verbatim text
\end{verbatim}
```


But the problem when doing that, is that the code block in the output can be highlighted by the R highlighting colors (not for the example above, but this could occur in some cases).

Answer

You could use pandoc's verbatim fenced code block syntax:

~~~latex
\begin{verbatim}
This is verbatim text
\end{verbatim}
~~~

Or also:

```latex
\begin{verbatim}
This is verbatim text
\end{verbatim}
```
Comments