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}File hello.py:
def greet(name):
return "Hello, " + name
print(greet("world"))Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- Text after
\end{verbatim}on the same line is lost; the log has the warningCharacters dropped after \end{verbatim}. Start a new line after it. verbatimand\verbdo not work inside the argument of another command such as\footnoteor\section. In\section,\verbstops with\verb illegal in argument. In a footnote the special characters act as LaTeX commands again, so an underscore there leads to Missing $ inserted.- A tab character is printed as a single space, so code indented with tabs loses its indentation.
fancyvrbandlistingshave atabsizesetting. - The
commentenvironment cannot be nested: the first\end{comment}ends it, and the second one is an error. Text after\begin{comment}or\end{comment}on the same line is dropped.
Related pages
- How-to Verbatim text in LaTeX
- How-to Inline code in LaTeX
- How-to Comments in LaTeX source
- Package
fancyvrb - Error \verb ended by end of line