Sometimes you need text to appear on the page exactly as you typed it — every command, comment, and special character switched off, set in a monospaced (typewriter) font. That mechanism is called verbatim. This page walks through inline \verb, the block-level verbatim environment, whole-file \verbatiminput, the important limitation that none of these may sit inside another command’s argument (and how to work around it), the alltt variant that still honors a few commands, and finally fancyvrb for line numbers and frames.
The verbatim environment and \verb
The foundation of literal output is the verbatim environment, built into LaTeX from the start. Whatever you place between \begin{verbatim} and \end{verbatim} is printed as typed: a backslash no longer starts a command but prints as \, % does not begin a comment, spaces and line breaks are kept literally, and the whole block is set in the typewriter font (\tt). It is ideal for program source, or for showing strings full of $ and &.
\begin{verbatim}
for i in range(3):
print("100% & $5") # nothing here is interpreted
\end{verbatim}There is just one rule to respect: the string \end{verbatim} must not appear inside the environment, because LaTeX treats the moment it sees that string as the end of the environment. If you genuinely need to display the text \end{verbatim} itself, change the terminator with fancyvrb (below) or escape that piece with \verb.
To drop a little verbatim into the middle of a line, use the inline form \verb. The recipe: put one delimiter character right after \verb, then the text you want literal, then the same delimiter again. The delimiter may be any non-letter character that does not appear in the text. The common choice is \verb|...|, but if | occurs inside, pick another, such as \verb!...! or \verb+...+. Do not put a space between \verb and its delimiter.
The macro \verb|\textbf{...}| sets bold text, and
the pipe itself is shown with \verb!a|b! instead.The starred form \verb* prints the spaces inside as a visible space character (␣) — handy in code examples where the exact number of spaces matters. Likewise the verbatim* environment shows the spaces in a block as ␣.
That said, if your only goal is a string that tends to contain ~, #, %, or _ — a URL, say — then \url{...} from the url or hyperref package can be easier, since it also breaks the line at sensible points.
Typesetting a whole file (\verbatiminput)
To paste the contents of an external file as-is, the verbatim package (part of the LaTeX required tools) provides \verbatiminput{filename}. Load \usepackage{verbatim} in the preamble, then write \verbatiminput{program.py} in the body, and every line of that file is set verbatim. Unlike copying into the manuscript by hand, editing the source file updates the output automatically — no keeping the code and the document in sync by hand.
\usepackage{verbatim}
% ...
\verbatiminput{hello.py}The same verbatim package also adds a comment environment that skips everything between \begin{comment} and \end{comment} — not for literal output but for *not* printing at all, useful for temporarily setting aside a draft passage.
It cannot go inside a command argument
This is the single most common stumbling block with verbatim. \verb and the verbatim environment cannot appear inside another command’s argument — for example inside \section{\verb|code|}, a \footnote{...}, a table cell, or a \caption{...}. The reason is category codes (catcodes — the role assigned to each character). \verb switches the catcode regime just before it reads its argument, but a command’s argument has already been converted to a token list under the normal catcodes at the moment the command was called — so interpretation is over before \verb can change anything. Writing it this way usually errors out.
There are two workarounds. One is the cprotect package: just prefix the offending command with \cprotect, and verbatim inside its argument goes through. For instance, write \cprotect\section{\verb"foo"}. There is also \cprotEnv for protecting the \begin of an environment.
The other is \SaveVerb / \UseVerb from fancyvrb (below): a two-step approach where you save the verbatim text under a name first, then merely call that name inside the argument. Save with \SaveVerb{label}|verbatim text|, and typeset it with \UseVerb{label}.
% Workaround 1: cprotect
\usepackage{cprotect}
\cprotect\section{The \verb|\foo| command}
% Workaround 2: fancyvrb SaveVerb / UseVerb
\usepackage{fancyvrb}
\SaveVerb{cmd}|\foo|
\section{The \UseVerb{cmd} command}Keeping commands alive with alltt
In ordinary verbatim, you cannot highlight or color just part of a code example, because commands are switched off. The alltt package (also part of the standard LaTeX distribution) and its alltt environment solve this. alltt sets text monospaced and as-typed much like verbatim, except that the backslash \ and the braces { and } keep their usual meaning. So you get a verbatim-like look while still being able to run LaTeX commands inside.
\usepackage{alltt}
% ...
\begin{alltt}
def \textbf{greet}(name):
return "Hi, " + name \textit{# a comment}
\end{alltt}Here the function name greet becomes bold and the comment becomes italic, while everything else stays exactly as typed. The trade-off is that to print the three characters \, {, } literally you must spell them out as \textbackslash, \{, \} (whereas verbatim prints them directly). Reach for alltt when you want to add light formatting by hand, and verbatim when you want everything truly as-is.
Line numbers and frames with fancyvrb
The built-in verbatim offers neither line numbers nor frames. The more capable choice is the fancyvrb package, centered on the capital-V Verbatim environment (a different beast from lowercase verbatim). You set options per environment as \begin{Verbatim}[key=value, ...], or globally from the preamble with \fvset{key=value, ...}.
The options you reach for most: numbers=left (or right) adds line numbers, frame=single draws a box around the whole block, and fontsize=\small sets the font size. And commandchars lets you keep a few commands working inside the otherwise-verbatim text.
| Option | Typical values | What it does |
|---|---|---|
numbers | none / left / right | Position of line numbers (default none) |
frame | none / single / lines / leftline / topline / bottomline | Type of frame (default none) |
fontsize | \small, \footnotesize, etc. | Font size (default: same as body) |
commandchars | e.g. \\\{\} | Names the escape char and the two group chars, enabling commands |
The following example is a Verbatim block with line numbers on the left, a single frame, and a slightly smaller font.
\usepackage{fancyvrb}
% ...
\begin{Verbatim}[numbers=left, frame=single, fontsize=\small]
def greet(name):
return "Hello, " + name
\end{Verbatim}With commandchars=\\\{\}, the characters \, {, } act as the escape and group delimiters inside the verbatim text, letting you embed commands much like alltt. There is also \VerbatimInput[options]{filename} to pull in a whole file — unlike \verbatiminput, it accepts the options above. And via \SaveVerb / \UseVerb (shown earlier), verbatim works even inside command arguments.
One closing caveat: everything here is about printing text as-is, not syntax highlighting such as coloring keywords. To present program source with colors and formatting, the right tools are the dedicated listings package or minted, which uses Python’s Pygments (covered on the “Code listings” page).