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?

  • The lstlisting environment can't be the argument of a macro, instead of \subcaptionbox, you could use the subfigure environment. – Skillmon Dec 05 '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? – Tilman Zuckmantel Dec 05 '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 05 '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 ? – Tilman Zuckmantel Dec 07 '20 at 11:40

2 Answers2

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.

egreg
  • 1,121,712
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

Skillmon
  • 60,462