# How to place two code snippets next to each other with minted?

The minted package for code highlighting comes with a float environment called listing. Now I would like to place two of this code listings next to each other, but I have no idea how to do this. I tried using the multicol package, but code of the two listings overlapped.

Any suggestions on how to do this?

EDIT: Here is some little macro I created for convenience and an example on its usage.

\newcommand{\insertminted}[2]{\inputminted[linenos=true,
frame=lines,
framesep=2mm,
xleftmargin=2cm,
xrightmargin=2cm]{#1}{#2}}

\begin{listing}[H]
\insertminted{xml}{code_examples/user.xml}
\insertminted{js}{code_examples/user.js}
\caption{SomeCaption}
\label{lst:representation_examples}
\end{listing}


This way the code of user.xml is above the code of user.js. What I would like to have is the code next to each other in two cols, so that it can be compared. I also would like to have a seperate caption for each of them. The listing as a whole should appear in the output of \listoflistings.

-
Welcome to TeX.sx! You don't have to sign with your name since it automatically appears in the lower right corner of your post. – Werner Nov 30 '11 at 20:00
Could you post what you have so far? It's always good to show what you've done so that folks don't have to create things from scratch :) – cmhughes Nov 30 '11 at 20:34
Thx for the fast responses! I added some more information to my original post. If need be I can put up a minimal example document on the morrow. This is not as easy, because this is deeply nested within my bachelor thesis. :) – NilsHaldenwang Nov 30 '11 at 21:52
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Nov 30 '11 at 22:49
Have you considered using two minipages? – Peter Grill Nov 30 '11 at 23:02

The minted package provides the listing environment, which is a float. My first attempt was to put minipages inside of the listing environment together with their own captions. This did not work.

Instead, I have used the caption package (with compatability=false as detailed in the minted documentation) and then put two minted environments inside a figure environment (so that it floats), and used the captionof command to get the appropriate captions.

\documentclass{article}
\usepackage[compatibility=false]{caption}
\usepackage{minted}

\begin{document}
\listoflistings

\vspace{2cm}

\begin{figure}[!h]
\begin{minipage}{0.5\textwidth}
\centering
\begin{minted}{python}
some code
\end{minted}
\captionof{listing}{Sub caption}
\end{minipage}
\begin{minipage}{0.5\textwidth}
\centering
\begin{minted}{python}
some other code
\end{minted}
\captionof{listing}{Another sub caption}
\end{minipage}
\captionof{listing}{SomeCaption}
\label{lst:representation_examples}
\end{figure}

\end{document}


For anyone new to the minted package you'll need python-pygments

sudo apt-get install python-pygments


and you need to run pdflatex with

 pdflatex -shell-escape myfile.tex


both of which are detailed in the documentation.

-
Thx a lot, this works exactly as I want. – NilsHaldenwang Dec 1 '11 at 12:19
I already tried, need to get 15 reputation before I can upvote. I will do it as soon as I can. – NilsHaldenwang Dec 1 '11 at 21:02

Using a minipage is an easy way to get two things next to each other. Here is an example using lstlisting from the listings package. You should be able to adapt this for the minted package.

\documentclass[border=2pt]{standalone}
\usepackage{listings}

\begin{document}
\begin{minipage}[t]{0.45\linewidth}
\begin{lstlisting}[caption={Some XML Caption}]
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
\end{lstlisting}
\end{minipage}
%
\begin{minipage}[t]{0.45\linewidth}
\begin{lstlisting}[caption={Some Javascript Caption}]
... javascript code ...
... javascript code ...
... javascript code ...
\end{lstlisting}
\end{minipage}
\end{document}

-
I'm not sure what documentclass standalone is, but it gives me a compile error. If I change this to article it works. – Faheem Mitha Dec 19 '11 at 22:00
@FaheemMitha: You must have an old distribution as the standalone package/class has been part of the standard distribution for a while now. – Peter Grill Dec 19 '11 at 22:07
Not that old.TeX Live 2009 as packaged for Debian squeeze. – Faheem Mitha Dec 19 '11 at 22:09