R code not running/echoing in Latex Enumerate environment

Hi everyone! I was just wondering if anyone knew of a workaround for this problem I'm having. I'm having trouble displaying code properly knitting to PDF. I'm posting some sample code to show you what I mean:

---
title: "Untitled"
author: "Me"
date: "9/20/2019"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown
\begin{enumerate}
\item
This works just fine.
\end{enumerate}

Now the compiler is going to think I'm still writing in \LaTeX \ instead of `R`.
\begin{enumerate}\item
The code doesn't run or display properly.

```{r}
x <- 0
```

Why????
\end{enumerate}

(Edited to add: I'm new here so I thought the post would display my code, but I swear if I insert an R chunk into RMarkdown in an enumerate environment it won't display correctly!)

I figured out that the problem is the fact that I'm writing the code as part of an enumerate item, and I guess RMarkdown doesn't like that? It worked just fine last semester but I've updated RStudio and this is the first time I'm using RMarkdown since. I'm not sure if this is a bug or what, but I'm a stats grad student so I use RMarkdown ALL THE TIME for my homework assignments (which have problem numbers, so I need to be able to use enumerate!)

I know Markdown has a numbering system which I guess I can use if I have to but I really prefer the look of Latex. Can anyone help me out with this?

Thanks!

Hi @Trish! Welcome!

I believe the interpretation of raw LaTeX here is being handled by Pandoc (rmarkdown orchestrates using knitr to record the results of the R code and compile it into Markdown, then Pandoc converts the Markdown into other formats, such as PDF via LaTeX). Specifically, Pandoc's raw_tex extension, whose documentation says:

Note that in LaTeX environments, like

\begin{tabular}{|l|l|}\hline
Age & Frequency \\ \hline
18--25  & 15 \\
26--35  & 33 \\
36--45  & 22 \\ \hline
\end{tabular}

the material between the begin and end tags will be interpreted as raw LaTeX, not as Markdown.

If I'm correct about the conversion chain, this would explain why the code blocks inside your raw TeX environment are not being interpreted correctly. But it doesn't explain why you remember it possibly working before :pensive:. I wonder if something about this behavior has changed in Pandoc? (RStudio bundles Pandoc, so when you updated RStudio you may have wound up with an updated version of Pandoc).

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.