The verbatim package

Reimplements the verbatim environment and adds \verbatiminput for whole files and the comment environment for text that LaTeX should skip.

Loading the package

\usepackage{verbatim}

LaTeX itself has the verbatim environment and \verb: text inside is printed in typewriter type exactly as typed, and characters such as \, %, $ and _ lose their special meaning.

The package processes the text line by line instead of reading it as one argument, so the length of a verbatim block is not limited. It also adds two features: \verbatiminput{file} prints a whole file, and everything between \begin{comment} and \end{comment} is ignored.

There is no highlighting, numbering or line breaking. For those see fancyvrb and listings.

Main commands

\begin{verbatim} ... \end{verbatim}Prints the lines as typed.
\begin{verbatim*} ... \end{verbatim*}The same with every space shown as a visible mark.
\verbatiminput{hello.py}Prints the content of a file.
\begin{comment} ... \end{comment}Skips everything inside, including LaTeX errors in it.
\verb|\section{...}|Short verbatim text inside a line; a kernel command that needs no package.

Complete example

\documentclass{article}
\usepackage{verbatim}
\begin{document}
A heading is written like this:
\begin{verbatim}
\section{Results}   % 100 % of the text, as typed
\end{verbatim}

\begin{comment}
This paragraph is not printed, and \undefinedcommand causes no error.
\end{comment}

The file \verb|hello.py| contains:
\verbatiminput{hello.py}
\end{document}
Result
The compiled example

Open in the editor

File hello.py:

def greet(name):
    return "Hello, " + name

print(greet("world"))

Compiled with pdfLaTeX from TeX Live 2023.

Pitfalls

Related pages