Technical Writing
Source Code Listings
How to display, format, and highlight raw source code in LaTeX documents using listings and minted.
For displaying, formatting, and highlighting raw programming source code in a LaTeX document, there are two primary packages:
listings: The standard, built-in package that requires no external dependencies.minted: A high-fidelity highlighter using the Python library Pygments, supporting over 500 languages.
The listings Package
Load the package in your preamble:
\usepackage{listings}
\usepackage{xcolor} % Optional: to define custom highlight colors
Basic Usage
You can insert code blocks directly into the document using the lstlisting environment:
\begin{lstlisting}[language=Python, caption={Python example}]
# Print Fibonacci sequence
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a + b
fib(1000)
\end{lstlisting}
Loading External Source Files
Instead of copy-pasting code into LaTeX, you can read it directly from raw source files:
\lstinputlisting[language=C++, firstline=10, lastline=30]{src/main.cpp}
Custom Styling
You can configure the appearance globally in the preamble:
\lstset{
backgroundcolor=\color{lightgray},
basicstyle=\footnotesize\ttfamily,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{green!60!black},
stringstyle=\color{red},
numbers=left,
numberstyle=\tiny\color{gray},
frame=single,
breaklines=true,
captionpos=b,
tabsize=4
}
Line Range Selection
Show only specific lines from a file:
\lstinputlisting[language=Python, firstline=10, lastline=25]{script.py}
Escape to LaTeX
Insert LaTeX commands inside code listings using escapeinside:
\lstset{escapeinside={(*@}{@*)}}
\begin{lstlisting}[language=Python]
def hello():
print("Hello") (*@\textcolor{red}{\# fix this}@*)
\end{lstlisting}
Per-Language Styling
\lstdefinestyle{python}{
language=Python,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{green!60!black},
stringstyle=\color{red}
}
\lstdefinestyle{cpp}{
language=C++,
keywordstyle=\color{purple}\bfseries,
commentstyle=\color{gray}
}
\lstinputlisting[style=python]{app.py}
\lstinputlisting[style=cpp]{main.cpp}
The minted Package
minted provides better highlighting than listings but requires Python and the pygments package installed on the system, compiled with shell escape enabled (-shell-escape flag):
\usepackage{minted}
Usage inside document:
\begin{minted}{python}
def hello():
print("Hello World")
\end{minted}
Inline Code
Use `\mintinline{python}{print("hello")}` for inline code.
From Files
\inputminted{python}{script.py}
Custom Styles
\usemintedstyle{monokai} % Pygments style
\setminted{
linenos=true,
fontsize=\small,
frame=single,
breaklines=true
}
Compilation Requirement
minted requires shell escape:
pdflatex -shell-escape filename.tex
Or in latexmk:
latexmk -pdf -shell-escape filename.tex
Comparison: listings vs minted
| Feature | listings | minted |
|---|---|---|
| External dependencies | None | Python + Pygments |
| Language support | ~200 | 500+ |
| Highlighting quality | Good | Excellent |
| Shell escape required | No | Yes |
| Custom styles | Manual | Pygments themes |
| Inline code | \lstinline | \mintinline |
Choosing a Package
- No external tools: Use
listings— works everywhere, no dependencies. - Best highlighting: Use
minted— superior color schemes and language support. - Restricted environments: Use
listings— no shell escape needed.