Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical Supply Distributor100,000+ medical supplies. 48-hour critical dispatch.Ships from 8 regional U.S. hubs. Net-30 terms for verified institutional accounts.Shop lac.us CodeCASRAIlac.us

LaTeX Listing: lstlisting, lstinputlisting and minted

How to add a LaTeX listing with lstlisting, lstinputlisting or minted: commands, line breaking and non-ASCII fixes, and a sourced listings vs minted table.

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

  1. Load the package. In the preamble add \usepackage{listings}. For coloured output also add \usepackage{xcolor}.
  2. Wrap the code. Put it in the environment:
    \begin{lstlisting}
    print("hello")
    \end{lstlisting}
  3. 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}
  4. 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 same lastline key).
  5. 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.
  6. Define one style for the whole document. Declare it once and activate it:
    \lstdefinestyle{mystyle}{ ... }
    \lstset{style=mystyle}
  7. Add a list of listings if needed. \lstlistoflistings generates it; the key nolol keeps 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)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about LaTeX Listing: lstlisting, lstinputlisting and minted

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →