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.
Best Answer
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}
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}
Note: For using any of these packages; you need to
install python3-pygments package and invoke
--shell-escape flag in the terminal as follows.
The listings package provides a command for inline code snippets:
\documentclass{article}
\usepackage{listings}
\lstset{language = C++}
\begin{document}
In order to do this just run the
\lstinline!#include <iostream>! command
command in your terminal.
\end{document}
Instead of the exclamation mark you could use any character as delimiter, as long as it does not appear in the code snippet.
Here is an option with fancyvrb where you can change the formatting of specific lines; the code should be self-explanatory:
\documentclass{article}
\usepackage{fancyvrb,xcolor}
\begin{document}
Write this:
\begin{Verbatim}
function doit() {
console.log("Hello");
}
\end{Verbatim}
And then add these lines:
\renewcommand{\FancyVerbFormatLine}[1]{%
\ifnum\value{FancyVerbLine}=2
\textcolor{red}{#1}% Format specific line
\else
#1% Else, do nothing different
\fi
}
\begin{Verbatim}
function doit() {
var s = "Hello";
console.log(s);
}
\end{Verbatim}
\renewcommand{\FancyVerbFormatLine}{}% Clear how lines are formatted differently
\begin{Verbatim}
function doit() {
var s = "Hello";
console.log(s);
}
\end{Verbatim}
\end{document}
It should work for elementary setups and small code formatting.
Best Answer
Your example shows a fixed font for the code. Using package
ffcode
might be one way.With package
minted
though you can have several themes with colored characters. Note that I have also customized a background color using thexcolor
package for thevim
theme as the characters weren't easily visible.Note: For using any of these packages; you need to install
python3-pygments
package and invoke--shell-escape
flag in the terminal as follows.