LaTeX Code Listings: Display Programming Code Beautifully

Date Published

LaTeX Code Listings: Display Programming Code Beautifully

Code Listings in LaTeX

Display programming code beautifully in your LaTeX documents with syntax highlighting.

Quick answer: How do you write inline code in LaTeX? Use \texttt{name} for monospaced text with no package (escape _ % & # $ as \_ \% and so on), \verb|x_1 & $y$| to print any characters exactly, \lstinline|print("hi")| from the listings package for highlighted inline code, or \mintinline{python}{print("hi")} from minted (needs -shell-escape). For code blocks use the verbatim, lstlisting or minted environments.

Inline Code in LaTeX

Command

Package

Special characters

Highlighting

Inside \section or \footnote

\texttt{...}

none

Must be escaped (\_ \% \&)

No

Yes

\verb|...|

none

Printed as typed

No

No

\lstinline|...|

listings

Printed as typed

Yes

Usually no

\mintinline{lang}{...}

minted

Printed as typed

Yes (Pygments)

Usually no

Any delimiter that does not appear in the code works for \verb and \lstinline: \verb+a|b+ prints a|b. This document compiles as written:

1\documentclass{article}
2\usepackage{listings} % \lstinline, lstlisting
3\lstset{basicstyle=\ttfamily, language=Python}
4\begin{document}
5Call \texttt{np.mean()} on the array and store it in \texttt{x\_mean}.
6
7Print characters exactly with \verb|x_1 & $y$ % 100|.
8
9Highlighted inline code: \lstinline|print("hello")|.
10\end{document}

Try this in Octree — free LaTeX editor with AI, compiles in your browser

Code Block Snippets

A listings block with colors, a plain verbatim block, and (separately, because it needs shell escape) a minted block:

1\documentclass{article}
2\usepackage{listings} % lstlisting
3\usepackage{xcolor} % \color in \lstset
4\lstset{basicstyle=\ttfamily\small, keywordstyle=\color{blue},
5 commentstyle=\color{gray}, stringstyle=\color{teal},
6 numbers=left, breaklines=true, frame=single}
7\begin{document}
8\begin{lstlisting}[language=Python]
9def greet(name):
10 # say hello
11 print(f"Hello, {name}!")
12\end{lstlisting}
13
14\begin{verbatim}
15verbatim is built in: $ % & _ { } are printed as typed
16\end{verbatim}
17\end{document}
1% compile with: pdflatex -shell-escape file.tex (needs Python Pygments)
2\documentclass{article}
3\usepackage{minted} % minted, \mintinline
4\begin{document}
5Inline: \mintinline{python}{print("hello")}.
6\begin{minted}[linenos, frame=lines]{python}
7def greet(name):
8 print(f"Hello, {name}!")
9\end{minted}
10\end{document}

minted runs the external Pygments highlighter, so it only works where shell escape is enabled. If your editor or compile service does not allow it, listings gives similar results with no external tools.

The listings Package

The most common solution for code in LaTeX:

\usepackage{listings}

Basic Usage

\begin{lstlisting} def hello(): print("Hello, World!") \end{lstlisting}

Specify Language

\begin{lstlisting}[language=Python] import numpy as np x = np.array([1, 2, 3]) \end{lstlisting}

Inline Code

\lstinline|print("hello")|

Supported Languages

The listings package supports 100+ languages:

- Python, Java, C, C++ - JavaScript, TypeScript - HTML, CSS - SQL, Bash - And many more

Customizing Appearance

Global Settings

\lstset{ basicstyle=\ttfamily\small, keywordstyle=\color{blue}, commentstyle=\color{green}, stringstyle=\color{red}, numbers=left, numberstyle=\tiny, frame=single, breaklines=true }

Key Options

- basicstyle — font for code - keywordstyle — keywords formatting - commentstyle — comments formatting - stringstyle — strings formatting - numbers — line numbers (left, right, none) - frame — border (none, single, shadowbox) - breaklines — wrap long lines - tabsize — spaces per tab - showstringspaces — show spaces in strings

The minted Package

More powerful syntax highlighting using Pygments:

\usepackage{minted}

Basic Usage

\begin{minted}{python} def factorial(n): return 1 if n <= 1 else n * factorial(n-1) \end{minted}

Inline Code

\mintinline{python}{print("hello")}

Minted Options

\begin{minted}[linenos, frame=lines, fontsize=\small]{python}

Note: minted requires Python and Pygments installed, and compilation with -shell-escape.

Verbatim Environment

For simple, unformatted code:

\begin{verbatim} This is verbatim text. All characters are literal. \end{verbatim}

Inline Verbatim

\verb|special chars: $#%|

Including External Files

With listings

\lstinputlisting[language=Python]{script.py}

With minted

\inputminted{python}{script.py}

Algorithm Pseudocode

For pseudocode (not real code), use algorithm2e or algorithmicx packages instead.

Styling Tips

Choose Readable Fonts

\usepackage{inconsolata} — nice monospace font

Color Schemes

Popular combinations: - Dark keywords, gray comments - Blue keywords, green strings - Match your IDE theme

Consistent Sizing

Use \small or \footnotesize for long code to fit page width.

Common Issues

Problem: Special characters break Solution: Use mathescape=true and escape with $...$

Problem: Long lines overflow Solution: Set breaklines=true

Problem: Wrong language detection Solution: Always specify language=... explicitly

Inline Code in LaTeX FAQ

How do you write inline code in LaTeX?

For a short name, \texttt{name} is enough. For code with special characters use \verb|code|, and for syntax-highlighted inline code use \lstinline|code| (listings) or \mintinline{python}{code} (minted).

Why does \verb not work inside \section or \footnote?

\verb changes how LaTeX reads characters, which is impossible once text has already been read as the argument of another command. Use \texttt{} with escaped characters there, or save the text first with \SaveVerb from the fancyvrb package.

What is the difference between listings and minted?

listings is pure LaTeX: it works everywhere but its highlighting is simpler. minted calls Python's Pygments for more accurate, prettier highlighting but needs Pygments installed and compiling with -shell-escape.

How do I write an underscore in \texttt?

Escape it as \_, for example \texttt{my\_var}. The same applies to \%, \&, \# and \$. To avoid escaping, use \verb|my_var| instead.

Display Code with Octree

Octree handles code beautifully:

- Easy listings/minted setup - Preview syntax highlighting - AI helps with formatting - Export polished PDFs

Try it at https://useoctree.com

Try this in Octree — free LaTeX editor with AI, compiles in your browser