Written and maintained by CASRAI Editorial Board
Last updated
A LaTeX listing is a block of program source code typeset exactly as typed. The standard way is the listings package: load it with \usepackage{listings} and put the code between \begin{lstlisting} and \end{lstlisting}. The alternative, minted, hands the code to Pygments for highlighting and needs Python. This guide is for researchers and analysts who must put code in a thesis, article or supplement, and shows the commands, the output controls, the fixes for the usual failures, and a sourced comparison of the two packages (facts read 2026-11-07).
What a LaTeX listing is used for in research
Code in a manuscript, thesis or methods appendix needs a fixed-width, line-preserving rendering that LaTeX does not mangle. According to Overleaf’s Code listing documentation, lstlisting prints the text as typed, keeping line breaks and spaces and ignoring LaTeX commands inside it. Both packages also support captions and cross-references, which matters when a journal or committee asks you to number and refer to code examples. For citing the software itself rather than typesetting it, see how to cite software, code and R packages.
This page also answers the variant searches listing latex, listings latex, lstlisting, LaTeX code block, LaTeX listings Python style and LaTeX minted.
Install or access (official source only)
listings
The package is distributed on CTAN as listings, described as typesetting program source code. CTAN lists version 1.11b dated 2025-11-14 under the LaTeX Project Public License 1.3c (read 2026-11-07, ctan.org/pkg/listings). Its manual is at mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf. In the Overleaf editor no installation is needed; add the package to the preamble.
minted
CTAN lists minted at version 3.8.0 dated 2026-03-03, Pygments-based, licensed under LPPL 1.3c, LPPL 1.3 or the 3-clause BSD licence (ctan.org/pkg/minted, read 2026-11-07). According to the version 3 manual, minted uses the latexminted Python executable, requires Python 3.8 or later on the PATH, and installs with tlmgr install minted on TeX Live or mpm --admin --install=minted on MiKTeX. The manual recommends pip install latexminted for the plugin.
Step-by-step: add a code listing with listings
- Load the package. In the preamble add
\usepackage{listings}. For coloured output also add\usepackage{xcolor}. - Wrap the code. Put it in the environment:
\begin{lstlisting} print("hello") \end{lstlisting} - Set the language (Python style and others). Pass it as an option, and a caption in the same list:
\begin{lstlisting}[language=Python, caption=Python example] ... \end{lstlisting} - Include a code file instead of pasting. Use
\lstinputlisting[language=Octave]{BitXorMatrix.m}. To print only part of the file add line limits, for example\lstinputlisting[language=Octave, firstline=2, lastline=12]{BitXorMatrix.m}(both from the Overleaf documentation; the listings manual shows the samelastlinekey). - Put short code inline.
\lstinline!var i:integer;!typesets a fragment inside a sentence. The manual states that any delimiter character that does not occur in the fragment can be used, and that\lstinline[key=value]{source}sets options for that one fragment. - Define one style for the whole document. Declare it once and activate it:
\lstdefinestyle{mystyle}{ ... } \lstset{style=mystyle} - Add a list of listings if needed.
\lstlistoflistingsgenerates it; the keynololkeeps one listing out of the list.
The same task with minted
Use \begin{minted}{python} … \end{minted}; the language is a required argument and the code is highlighted with Pygments. For a numbered, captioned float use the manual’s listing environment: \begin{listing}[H], the code, \caption{...}, \label{lst:example}, \end{listing} (default placement tbp). Compiling: in TeX Live 2024 and later the -shell-escape flag is no longer required. Older TeX Live needs -shell-escape, and MiKTeX needs -enable-write18; the manual’s example is pdflatex -shell-escape minimal. The manual warns that this option allows arbitrary commands to run, so use it only with documents you trust.
Output and how to read it
The listing appears in a monospaced face with line breaks and spaces preserved. Keys named in the Overleaf documentation control the look, and each can go in \lstdefinestyle or directly in the optional argument:
| Key | What it controls (per Overleaf docs) |
|---|---|
basicstyle |
Base font, for example \ttfamily\small |
numbers, numbersep |
Line numbers on the left, right or none; gap to the code |
captionpos |
Caption at top (t) or bottom (b) |
backgroundcolor, frame |
Background fill and frame around the listing |
keywordstyle, commentstyle, stringstyle |
Appearance of keywords, comments and strings |
breaklines, tabsize, showspaces |
Wrapping of long lines, tab width, visible spaces |
escapeinside |
Characters that let LaTeX commands appear inside the listing |
Troubleshooting
| Symptom | Cause and fix (source: listings manual unless noted) |
|---|---|
| Long lines run off the page | breaklines defaults to false. Set breaklines=true; postbreak, breakindent and breakatwhitespace refine the break. The manual’s example is \lstset{postbreak=space, breakindent=5pt, breaklines}. |
| Accented or non-ASCII characters fail | Characters with codes 128-255 are handled through extendedchars; the manual says to load fontenc, inputenc and/or any other package that defines the characters. |
| Inline code breaks on a delimiter | Choose a delimiter that is absent from the fragment. The manual notes an optional-argument limitation when the fragment itself begins with [. |
| A listing is missing from the list of listings | Check whether the nolol key was set on it. |
| minted fails to compile | Confirm Python 3.8+ is on the PATH and the shell-escape rule for your TeX distribution and version (see above). |
Free alternatives: listings vs minted
Both packages are free to use under open licences; the table compares what each needs. Facts from CTAN and the two manuals, read 2026-11-07.
| listings | minted | |
|---|---|---|
| Current CTAN version | 1.11b (2025-11-14) | 3.8.0 (2026-03-03) |
| Licence | LPPL 1.3c | LPPL 1.3c / LPPL 1.3 / 3-clause BSD |
| Highlighting engine | Built into the package | Pygments, via Python |
| External requirement | None beyond LaTeX | Python 3.8+ on PATH, latexminted |
| Shell escape | Not needed | Not needed in TeX Live 2024+; needed in older TeX Live and in MiKTeX |
| Basic environment | lstlisting |
minted |
If your submission system compiles documents itself, check whether it allows the extra requirements of minted before relying on it; the listings package has none. Related LaTeX guides: LaTeX notes template, indentation in LaTeX, Springer LNCS LaTeX template and Google Docs vs Overleaf vs Word.
FAQ
What is lstlisting in LaTeX?
It is the environment of the listings package that prints program code as typed, preserving line breaks and spaces. Load the package with \usepackage{listings}.
How do I set Python style in a LaTeX listing?
Add the option language=Python: \begin{lstlisting}[language=Python]. With minted, write \begin{minted}{python}.
Do I need -shell-escape for listings?
No. Only minted involves an external program; per its manual, TeX Live 2024 and later no longer need the flag.
How do I add a caption or a list of listings?
Use caption=... in the options and \lstlistoflistings where the list should appear.
Can I include a whole code file?
Yes, with \lstinputlisting{file}, optionally limited by firstline and lastline.
Sources (official documentation)
- Overleaf, Code listing, read 2026-11-07.
- CTAN, listings and its manual, read 2026-11-07.
- CTAN, minted and its manual, read 2026-11-07.







