[Tex/LaTex] How to get a double column verbatim in a single column table, in a double column document

floatspositioningsourcecodetwo-column

I have a double column document in which I need to place two source code listings side by side. Naturally a two column document's single column is too narrow so the two listings need to span the page width.

\onecolum \twocolumn isn't an option because these need to be in the middle of a document and space is a premium.

I've tried several options of figure*, table* but can't get my head around how to get it to display properly.

In a single column document it was simple enough as I used {multicols}{2} and then used lstlisting to display the code, but switching to two column as expected, places the two columns in a single document column.

Any ideas on how to go about this?

Best Answer

Here's one possible solution using the twocolumn class option and figure* and two minipages to typeset the listings (since figure* was used, the listings will appear on the top of the next page):

\documentclass[twocolumn]{article}
\usepackage{listings}
\usepackage{lipsum}

\begin{document}

\lipsum[1-6]
\begin{figure*}
\begin{minipage}[t]{0.5\textwidth}
\begin{lstlisting}
if (i<=0) then i := 1;
if (i>=0) then i := 0;
if (i<>0) then i := 0;
\end{lstlisting}
\end{minipage}%
\begin{minipage}[t]{0.5\textwidth}
\begin{lstlisting}
if (i<=0) then i := 1;
if (i>=0) then i := 0;
\end{lstlisting}
\end{minipage}%
\caption{Two  side-by-side listings}
\label{fig:listings}
\end{figure*}
\lipsum[1-6]

\end{document}

enter image description here

And here's now another option: instead of using the twocolumn document option, the multicol package is used to produce the text in two-column mode; the multicols environment is ended, the side-by-side listings are typeset using simple minipages (the caption package was used to produce a possible caption using \captionof), and then another multicols environment begins:

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

\begin{document}

\begin{multicols}{2}
\lipsum[1-2]
\end{multicols}
\noindent\begin{minipage}{\textwidth}
\begin{minipage}[t]{0.5\textwidth}
\begin{lstlisting}
if (i<=0) then i := 1;
if (i>=0) then i := 0;
if (i<>0) then i := 0;
\end{lstlisting}
\end{minipage}%
\begin{minipage}[t]{0.5\textwidth}
\begin{lstlisting}
if (i<=0) then i := 1;
if (i>=0) then i := 0;
\end{lstlisting}
\end{minipage}%
\captionof{figure}{Two  side-by-side listings}
\label{fig:listings}
\end{minipage}
\begin{multicols}{2}
\lipsum[1-2]
\end{multicols}

\end{document}

In this last option, the usual restriction of multicol with floats apply.

enter image description here

Related Question