5

I have a two column article:

\documentclass[a4paper, twocolumn]{article}

Inside this is a bigger lstlisting which has a float=* to merge the two columns. However this listing needs a page break. LaTeX won't break the listing due to the float I think. What can I do to let LaTeX page break my listing?

0

2 Answers 2

5

If you can use the package multicol instead, then try this:

\documentclass[english,a5paper]{article}
\usepackage{babel}
\usepackage{listings}
\usepackage{multicol}
\usepackage{blindtext}
\begin{document}
\begin{multicols}{2}
 \blindtext[2]
\end{multicols}

\begin{lstlisting}[caption=A Listing,,basicstyle=\ttfamily,numbers=left]
ffooo 
ffooo 
ffooo 
ffooo 
ffooo 
ffooo 
ffooo 
ffooo 
ffooo 
ffooo 
ffooo 
\end{lstlisting}

\begin{multicols}{2}
 \blindtext[3]
\end{multicols}

\end{document}
3

Herbert gave a good answer for this if you can use the multicol package. If you cannot, or don't want to, here's one way to do it. I'm not claiming that this is the best way to do it, or even that it works in all cases, but at least with my limited testing, it seems to work.

\documentclass[twocolumn]{article}
\usepackage{afterpage}
\usepackage{listings}
\usepackage{lipsum} % Just for dummy text.
\makeatletter
\newbox\loc@box
\newenvironment{longonecolumn}{%
        \ifvoid\loc@box\else
                \errmessage{Too many longonecolumn environments too
                close together.}%
        \fi
        \global\setbox\loc@box\vbox\bgroup
                \hsize\textwidth
                \columnwidth\hsize
                \linewidth\hsize
                \@twocolumnfalse
}{%
        \egroup
        \afterpage\loc@placebox
}
\newcommand*\loc@placebox{%
        \if@firstcolumn
                \loc@placebox@
        \else
                \afterpage\loc@placebox
        \fi
}
\newcommand*\loc@placebox@{%
        \onecolumn
        \begingroup
                \loop\ifdim\ht\loc@box>\vsize
                        \setbox\z@\vsplit\loc@box to\vsize
                        \unvbox\z@
                        \vfill
                        \pagebreak
                \repeat
                \ifdim\ht\loc@box>\dbltopfraction\vsize
                        \unvbox\loc@box
                        \vfill
                        \twocolumn\relax
                \else
                        \twocolumn[\unvbox\loc@box\vspace\dbltextfloatsep]%
                \fi
        \endgroup
        \global\setbox\loc@box\box\voidb@x % Probably not needed
}
\makeatother
\begin{document}
\lipsum
\lipsum[1-3]
\begin{longonecolumn}
\begin{lstlisting}[caption=Here's my
caption,basicstyle=\ttfamily,numbers=left]
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
\end{lstlisting}
\end{longonecolumn}
\lipsum
\end{document}

The idea here is that the longonecolumn environment packages its argument up into a single box \loc@box. It then uses TeX's page breaking algorithm via \vsplit to break the box into \vsize sized chunks and then lets those chunks fit on the page naturally. After there is only a single chunk of size at most \vsize, it checks if it fits in the amount of space double column figures are allowed to take up. If it doesn't, then the last chunk also gets its own page. Otherwise, it gets put at the top of a normal two column page in the same way that \maketitle puts the title information at the top of the page.

You must log in to answer this question.