minted
Source-code listings with Pygments-powered syntax highlighting (requires shell-escape and Python).
- Example
- Preamble
% After a period, LaTeX sets the wider sentence space by default. The exception: a period after a capital letter
% is taken for an abbreviation or initial ("T. Scientist", "NASA") and gets the narrow inter-word space.
% "XML." ends a sentence, so \@ before the period restores the sentence space.
% (The other way round, "e.g.\ details" forces the narrow space after a lowercase abbreviation; ~ is a
% non-breaking inter-word space, as in "Prof.~Dr.~Eike Kraft".)
% An abbreviation set with \gls{xml} needs no \@: glossaries ends it with a non-letter, so the period after it
% gets the sentence space. The plain "XML" here is for documents without the glossaries package.
\Zcref{lst:XML} shows source code written in XML\@.
\Zcref{line:comment} contains a comment.
\begin{listing}[htbp]
\begin{minted}[linenos=true,escapeinside=||]{xml}
<listing name="example">
<!-- comment --> |\label{line:comment}|
<content>not interesting</content>
</listing>
\end{minted}
\caption{Example XML listing using minted}
\label{lst:XML}
\end{listing}
\begin{listing}[htbp]
\begin{minted}[linenos=true,escapeinside=||]{json}
{
key: "value"
}
\end{minted}
\caption{Example JSON listing using minted}
\label{lst:flJSON}
\end{listing}
\begin{listing}[htbp]
\begin{minted}[linenos=true,escapeinside=||]{java}
public class Hello {
public static void main (String[] args) {
System.out.println("Hello World!");
}
}
\end{minted}
\caption{Java code rendered using minted}
\label{lst:flJava}
\end{listing}
% See https://github.com/gpoore/minted
\usepackage[newfloat]{minted}
\setminted{
% Same size as the listings package uses (basicstyle)
fontsize=\footnotesize,
% Line numbers not flowing out of the margin
numbersep=5pt,
xleftmargin=12pt,
%
% Better listing breaking
breakafter=-/\{,
breakbefore=\\
%
% Alternative: Rely on pygment's tokenizer. Does not work well with LaTeX and comments
% breakbytoken=true,
% breakbytokenanywhere=true
}
%\usemintedstyle{bw} %black and white style
%\usemintedstyle{vs} %visual studio
\usemintedstyle{friendly_grayscale}
% Line numbers are not copied along with the code (in PDF viewers that support ActualText)
\usepackage{accsupp}
% robust, as \refstepcounter expands \theFancyVerbLine for \label
\DeclareRobustCommand{\mintednocopy}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
\renewcommand{\theFancyVerbLine}{\mintednocopy{\rmfamily\tiny\arabic{FancyVerbLine}}}
%\usemintedstyle{friendly}
%\usemintedstyle{eclipse} %http://www.jevon.org/wiki/Eclipse_Pygments_Style
%\usemintedstyle{autumn}
%\usemintedstyle{rrt}
%\usemintedstyle{borland}
% A paper keeps the caption style of its document class: the caption package would change it (e.g., "Figure 1:" instead of IEEE's "Fig. 1.")
\SetupFloatingEnvironment{listing}{name=List.,within=none}
% When using both minted and listings
% Compatibility of packages minted and listings with respect to the numbering of "List." caption
% Source: https://tex.stackexchange.com/a/269510/9075
% \AtBeginEnvironment{listing}{\setcounter{listing}{\value{lstlisting}}}
% \AtEndEnvironment{listing}{\stepcounter{lstlisting}}
% Enable \label in listing lines (via escapeinside) and referencing them using \zcref, e.g., "Line 2".
% fancyvrb steps the line counter without \refstepcounter: no counter for \zcref and no target for the hyperlink.
% See https://github.com/gusbrs/zref-clever/issues/65 and https://tex.stackexchange.com/q/132420/9075.
\makeatletter
\def\FV@refstepcounter#1{%
\refstepcounter{#1}%
\protected@edef\@currentlabel{\csname p@#1\endcsname\arabic{#1}}%
}
% Unique hyperlink targets in each listing
\newcounter{FancyVerbListing}
\let\FV@StepLineNo@orig\FV@StepLineNo
\def\FV@StepLineNo{\stepcounter{FancyVerbListing}\FV@StepLineNo@orig}
\def\theHFancyVerbLine{\arabic{FancyVerbListing}.\arabic{FancyVerbLine}}
\makeatother