Double column layout, left side for text, right side for listings

For a documentation I am having a special chapter where there are a lot of code listings, compared to the amount of prose text. What I would like to end up with is a two column layout, where the left column is used for the prose text only and the right column is used for code listings only.

The following creates a two page double column document, where the left column always says FOO and the right column only says BAR.

\documentclass{article}
\usepackage{multicol}
\usepackage{multido}

\begin{document}

\begin{multicols}{2}
\noindent        \multido{}{322}{FOO }  \multido{}{322}{BAR }
\multido{}{322}{FOO } \multido{}{322}{BAR }
\end{multicols}

\end{document}


I want the same effect, but without manually chopping up my input. I want a macro and/or an environment which I tell \multido{}{644}{FOO } and \multido{}{644}{BAR } and it automagically does the page/column breaking as illustrated by the example above.

Is there an environment that does this? Does anybody see a simple and quickway to achieving this?

WIP:

I've been trying a hack or two, but kept failing. The thing is, a page break in the left column must make the text continue in the left column of the next page. A page break in the right column must make the listings continue in the right column of the next page. My core idea was I have two "input-saveboxes" A and B and whenever LaTeX encounters a column break in a multicols environment it switches over to the other input-savebox.

But I never got close to implement anything like that. My best looking attemt was \hsize=.5\hsize and use \makebox to generate boxes that have neither width, nor height nor depth, but contain other boxes which carefully place the listings. Using this approach I cannot make listings break at the end of pages/columns.

There may be a way using a combination of flowfram as described in this answer and multicols:

• have a multicols with one column inside a staticcontent environment
• bewitch \pagebreak inside the multicolumn environment to do a \continueonframe in combination with \refstepcounter

I'll try this next while hoping for a ready to use solution ...

Update

The bewitching \pagebreak idea was based on some properties I thought the multicols environment had. Seems like I only immagined those. I still think multicols could be hacked to do what I want, because it already follows the approach of eating away form \box\partial@page. By adding a prefilled second box for partial pages and alternating output this could be done. However, this is a bigger project I won't be able to complete in time. And I'm still guessing a lot, because my TeX is not that good and reading Frank's TeX is almost as challenging as reading Perl code.

• a similar question was asked on the texhax list in march 2010, albeit for facing pages. these postings may be helpful: texhax: Multiple documents within a document and the answer at tug.org/pipermail/texhax/2010-March/014546.html – barbara beeton Oct 27 '14 at 14:25
• The simplest thing usually is to use a single column layout with \textsize reduced to one column width, then increase \marginparwidth by the same amount, then you can put your listings in the margin using standard marginpar or one of the packages that re-implement that. – David Carlisle Oct 27 '14 at 17:11
• @barbarabeeton: Thanks! \vsplit is the command that might help a lot. @DavidCarlisle: I couldn't find marginpar implementations that could do pagebreaks. – Bananguin Oct 27 '14 at 22:17
• @user1129682 well we can always make one... – David Carlisle Oct 27 '14 at 23:42

This is a bit rough but may get you started. It assumes there are no floats, it also doesn't reposition the page head and foot over both columns (fancyhdr could help with that) and it's probably incompatible with something,...

\begin{filecontents*}{file1.x}
\
\\\
\\\\\
some code [
here}
&& here
some code [
here}
&& here
some code [
here}
&& here
some code [
here}
&& here
some code [
here}
&& here
some code [
here}
&& here
some code [
here}
&& here
some code [
here}
&& here
some code [
here}
&& here
some code [
here}
&& here
some code [
here}
&& here
\end{filecontents*}
\begin{filecontents*}{file2.x}
$<mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo> <mi>a</mi><mo>=</mo><mi>b</mi> <mo>+</mo>$
\end{filecontents*}
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}

\def\a{One two three four five six. }
\def\b{\a\a$$\theequation=\theequation$$ \roman{equation} \roman{equation} \roman{equation} \a}
\def\c{Red yellow blue green black white. }

\makeatletter
\def \@combinefloats {%
\ifvoid\rightcol\else
\global\setbox\@ne\vsplit\rightcol to \textheight
\global\setbox\@ne\vbox to \textheight{\unvbox\@ne\vss}%
\setbox\@outputbox\vbox{\hbox{\box\@outputbox\kern\marginparsep\box\@ne}}%
\fi}
\makeatother

\usepackage{listings}

\newbox\rightcol
\setbox\rightcol\vbox{}