Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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

share|improve this answer
    
Man perfect, you are amazing, thanks a lot! –  janjust Sep 4 '12 at 21:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.