Generallstlisting spread between two pages causes errors

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
monstermunch
Posts: 2
Joined: Tue Nov 10, 2009 10:05 pm

lstlisting spread between two pages causes errors

Post by monstermunch »

Hi,

I'm making using of the listings package to format code. I frequently use the lstlisting environment to format blocks of inline (i.e. not floating) code:

"
Here is some code:

\begin{lstlisting}
example
example
example
example
example
example
example
\end{lstlisting}
"

This works fine most of the time. Every now and then, a lstlisting block will causes Latex errors after I add some extra text to the top of the document. One lstlisting block will sometimes generate 100 odd errors, all of the form:

"Extra }, or forgotten $. |"

This appears to happen when the lstlisting block starts on one page and ends on the other. Adding text above a block can push it onto the next page, meaning one can give me problems later when Latex was happy with it before.

The only workaround I have to to manually tweak the page so this doesn't happen. Is there anything better I can do?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

lstlisting spread between two pages causes errors

Post by gmedina »

Hi,

can you please post a minimal working example (MWE) showing the mentioned problem?

I ask you for this MWE since the following example produces no errors on my system:

Code: Select all

\documentclass{article}
\usepackage{listings}
\usepackage{lipsum}

\begin{document}

%\lipsum[1-4]
\begin{lstlisting}
example
example
example
example
example
example
example
example
example
example
example
\end{lstlisting}

\end{document}
Compile the example code as it is; then, uncomment the line \lipsum[1-4] and compile again (this will add some text before the listing and will force it to cross pages).
1,1,2,3,5,8,13,21,34,55,89,144,233,...
monstermunch
Posts: 2
Joined: Tue Nov 10, 2009 10:05 pm

Re: lstlisting spread between two pages causes errors

Post by monstermunch »

Thanks for the reply. I've tried to make a minimal working example but cannot seem to reproduce this behaviour in smaller documents. I'm making use of a custom thesis package and the document is about 200 pages. I was hoping it would just be some common problem. It seems clear it's a problem when the lstlisting block goes over a page as I can resolve the errors by adding a "\newpage" just before the block. Any idea of things I could try?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

lstlisting spread between two pages causes errors

Post by gmedina »

Without a MWE, all I can suggest you is to compile the code that produces the errors and post here, as an attachment, the .log file obtained. Perhaps this will help us to detect the problem (but I don't promise anything).
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply