我有一篇两栏文章:
\documentclass[a4paper, twocolumn]{article}
这里面有一个更大的lstlisting
,它有一个float=*
可以合并两列的。但是这个列表需要分页符。我认为 LaTeX 不会因为浮动而破坏列表。我该怎么做才能让 LaTeX 分页我的列表?
答案1
如果您可以使用该包multicol
,那么请尝试以下操作:
\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}
答案2
如果您可以使用该软件包,Herbert 对此给出了一个很好的答案multicol
。如果您不能或不想使用,这里有一种方法可以做到。我并不是说这是最好的方法,甚至不是说它在所有情况下都有效,但至少在我有限的测试中,它似乎有效。
\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}
这里的想法是,longonecolumn
环境将其参数打包成一个单独的框\loc@box
。然后它使用 TeX 的分页算法将\vsplit
框拆分成\vsize
大小合适的块,然后让这些块自然地适合页面。在只有一个大小不超过的块之后\vsize
,它会检查它是否适合双列图形允许占用的空间量。如果不适合,那么最后一个块也会有自己的页面。否则,它会被放在正常的两列页面的顶部,就像\maketitle
将标题信息放在页面顶部一样。