0

I am trying to get source code and a picture side by side with two captions that are aligned with the upper part of the caption text as suggested in this thread: Having two figures subcaption on the same line.

For the alignment someone suggested the \subcaptionbox environment. However, when trying to insert a lstlising into the \subcaptionbox environment in the following example I get multiple errors:

\begin{figure}[!htbp]
\centering
\subcaptionbox{Example of a parameterized type in Scala adopted from~\cite{2.13-types}}[.45\textwidth]{
  \begin{lstlisting}
class TreeMap[A <: Comparable[A], B] { … }
  \end{lstlisting}
}
\subcaptionbox{UML representation of parameterized type TreeMap}[.45\textwidth]{
  \includegraphics{parameterized-types.pdf}
}
\label{fig:scalaspecific:bounds}
\caption{Examplary UML representation of parameterized types}
\end{figure} 

Produced Errors:

! Package inputenc Error: Invalid UTF-8 byte "80.See the inputenc package documentation for explanation.Type H <return> for immediate help.... }
! Argument of \lst@next has an extra }.<inserted text>\par }
...

I basically have two questions now.

First: Is there a fix to this, so that one can put lisitings inside \subcaptionbox environments?

Second: If there is no fix, is there another way to achieve same line caption alignment for a picture and code?

4
  • The lstlisting environment can't be the argument of a macro, instead of \subcaptionbox, you could use the subfigure environment.
    – Skillmon
    Dec 5 '20 at 16:21
  • but with subfigure the captions are aligned at the bottom, but I want them to be aligned at the top. Is it possible to achieve this with subfigure? Dec 5 '20 at 16:49
  • 1
    Put the code of your listing in a separate file. Then input the listing with \lstinputlisting{<file>} instead. This way you should be able to put the listing in the argument of \subcaptionbox. Also, you'll have to change to ... if you're using pdflatex.
    – Skillmon
    Dec 5 '20 at 19:37
  • \lstinputlisting seems to be the best solution, do you want to state an answer yourself, or should i provide a detailed answer ? Dec 7 '20 at 11:40
1

If you put the contents of your listing in an external file, you can put \lstinputlisting in the argument of \subcaptionbox:

\begin{filecontents*}{\jobname.lst}
class TreeMap[A <: Comparable[A], B] { ... }
\end{filecontents*}

\documentclass{article}
\usepackage{subcaption,listings,graphicx}

\newsavebox{\listingsbox}

\begin{document}

\begin{figure}[!htbp]
\centering

\subcaptionbox{Example of a parameterized type in Scala 
  adopted from~\cite{2.13-types}}[.45\linewidth]{%
      \lstinputlisting[breaklines]{\jobname.lst}%
}\hfill
\subcaptionbox{UML representation of parameterized type TreeMap}[.45\textwidth]{
  \includegraphics[width=\linewidth]{example-image-duck}
}
\caption{Examplary UML representation of parameterized types\label{fig:scalaspecific:bounds}}
\end{figure}

\end{document}

enter image description here

2

Save the lstlistings environment in a box, which you can use inside \subcaptionbox.

\documentclass{article}
\usepackage{subcaption,listings,graphicx}

\newsavebox{\listingsbox}

\begin{document}

\begin{figure}[!htbp]
\centering

\begin{lrbox}{\listingsbox}
\begin{minipage}{0.45\textwidth}
\begin{lstlisting}[breaklines]
class TreeMap[A <: Comparable[A], B] { ... }
\end{lstlisting}
\end{minipage}
\end{lrbox}

\subcaptionbox{Example of a parameterized type in Scala 
  adopted from~\cite{2.13-types}}{%
  \usebox{\listingsbox}%
}
\subcaptionbox{UML representation of parameterized type TreeMap}[.45\textwidth]{
  \includegraphics[width=\linewidth]{example-image}
}

\caption{Examplary UML representation of parameterized types}
\label{fig:scalaspecific:bounds}

\end{figure}

\end{document}

enter image description here

The \label should go after \caption.

1
  • Well, technically it's best to put the \label in \caption.
    – Skillmon
    Dec 6 '20 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.