2

When I attempt to do this using this code:

---
title: "Title"
author: "Me"
date: "31 October 2014"
output: 
    pdf_document:
        includes:
            in_header: includes.sty
---

\begin{enumerate}[label=\textbf{\arabic*})]
    \item 
    \begin{enumerate} 
        \item
        ```{r}
            print("test")
        ```


    \end{enumerate}
\end{enumerate}

includes.sty just contains:

\usepackage{enumitem}

R studio does not recognise the chunk as a valid code block (no syntax highlighting), and when compiling I get the error:

! You can't use `macro parameter character #' in horizontal mode.
l.199         #

Is there any other way to do this?

  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Nov 1 '14 at 12:15
  • Okay, I've added the full file. Adding \documentclass{...} isn't necessary in this instance (as I'm guessing that it is automatically included when the source is compiled). – JackWoot Nov 1 '14 at 12:25
3

There are basically two things causing errors in your sample code:

  1. By default, knitr prefixes R output with ##, which can't be present in your TeX file. You have to substitute something else for it.
  2. Code chunks can't be indented, so you'll have to excuse your OCD self and flush the chunk to the left of your source code.

So this alone makes improves your code:

\begin{enumerate}
    \item 
    \begin{enumerate} 
        \item
```{r, comment="", results="asis"}
print("test")
```
    \end{enumerate}
\end{enumerate}

Your PDF should look like this:

asis

Notice how the final product is a monstrous TeX-knitr hybrid. It interpreted the R markdown, but printed everything in LaTeX. Notice how the triple quotes were converted to double quotes + single quote. Removing results="asis" just makes things a bit worse, because then the answer also gets those quotes.

markup

I've found an awesome answer on Stackoverflow that shows a nice trick to get proper formatting. You just have to rename your environment:

---
output: pdf_document
header-includes:
- \newcommand{\benum}{\begin{enumerate}}
- \newcommand{\eenum}{\end{enumerate}}
---
\benum
  \item 
    \benum
      \item
```{r, results="asis"}
print("test")
```
    \eenum
\eenum

Result:

newcommand

Removing results="asis" should give better (i.e., monospaced) formatting for the R output, but instead it just yields the following error on my machine:

LaTeX Error: \begin{enumerate} on input line 664 ended by \end{document}.


TL;DR

  1. Renew your commands on the preamble (e.g.: \newcommand{\benum}{\begin{enumerate}})
  2. Don't indent your chunk
  3. Use results="asis"
  • This is a brilliant answer, thanks a lot for this. Never would have worked it out myself! – JackWoot May 23 '15 at 17:23
  • @JackWoot: thanks! Please remember to vote it up. :) – Waldir Leoncio May 24 '15 at 14:05
  • Not enough reputation unfortunately, but I selected it as best answer. I'll be back to do so once I do. – JackWoot May 24 '15 at 21:41
  • I don't normally respond to these; but, thank you. This worked beautifully for my issue as well. I was trying to put my code in a column environment and ended up using the following based upon the answer above: \usepackage{multicol} \newcommand{\btwocol}{\begin{multicols}{2}} \newcommand{\etwocol}{\end{multicols}} – Alison Shelton Sep 18 '16 at 14:16
  • @AlisonShelton, I'm glad to be of assistance. In order to help the community, please remember to upvote answers you find useful. – Waldir Leoncio Sep 19 '16 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.