[Tex/LaTex] Give side by side minipages one (shared) caption instead of two

listingsminipage

I have two lstlisting areas that are shown side by side by using two minipages:

\noindent\begin{minipage}[t]{.43\textwidth}
\begin{lstlisting}[...]{part1}
// Code
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{.54\textwidth}
\begin{lstlisting}[...]{part2}
// Code
\end{lstlisting}
\end{minipage}

At the moment, both listings have their own caption (not shown above for the sake of simplicity). What I want to do is to display one single caption instead of two captions. The single caption should be centered below both listings. Is it possible to treat the two listings as one single listing? Thanks.

Best Answer

You could use the \captionof command from the package caption:

% arara: pdflatex

\documentclass{article}
\usepackage{listings}
\usepackage{caption}[2015/09/20]
\usepackage{showframe} % just for demo

\begin{document}    
\begin{center}
\begin{minipage}[t]{.43\textwidth}
    \begin{lstlisting}
    // Code
    \end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{.54\textwidth}
    \begin{lstlisting}
    // Code
    \end{lstlisting}
\end{minipage}
\captionof{lstlisting}{caption}
\end{center}
\end{document}

enter image description here

Related Question