\usepackage{listings}
The listings package is the standard way to typeset source code in LaTeX. It provides syntax highlighting for 80+ languages, line numbers, custom frames, background colours, and named styles — all without leaving your document.
The example below shows a minimal but complete setup: load the package, configure it globally with \lstset{…}, then place code inside a lstlisting environment with a language and caption.
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
% Global listings configuration
\lstset{
basicstyle=\ttfamily\small,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{gray}\itshape,
stringstyle=\color{orange},
breaklines=true,
showstringspaces=false,
frame=single,
rulecolor=\color{black!20},
tabsize=2,
}
\begin{document}
\begin{lstlisting}[language=Python, caption={Hello World in Python}]
def greet(name: str) -> None:
# Print a personalised greeting
message = f"Hello, {name}!"
print(message)
greet("World")
\end{lstlisting}
\end{document}Add \usepackage{listings} and optionally \usepackage{xcolor} to your preamble. Wrap code in a \begin{lstlisting}…\end{lstlisting} environment.
Specify the language with the language= option: Python, C++, Java, Bash, SQL, HTML, and 80+ others are built in and apply keyword colouring automatically.
Use caption={…} to add a numbered caption below the listing, and label={lst:name} for cross-references with \ref{lst:name} anywhere in your document.
\lstset{} applies settings globally to all listings. Place it in your preamble after \usepackage{listings} to avoid repeating options on every environment.
Define a named style with \lstdefinestyle{name}{…} and activate it globally with \lstset{style=name}. The example below creates custom colours for background, comments, keywords, and strings, then demonstrates them with a C++ binary search implementation.
The frame=tb option draws a horizontal rule above and below the listing, while numbers=left adds line numbers in the left margin.
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
% Custom colour definitions
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{magenta}\bfseries,
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2,
frame=tb,
}
\lstset{style=mystyle}
\begin{document}
\begin{lstlisting}[language=C++, caption={Binary search}, label={lst:binsearch}]
#include <iostream>
using namespace std;
int binarySearch(int arr[], int n, int target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1; // not found
}
\end{lstlisting}
\end{document}Use backgroundcolor=\color{name} inside \lstdefinestyle{} to apply a tinted background. Define custom colours first with \definecolor{name}{rgb}{r,g,b}.
Enable with numbers=left (or right). Control the font size and colour with numberstyle=\tiny\color{gray} and adjust the gutter width via numbersep=5pt.
The frame= option draws borders: single (full box), tb (top and bottom rules only), lines (top and bottom lines), or none. Combine with rulecolor= for styled rules.
listings ships with built-in keyword sets for over 80 programming and markup languages. Specify one with language= on the environment or globally in \lstset{}.
Not listed? Define your own with \lstdefinelanguage — see the section below.
When your language is not built in, use \lstdefinelanguage{name}{…} to declare keywords, comment syntax, and string delimiters. Place it in your preamble before the first use.
\lstdefinelanguage{MyLang}{
keywords={let, const, fn, return, if, else},
keywordstyle=\color{blue}\bfseries,
sensitive=true,
comment=[l]{//},
commentstyle=\color{gray}\itshape,
string=[b]",
stringstyle=\color{orange},
}
\begin{lstlisting}[language=MyLang]
fn add(a: Int, b: Int) -> Int {
return a + b // sum of two integers
}
\end{lstlisting}Both packages typeset code, but they take very different approaches. Choose based on your build environment and highlighting requirements.
Open the styled listings example — with custom colours, line numbers, and a C++ binary search — directly in the FormaTeX playground. Edit and compile to PDF in seconds, no install required.
Yes. The listings package ships with every major LaTeX distribution (TeX Live, MiKTeX, MacTeX). You do not need to install anything extra — just add \usepackage{listings} to your preamble.
Pass numbers=left or numbers=right to \lstset{} or directly on the environment as an option. Control the style with numberstyle=\tiny\color{gray} and the gap width with numbersep=5pt.
Not directly — listings processes its content as plain ASCII by default. For accented characters (é, ü, etc.) add inputencoding=utf8 and define each character with \lstset{literate={é}{{\'{e}}}1 ...}. For full Unicode support, consider the minted package with LuaLaTeX.
The verbatim environment reproduces text character-for-character with no formatting at all. listings builds on that foundation and adds syntax highlighting, line numbers, captions, frames, cross-referencing, and language-aware keyword detection — making it far more suitable for typesetting actual source code.
\end{document}
Use the FormaTeX API or playground to compile documents with the listings package. No local TeX installation needed — just send your source and get a PDF back.
One quick thing
We track anonymous usage — page views, feature usage, compilation events — to understand what works and what doesn't. No ads, no personal data, no third-party sharing.