Tx · LaTeX & TikZ

Why Is Text Dropped at the Start of a Listings Block?

Published By the 9to5Science editorial team

At a glance

  1. 1. Check the opening line

    Put a line break immediately after \begin{lstlisting}; code written on that same line may be discarded.

  2. 2. Keep code verbatim

    Place the listing contents on following lines, without LaTeX commands intended to format them.

  3. 3. Close the environment

    Write \end{lstlisting} on its own line so the listing ends where you expect.

If text seems to disappear right after a lstlisting environment begins, first check whether the code starts on the same line as \begin{lstlisting}. Put a line break after the opening command and begin the code on the next line. The listings package reads the environment as verbatim text, so its contents are not processed as ordinary LaTeX.

Put the listing contents on the next line

Use this structure:

\begin{lstlisting}
first line of code
second line of code
\end{lstlisting}

Avoid placing code directly after the opening command, like this:

\begin{lstlisting}first line of code
second line of code
\end{lstlisting}

With lstlisting, the opening line is for the environment command and any listing options. The code belongs on the following line. If you put text after the command on that opening line, it may not be included as listing content.

Minimal working example

This complete example uses the listings package and prints both lines of code:

\documentclass{article}
\usepackage{listings}

\begin{document}

Here is a code listing:

\begin{lstlisting}
print("first line")
print("second line")
\end{lstlisting}

\end{document}

The opening and closing commands each appear on their own lines. The text between them is displayed as code rather than interpreted as LaTeX. For example, a backslash in the listing is printed as a backslash; it does not start a LaTeX command.

You can also provide options after the opening command, then start the code on the next line:

\begin{lstlisting}[language=Python]
print("hello")
\end{lstlisting}

If text still disappears

Check where the missing text is located and what kind of text it is:

  • Missing text is on the opening line: Move it to the next line, inside the environment.
  • Missing text is after the listing: Check that \end{lstlisting} occurs after that text, not before it.
  • The listing never ends as expected: Put \end{lstlisting} on a line by itself and check that its spelling matches exactly.
  • You expected LaTeX commands to run inside the listing: They will be printed literally. Move those commands outside the listing or use an appropriate listing configuration.
  • You need to show a short piece of code within a sentence: Use \lstinline rather than opening a block environment. For example, \lstinline|x = 1| displays the code inline.

A listing environment is a verbatim environment: it captures its contents for display instead of processing them as normal LaTeX. That behavior is useful for code, but it means ordinary LaTeX rules—such as using commands inside the environment to add formatting—do not apply.

References

  1. The listings package

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