R Markdown and Latex preamble; how do I get non-evaluated code blocks and evaluated code blocks to look the same?

19 Views Asked by At

I have a new R package I'm developing for a set of R Markdown templates [among other things] to get all our analytics departments aligned with the same style for reports. I've created a new preamble.tex file and have successfully gotten it to apply to the knitted document. My problem comes with code blocks and, what I assume to be, the verbatim environment.

Code blocks which are not evaluated, or for which no language has been specified, correctly show line numbering as defined in the preamble below. Code blocks which are evaluated, however, do not have the line numbers displayed.

Current output with different formats for evaluated and non-evaluated code chunks

My question is, then, why? And what do I need to do to my preamble to get both evaluated [and, therefore highlighted] code and non-evaluated code to have the line numbers included? I also notice that the shaded environment for evaluated code doesn't have the same before and after spacing as the non-evaluated code, and I have no idea why.

To be clear: I wouldn't even consider myself a newbie at latex. I have no idea what I'm actually doing, but it's mostly been working.

The relevant parts of my preamble.tex are included below, along with a MWE .Rmd document.

Latex Preamble:

% Load required LaTeX packages
\usepackage{xcolor} % Color and color definitions
\usepackage{framed} % Package for block quote styling
\usepackage{fancyvrb}   % Package for code block styling

% Define Color Palette
\definecolor{mpsyellow}{RGB}{253,184,19}
\definecolor{mpswine}{RGB}{133,12,112}
\definecolor{mpsdarkblue}{RGB}{0,101,164}

% Block Quotes Defined
\let\oldquote=\quote
\let\endoldquote=\endquote
\renewenvironment{quote}{
    \colorlet{shadecolor}{mpswine!10}
    \begin{shaded*}
    \begin{oldquote}\itshape%
}{%
    \end{oldquote}%
    \end{shaded*}%
}

% Code Block Quotes Defined
\renewenvironment{verbatim}{%
    \VerbatimEnvironment
    \colorlet{shadecolor}{mpsyellow!5}
    \begin{shaded*}
    \begin{Verbatim}[numbers=left,xleftmargin=5mm]%
}{%
    \end{Verbatim}%
    \end{shaded*}%
}

R Markdown:

---
title: "Untitled"
author: "author"
date: 'r Sys.Date()'
output: 
  pdf_document:
    latex_engine: lualatex
header-includes:
  - \input{preamble.tex}
---

## R Code Chunk:
```{r demo_chunk, echo=TRUE}
summary(mtcars$mpg)
```

## Block Quote:

> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed tincidunt 
> urna. Sed scelerisque sit amet nunc et maximus. Maecenas et posuere quam. 
> Pellentesque aliquam volutpat auctor. Duis non neque luctus felis varius 
> vehicula at quis mi. Etiam sed sem neque. Nam odio nulla, suscipit eu erat non, 
> feugiat posuere massa.

## Non-evaluated code blocks:
```
summary(mtcars$mpg)
```

## Inline Code
You can use `scale_color_mps()` with your plots now!
0

There are 0 best solutions below