LaTeX Guide

LaTeX Code Listings

listings for portable code blocks, minted for prettier highlighting, verbatim for raw text. Pick by what you need.

The decision matrix

PackageSetupQuality
verbatimBuilt-inUgly, no highlighting
listingsPure LaTeXGood with config
mintedNeeds Python + Pygments + shell-escapeExcellent

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

Languagelistingsminted
PythonPythonpython
JavaScriptJavaScriptjavascript
C++C++cpp
CCc
Rust— (not built-in)rust
Go— (not built-in)go
Bashbashbash
SQLSQLsql
Typst code blocks: just use backticks
```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 free

Frequently Asked Questions

How do I include code in a LaTeX document?

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.

What's the difference between listings and minted?

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.

How do I use the listings package?

\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|.

How do I use minted?

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.

How do I add line numbers to code in LaTeX?

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.

How do I caption a code listing?

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.

How do I include code from an external file?

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.

How do I show inline code (single word) in LaTeX?

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}.

Why does my listings code break with special characters?

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.

More LaTeX guides