LaTeX Code Listings
listings for portable code blocks, minted for prettier highlighting, verbatim for raw text. Pick by what you need.
The decision matrix
| Package | Setup | Quality |
|---|---|---|
| verbatim | Built-in | Ugly, no highlighting |
| listings | Pure LaTeX | Good with config |
| minted | Needs Python + Pygments + shell-escape | Excellent |
listings (most common)
\usepackage{listings}
\usepackage{xcolor}
\lstset{
basicstyle=\ttfamily\small,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{gray}\itshape,
stringstyle=\color{red},
frame=single,
numbers=left,
numberstyle=\tiny\color{gray},
breaklines=true,
showstringspaces=false,
language=Python
}
\begin{lstlisting}[caption=Hello world, label=lst:hello]
def greet(name):
"""Print a greeting."""
print(f"Hello, {name}!")
greet("World")
\end{lstlisting}
% Inline: \lstinline|x = 1|
% From file:
\lstinputlisting[language=Python]{example.py}minted (best looking)
% Compile with: pdflatex -shell-escape main.tex
% Requires: pip install Pygments
\usepackage{minted}
\begin{minted}[linenos, frame=single, fontsize=\small]{python}
def greet(name):
"""Print a greeting."""
print(f"Hello, {name}!")
greet("World")
\end{minted}
% Inline: \mintinline{python}{x = 1}
% From file:
\inputminted{python}{example.py}
% With caption (use 'listing' float)
\begin{listing}
\begin{minted}{python}
def example(): pass
\end{minted}
\caption{Example function.}
\label{lst:example}
\end{listing}verbatim (built-in fallback)
\begin{verbatim}
def greet(name):
print(f"Hello, {name}!")
\end{verbatim}
% Inline:
\verb|x = 1|Common languages and their listings names
| Language | listings | minted |
|---|---|---|
| Python | Python | python |
| JavaScript | JavaScript | javascript |
| C++ | C++ | cpp |
| C | C | c |
| Rust | — (not built-in) | rust |
| Go | — (not built-in) | go |
| Bash | bash | bash |
| SQL | SQL | sql |
```python
def greet(name):
print(f"Hello, {name}!")
```
// Inline: `x = 1`Markdown-style triple backticks with language hint. Built-in syntax highlighting, no shell-escape, no Python install. Try TypeTeX free.
Try TypeTeX freeFrequently Asked Questions
Three options: (1) verbatim — built-in but ugly, no syntax highlighting; (2) listings — most common, customizable, no external dependencies; (3) minted — best-looking, uses Pygments for syntax highlighting but requires shell-escape and Python. For most papers, listings is the right choice.
listings is pure LaTeX — works on any TeX install, no external tools. minted requires shell-escape mode (-shell-escape compile flag) and Python with Pygments installed, but produces dramatically prettier syntax highlighting with hundreds of supported languages. Use listings for portability, minted for visual quality.
\usepackage{listings} \lstset{basicstyle=\ttfamily\small, keywordstyle=\color{blue}\bfseries, commentstyle=\color{gray}, language=Python, frame=single}. Then wrap code in \begin{lstlisting} ... \end{lstlisting}. For inline code: \lstinline|x = 1|.
Install Python + Pygments first (pip install Pygments). \usepackage{minted}. Compile with -shell-escape: pdflatex -shell-escape main.tex. Then \begin{minted}{python} ... \end{minted} for blocks, \mintinline{python}{x = 1} for inline. minted supports 500+ languages.
listings: \lstset{numbers=left, numberstyle=\tiny\color{gray}}. minted: \begin{minted}[linenos]{python} ... \end{minted}. Both support starting at a specific line, stepping, and styling the numbers. Useful for referencing specific lines in your text.
listings: \begin{lstlisting}[caption=Your caption, label=lst:name] ... \end{lstlisting}. minted: wrap in \begin{listing} ... \caption{...} \label{lst:name} \end{listing} (note the singular 'listing' float environment). Then reference with \ref or \autoref.
listings: \lstinputlisting[language=Python]{file.py}. minted: \inputminted{python}{file.py}. Both read the entire file into a code block — useful for keeping sample code in real .py/.js files that can be tested separately.
Three options: \verb|x = 1| (built-in, can't be in arguments), \lstinline|x = 1| (listings, more flexible), \mintinline{python}{x = 1} (minted, with highlighting). For simple monospace without code semantics, just \texttt{code}.
verbatim and listings handle most special chars correctly, but # and ^ in language modes can confuse listings. Set \lstset{escapechar=@} and use @\textbf{}@ to insert LaTeX commands. minted handles special characters natively because it pre-processes through Python.