The minted package
Highlights source code with the Python library Pygments: the minted environment, \mintinline and \inputminted. It needs a compile run with shell escape.
This package runs an external program and needs -shell-escape. The editor compiles without shell escape, so the package does not work there.
Loading the package
\usepackage{minted}minted hands every piece of code to Pygments, a highlighter with lexers for several hundred languages, and reads back the formatted result. The highlighting is more exact than that of listings, and languages such as JavaScript or Rust work without a definition of your own.
The price is an external program. Python and Pygments have to be installed, and LaTeX has to be started with -shell-escape, because the package calls pygmentize during the run. Without the flag, version 2 stops with Package minted Error: You must invoke LaTeX with the -shell-escape flag.
Shell escape allows a document to run any program on the computer. TeX Live therefore leaves it off unless you ask for it, and some compile environments do not offer the setting at all; there minted cannot be used. listings is the alternative that needs no external program.
Version 3 from 2024 replaces the call of pygmentize by its own Python program, latexminted, which TeX Live 2024 and later run in the restricted shell mode without the flag. With -no-shell-escape, neither version works.
Main commands
\begin{minted}{python} ... \end{minted} | A block of code; the argument is the Pygments name of the language. |
\mintinline{python}{sum(values)} | Highlighted code inside a line of text. |
\inputminted[firstline=10, lastline=25]{c}{main.c} | Reads the code from a file, here only lines 10 to 25. |
\setminted{linenos, breaklines, fontsize=\small} | Settings for all following code: line numbers, automatic line breaks, font size. |
\usemintedstyle{friendly} | Chooses a colour scheme of Pygments. |
\begin{minted}[frame=lines, highlightlines={2}]{python} | Options for one block: rules above and below, line 2 with a coloured background. |
\begin{listing} ... \end{listing} | A float for code with \caption and \label; \listoflistings prints the list. |
Complete example
\documentclass{article}
\usepackage{minted}
\setminted{fontsize=\small, linenos, breaklines}
\begin{document}
\begin{listing}[ht]
\begin{minted}{python}
def total(values):
# add all values
return sum(values)
\end{minted}
\caption{Sum of a list}
\label{lst:total}
\end{listing}
Listing~\ref{lst:total} calls \mintinline{python}{sum(values)}.
\end{document}This example needs shell escape and was not compiled for this page.
Pitfalls
- The package stores the highlighted code in a folder named
_minted-plus the name of the main file, next to the document. Keep it out of version control. - In
beamer, a frame that containsmintedcode needs the optionfragile. - When LaTeX writes its files to another folder (
-output-directory), version 2 needs the same folder in the package optionoutputdir.
Related pages
- Package
listings - How-to Insert source code in LaTeX
- How-to Inline code in LaTeX
- Package
fancyvrb