13

How do I fix this haunting message?

"text dropped after beginning of listing"

In my preamble I use:

\usepackage{listings}

\lstset{basicstyle=\ttfamily,breaklines=true}

Then within the document I use the package this way:

\begin{lstlisting} http://www.victim.com/cms/login.php?..\end{lstlisting}

Also this way:

\begin{lstlisting}
// connect to the database
$conn = mysql_connect("localhost","username","password");
...
\end{lstlisting}

Is that because the text in a listing environment shouldn't be on one line only?

2
  • For single-line listings, you could use \lstinline rather than the environment form.
    – Werner
    Jan 23, 2013 at 18:16
  • Should I define \lstinline in the preamble as well? \def\inline{\lstinline[basicstyle=\ttfamily]} And whenever I need I only call it? Can you please show me a practical example? Jan 23, 2013 at 18:21

2 Answers 2

10

For lengthy listings, leave the environment \begin and \end on their own lines. For short, in-line listings, use \lstinline (already defined). Here's an example of their usages:

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{basicstyle=\ttfamily,breaklines=true}
\begin{document}
\begin{lstlisting}
http://www.victim.com/cms/login.php?..
\end{lstlisting}
Some text and \lstinline!http://www.victim.com/cms/login.php?..! and some more text.
\end{document}

The listings documentation uses the following terminology (section 4.2 Typesetting listings, p 25-26):

  • Code snippets: \lstinline
  • Displayed code: lstlisting environment
  • Stand alone files: \lstinputlisting

Regarding the use of the displayed code lstlisting environment, it also states (p 26):

In contrast to the environment of the verbatim package, LaTeX code on the same line and after the end of environment is typeset respectively executed.

This allows you to use

\begin{lstlisting}
http://www.victim.com/cms/login.php?..\end{lstlisting}

but not

\begin{lstlisting}http://www.victim.com/cms/login.php?..
\end{lstlisting}

Regardless, I don't see the need to keep code listings and their enclosed environment definitions on the same line, so even the allowed use above could benefit from putting \end{lstlisting} on its own line.

4

The lstlisting environment ignores any text after \begin{lstlisting} on the same line, hence the warning.

There is even another pitfall that one must be aware of: A comment at the end of the line ending \begin{lstlisting}[…] extend the line as usual. Example:

\begin{lstlisting}[float=bp] % try to place at bottom of page for some reason
foo;
bar;
% baz;
\end{lstlisting}

prints

bar;
% baz;

The comment will be correctly parsed by latex and ignored by lstlistings (as the warning indicates). This might be the intended behaviour.

However, as the comment extends the line, the first line of the listing will also be ignored!

Instead, ensure that the \begin{lstlisting} command is not followed by any other character on that line - intermediate comments are allowed, though:

\begin{lstlisting}[
    float=bp % try to place at bottom of page for some reason
]
foo;
bar;
% baz;
\end{lstlisting}

(I am posting this as an answer because my reputation is not high enough to post comments.)

2
  • This seems to be a very specific case of the problem at hand.
    – Werner
    Aug 9, 2016 at 19:16
  • 1
    @Werner I tried to elaborate a bit more on the causes of the warning of the original post. I hope this answer is now more useful. Aug 10, 2016 at 9:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .