1

I'm trying to highlight my R code using minted package. However, after %, a few lines didn't get highlighted.

When I delete % problem disappears.

My code:

\documentclass{article}
\usepackage{minted}

\begin{document}

\begin{minted}{R} > quantile(x_2, 0.75) 75% 270.745 > quantile(x_2, 0.25) 25% 105.35 > quantile(x_2, 0.75) - quantile(x_2, 0.25) 75% 165.395 \end{minted}

\begin{minted}{R} > quantile(x_2, 0.75) 75 270.745 > quantile(x_2, 0.25) 25 105.35 > quantile(x_2, 0.75) - quantile(x_2, 0.25) 75% 165.395 \end{minted}

\end{document}

enter image description here

  • Welcome to TeX.SE! – Mensch Mar 04 '23 at 16:01
  • Maybe you want to use \begin{minted}[escapeinside=||]{R} and then replace all % by |\%|. This will give percent signs in black, though, but you can use \textcolor to achieve the correct color. – Οὖτις Mar 05 '23 at 08:21

1 Answers1

2

You could also preprocess the code with Knitr, see How to build Knitr document from the command line for example. Code:

yourfile.Rnw

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\section{Quantiles}
<<quantile-examples, eval=TRUE>>=
x_2 <- c(0,105.35,150,270.745,300)
quantile(x_2, 0.75)
quantile(x_2, 0.25)
quantile(x_2, 0.75) - quantile(x_2, 0.25)
@
See the \textcolor{blue}{colored} examples above.
\end{document}

Compile in the terminal with:

Rscript -e "library(knitr); knit('yourfile.Rnw')"

This will produce a file yourfile.tex. After running LaTeX on that file the pdf looks as follows:

enter image description here

Marijn
  • 37,699