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

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, sadly.

Instead, I have used the caption package (with compatibility=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.

screenshot

\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.


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.

enter image description here

\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}