# Placing listing and figure side by side in memoir

I am writing a document using the memoir class where I want to place listings (with the listings package) and figures side by side. My first approach was to save the listing in a box and use subbottom to place both elements side by side:

\documentclass[12pt,oneside,a4paper]{memoir}

\usepackage{graphicx}
\usepackage{listings}
\usepackage{color}
\usepackage{calc}
\usepackage{caption}

\newsubfloat{figure}
\definecolor{lgrey}{RGB}{223,223,223,0}
\lstset{
basicstyle=\footnotesize\ttfamily,
frame=lines,                                    % environment border
rulecolor=\color[cmyk]{0.43, 0.35, 0.35, 0.01}, % top rule color
xleftmargin=17pt,                               % left margin
framexleftmargin=17pt,                          % frame left margin
framextopmargin=2pt,                            % frame top margin
backgroundcolor=\color{lgrey},                  % background color
showstringspaces=false                          % show spaces in strings
}

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}
{\colorbox[cmyk]{0.43, 0.35, 0.35, 0.01}
{\parbox{\textwidth-6pt}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{
format=listing,
labelfont=white,
textfont=white,
font={sf,bf,footnotesize}}

\begin{document}

% Example that does not work as intended
% --------------------------------------
\newsavebox{\firstlisting}
\begin{lrbox}{\firstlisting}
\begin{lstlisting}[linewidth=0.45\textwidth,caption=Hello,language=python]
echo("Hello World!")
\end{lstlisting}
\end{lrbox}

\begin{figure}[htbp]
\centering
\subbottom{
\usebox{\firstlisting}}
\subbottom{
\includegraphics[width=0.45\textwidth]{philosoraptor}
\label{fig:c}}
\caption{Example}
\end{figure}

% How the listing environment normally looks like
% -----------------------------------------------
\begin{lstlisting}[caption=Hello,language=python]
echo("Hello World!")
\end{lstlisting}

\end{document}


However, this messes up the placement, as I have formated my listings environment to look like this: https://stackoverflow.com/a/742069/1809175

Here is how it looks in the document:

I also tried two minipages in a figure environment but this results in cryptic error messages.

Is there a best practice for placing different environments like listings and figures side by side in the memoir class?

• Welcome to TeX.SX! Please make your code compilable, starting with \documentclass{...} and ending with \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to help you. Help them help you: remove that one hurdle between you and a solution to your problem. – jubobs Aug 20 '13 at 10:56
• The listings has full width so that will never work. You may need to wrap the listings in a minipage to control the width. – daleif Aug 20 '13 at 11:28
• @daleif you could expand a little your comment (perhaps providing a little example?), and turn it into an answer. – Gonzalo Medina Aug 20 '13 at 17:08

Thanks to @daleif I figured out the solution: The minipage has to be wrapped around the box, which encapsulates the listings environment. In my last attempt I did not use the box inside the minipage.

Here is the working code (the preamble stays the same):

\begin{document}

\newsavebox{\firstlisting}
\begin{lrbox}{\firstlisting}
\begin{minipage}[c]{0.45\textwidth}
\begin{lstlisting}[caption=Hello,language=python]
echo("Hello World!")
\end{lstlisting}
\end{minipage}
\end{lrbox}

\begin{figure}[htbp]
\centering
\subbottom{
\usebox{\firstlisting}}
\subbottom{
\begin{minipage}[c]{0.45\textwidth}
\includegraphics[width=0.95\textwidth]{philosoraptor}
\end{minipage}
\label{fig:c}}
\caption{Example}
\end{figure}
\end{document}


• It gives following error: undefined control sequence for the \subbottom @sfat – alper May 8 at 16:35
• @alper Did you import all the required packages? As stated in my answer, "the preamble stays the same". This means the preamble of the example in the original question. – sfat May 9 at 18:05
• I imported all the required packages given on the question but still I am having the same error. @sfat – alper May 10 at 8:12
• @alper Are you using the document class memoir? This is required for this example to work. – sfat May 11 at 9:32
• I am not using document class memoir, that's why the issue. @sfat – alper May 13 at 10:32