6

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:

listing and figure subfloats side by side

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?

sfat
  • 425
  • 3
  • 12
  • 2
    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. – jub0bs Aug 20 '13 at 10:56
  • 4
    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

1 Answers1

7

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}

Result

Heiko Oberdiek
  • 271,626
sfat
  • 425
  • 3
  • 12
  • It gives following error: undefined control sequence for the \subbottom @sfat – alper May 08 '19 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 09 '19 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 '19 at 08:12
  • @alper Are you using the document class memoir? This is required for this example to work. – sfat May 11 '19 at 09:32
  • I am not using document class memoir, that's why the issue. @sfat – alper May 13 '19 at 10:32
  • The only thing I don't get is how your picture show both vertically centered but when I copy your code they're aligned to the bottom. – Telmo Trooper Aug 10 '20 at 00:50