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?

\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. – jub0bs Aug 20 '13 at 10:56listingshas full width so that will never work. You may need to wrap thelistingsin aminipageto control the width. – daleif Aug 20 '13 at 11:28