[Tex/LaTex] Using lstlistings in two columns

horizontal alignmentlistingsmulticolpage-breaking

My goal is to display source code (awk, Python, R and SAS) and text (their output) side-by-side. Both are loaded from external files, and can be pretty long sometimes (pagebreaking is inevitable), the source code should be syntax highlighted. The source code should stay on the left side, and the output should stay on the right side, even if there is a pagebreak.

I'm using the listings package for its syntax highlighting.

I can't find an environment to behave right when there's a pagebreak.

Some of what I've tried already:

  1. multicols: this environment is fine, until there's a pagebreak (left side breaks to the right side and messes up everything)
  2. minipage: no pagebreak yet

Best Answer

I have finally solved my problem (as I mentioned it in a comment) using the paracol environment.

\documentclass{minimal}
\usepackage{paracol}
\begin{document}
\begin{paracol}{2}
Left
\switchcolumn
Right
\end{paracol}
\end{document}

Still, the background coloring needed some patching up. I've replaced the listing background using the mdframed package.

So the final MWE has come to this:

\documentclass{minimal}

\usepackage{paracol}
\usepackage{listings}
\usepackage[framemethod=TikZ]{mdframed}

\lstnewenvironment{code}{
    \mdframed[outerlinecolor=black, backgroundcolor=gray]{}
}{
    \endmdframed
}

\begin{document}

\begin{paracol}{2}
\begin{code}
Left
\end{code}
\switchcolumn
\begin{code}
Right
\end{code}
\end{paracol}

\end{document}

However, this also generates some warnings (multiply defined labels in mdframed), I've made a patch for this, and asked here whether there is a better solution.

Related Question