How to Add Code Listings in LaTeX (listings & minted)
To add syntax-highlighted code in LaTeX, use the listings package or minted (which calls Pygments). Set the language, import files, and caption code blocks.
To add syntax-highlighted code in LaTeX, use the listings package (pure LaTeX, works everywhere) or minted (better highlighting via Pygments, needs -shell-escape). Both let you set the language, import code from a file so it stays in sync, and caption listings like figures. Here's how to use each and which to pick.
1. The listings package (portable default)
\usepackage{listings}
\usepackage{xcolor}
\lstset{
basicstyle=\ttfamily\small,
keywordstyle=\color{blue},
commentstyle=\color{gray},
numbers=left, numberstyle=\tiny,
frame=single, breaklines=true
}
...
\begin{lstlisting}[language=Python, caption=Quicksort]
def quicksort(a):
if len(a) <= 1: return a
p = a[len(a)//2]
return (quicksort([x for x in a if x < p])
+ [x for x in a if x == p]
+ quicksort([x for x in a if x > p]))
\end{lstlisting}
listings is pure LaTeX — no external tools, works on arXiv.
2. Import code from a file
\lstinputlisting[language=Python, firstline=10, lastline=30]{analysis.py}
Pulling the file in keeps the listing synced with your real code — rerun the script and the next compile shows the current version, ideal for reproducible reports.
3. minted (best highlighting)
\usepackage{minted} % compile with -shell-escape
...
\begin{minted}{python}
print("Hello, LaTeX")
\end{minted}
minted calls Pygments for superior highlighting but needs Python + Pygments and the -shell-escape flag.
4. listings vs minted
| | listings | minted |
|---|---|---|
| Setup | None | Python + Pygments |
| Compile flag | None | -shell-escape |
| Highlight quality | Good | Excellent |
| Works on arXiv | Yes | No (no shell-escape) |
| Inline code | \lstinline | \mintinline |
5. Inline code and placement
For a snippet in a sentence, use \lstinline|code| or \mintinline{python}|code|. Put long listings in an appendix and reference key lines in the body. For code on slides, see Beamer.
→ Add highlighted code listings with live preview in LetX.
Written by Shihab Shahriar Antor — AI Engineer & Founder of Shahriar Labs, maker of LetX.
Frequently Asked Questions
Should I use the listings package or minted?
Use listings for portability and zero setup — it's pure LaTeX, works everywhere including arXiv, and highlights most languages adequately. Use minted when you want superior, Pygments-quality highlighting and don't mind the requirements: Python with Pygments installed and the -shell-escape flag enabled when compiling. minted looks noticeably better for many languages, but listings is the safer default for submissions where you can't control the build environment.
How do I include code from an external file?
With listings, use \lstinputlisting[language=Python]{script.py} to pull in the whole file, or add firstline and lastline options to include only a range. With minted, the equivalent is \inputminted{python}{script.py}. Importing the file rather than pasting keeps the listing in sync with your actual code — rerun or edit the script and the next compile shows the current version, which matters for reproducible reports.
Why does minted fail with 'you must invoke LaTeX with -shell-escape'?
minted runs the external Pygments program to highlight code, and LaTeX blocks external program calls unless you pass -shell-escape (or -enable-write18). Add that flag to your compile command, or enable shell-escape in your editor's settings. Because shell-escape has security implications and isn't always available — for example on arXiv — many authors prefer listings, which needs no external calls.