1

How to get, in Latex, this kind of rendering with firstly a text and just after a block of code with a shaded grey background ? I would like to get the same font for code block.

text and code background shaded in grey

Juan Castaño
  • 28,426

1 Answers1

1

Your example shows a fixed font for the code. Using package ffcode might be one way.

\documentclass{article}
\usepackage{ffcode}

\begin{document} \begin{ffcode} set.seed(123)

do grid

y <- rcoga(100000, c(2, 5, 7), c(3, 2, 4)) grid <- seq(0,15,length.out=100)

calculate pdf and cdf

\end{ffcode} \end{document}

1

With package minted though you can have several themes with colored characters. Note that I have also customized a background color using the xcolor package for the vim theme as the characters weren't easily visible.

\documentclass{article}
\usepackage{minted}
\usepackage{xcolor}
\colorlet{myblack}{black!50!white}

\begin{document} \section{Minted styles with non-italic comments} \subsection{Light theme} \subsubsection*{\texttt{perldoc}} \usemintedstyle{perldoc} \begin{minted}[linenos]{r} set.seed(123)

do grid

y <- rcoga(100000, c(2, 5, 7), c(3, 2, 4)) grid <- seq(0,15,length.out=100)

calculate pdf and cdf

\end{minted} \subsubsection*{\texttt{rrt}} \usemintedstyle{rrt} \begin{minted}[linenos]{r} set.seed(123)

do grid

y <- rcoga(100000, c(2, 5, 7), c(3, 2, 4)) grid <- seq(0,15,length.out=100)

calculate pdf and cdf

\end{minted} \subsubsection*{\texttt{pastie}} \usemintedstyle{colorful} \begin{minted}[linenos]{r} set.seed(123)

do grid

y <- rcoga(100000, c(2, 5, 7), c(3, 2, 4)) grid <- seq(0,15,length.out=100)

calculate pdf and cdf

\end{minted} \subsubsection*{\texttt{colorful}} \usemintedstyle{colorful} \begin{minted}[linenos]{r} set.seed(123)

do grid

y <- rcoga(100000, c(2, 5, 7), c(3, 2, 4)) grid <- seq(0,15,length.out=100)

calculate pdf and cdf

\end{minted} \subsubsection*{\texttt{vs}} \usemintedstyle{vs} \begin{minted}[linenos]{r} set.seed(123)

do grid

y <- rcoga(100000, c(2, 5, 7), c(3, 2, 4)) grid <- seq(0,15,length.out=100)

calculate pdf and cdf

\end{minted} \subsection{Dark theme} \subsubsection{\texttt{vim}} \usemintedstyle{vim} \begin{minted}[bgcolor=myblack,linenos]{r} set.seed(123)

do grid

y <- rcoga(100000, c(2, 5, 7), c(3, 2, 4)) grid <- seq(0,15,length.out=100)

calculate pdf and cdf

\end{minted} \subsubsection*{\texttt{monokai}} \usemintedstyle{monokai} \begin{minted}[bgcolor=black,linenos]{r} set.seed(123)

do grid

y <- rcoga(100000, c(2, 5, 7), c(3, 2, 4)) grid <- seq(0,15,length.out=100)

calculate pdf and cdf

\end{minted}

2 3


Note: For using any of these packages; you need to install python3-pygments package and invoke --shell-escape flag in the terminal as follows.

pdflatex --shell-escape filename.tex
Niranjan
  • 3,435
  • Thanks a lot ! is it possible to have colorified code with ffcode ? –  Sep 03 '21 at 11:10
  • ffcode seems to be a strict package, but minted itself is quite flexible. I realized it after you raised this question. There are plenty of styles with which you can do whatever you want. It is also possible to customize these styles if you are super enthu! See my edit for seeing some options you might find interesting. – Niranjan Sep 03 '21 at 14:25
  • 1
    Perfect, thanks for your help. –  Sep 03 '21 at 14:30
  • What would you write if you wanted all text to be black inside of the code-block (no syntax highlighting. We don't want \usemintedstyle{perldoc} or \usemintedstyle{colorful} – IdleCustard Jun 07 '23 at 15:51
  • @SamuelMuldoon what about the example with ffcode package? – Niranjan Jun 07 '23 at 17:36
  • Also, it highly depends on your purpose. These packages provide a way to typeset code in simple LaTeX documents, but if you want to go slightly far with LaTeX and experience the new advancements, l3doc class is the one you need. It provides a lot of amazing features, but requires a different style of writing. Personally I find it absolutely worth it. If you want to see how it looks, you can have a look at the documentation of my packages, e.g., gfdl. – Niranjan Jun 07 '23 at 17:47