Tx · LaTeX & TikZ

How to Display LaTeX Code in a LaTeX Document

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose a method

    Use verbatim for a quick example, or listings for configurable code blocks and source files.

  2. 2. Load listings

    Add the listings package and set options such as font, line wrapping, and syntax highlighting.

  3. 3. Handle Beamer frames

    Mark any frame containing verbatim-style code as fragile.

  4. 4. Compile and check

    Build the document and adjust the listing width or font if lines overflow.

To show LaTeX source in a document, you want the commands to appear as text rather than be interpreted as formatting instructions. The built-in verbatim environment is enough for a quick example. For code blocks with options such as line wrapping and syntax highlighting, use the listings package.

In Beamer, code environments need a little extra care: mark the containing frame as [fragile]. The example below is a complete presentation showing LaTeX source code.

A complete Beamer example with listings

The listings package formats code as text and can apply language-specific styling. This example uses a monospaced font, enables line wrapping, and labels the listing as LaTeX code.

\documentclass{beamer}
\usepackage{listings}

\lstset{
  language=[LaTeX]TeX,
  basicstyle=\ttfamily\small,
  columns=fullflexible,
  keepspaces=true,
  breaklines=true
}

\begin{document}

\begin{frame}[fragile]{A LaTeX source example}
Here is a command that prints a heading:

\begin{lstlisting}
\section{Introduction}
This is text in the document.
\end{lstlisting}
\end{frame}

\end{document}

Compile this with a standard LaTeX engine such as pdfLaTeX. The [fragile] option tells Beamer to handle the frame in a way that supports verbatim-like content, including lstlisting. Without it, compilation commonly fails with an error about verbatim material or an unexpected end of the frame.

The language=[LaTeX]TeX option identifies the language for listings’ syntax rules. basicstyle chooses a small monospaced font, while breaklines=true allows long lines to wrap. keepspaces=true preserves runs of spaces, which can matter when showing indented code.

Quick alternative: verbatim

If you do not need syntax styling or line-wrapping options, LaTeX’s built-in verbatim environment is simpler:

\begin{verbatim}
\textbf{This command is printed, not executed.}
\end{verbatim}

In an ordinary article or report, this works directly. In Beamer, put it in a [fragile] frame just as you would with lstlisting.

For a short piece of code within a sentence, use \verb with a delimiter that does not occur in the code:

The command \verb|\emph{important}| emphasizes text.

The characters between the delimiters are printed literally. \verb cannot span paragraphs or be used inside many command arguments, such as a section title. In Beamer, it also belongs in a fragile frame.

Include code from a separate file

If your source is already saved in a file, \lstinputlisting avoids copying it into the document. For example, if example.tex is next to your main document, write:

\lstinputlisting[language={[LaTeX]TeX}]{example.tex}

You can use the same listing options as for lstlisting, either globally with \lstset or locally in the optional argument. This is useful when the code changes often, since the displayed version comes from the file itself. In Beamer, the command still appears in a fragile frame.

Common mistakes

  • Forgetting [fragile] in Beamer: Add it to the frame containing verbatim, lstlisting, or \verb content.
  • Expecting displayed code to run: verbatim and listings print the source; they do not execute or typeset the commands shown inside them.
  • Using \verb where an environment is needed: For multiple lines, use verbatim or lstlisting instead.
  • Putting the listing’s closing marker in the sample: A literal \end{lstlisting} can terminate a lstlisting environment early. If you need to display that exact line, use a different method, such as including an external file with \lstinputlisting or choosing a different delimiter with an appropriate setup.
  • Assuming long lines will fit: Enable breaklines=true, reduce the listing font, or shorten the displayed lines. Wrapping is often preferable to shrinking text until it is hard to read.

For most presentations, listings is a good starting point: it works without an external highlighting tool, gives you control over the appearance, and can include code directly from a file.

References

  1. The Listings Package
  2. The beamer class User Guide

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.