5

I have two listings I would like to place side-by-side in a figure. One of the listings is longer than the other. I want to have both listings centered horizontally and vertically aligned at the top (so that, in the image below, the first code block would be moved up to be approximately touching the red line). I have been enclosing my listings environments in \begin{tabular}{c} ... \end{tabular} to center them horizontally, and I have put them in two minipages to have them side-by-side, but trying to vertically-align them by adding [t] to the minipages doesn't seem to work with the tabular environments. If I remove the tabular environments, I can vertically-align the two minipages like I want, but the listings aren't centered anymore.

MWE:

\documentclass{article}

\usepackage{listings}
\usepackage{caption}

\begin{document}

\begin{figure}[ht]
    \centering
    \begin{minipage}[t]{0.45\linewidth}
        \centering
        \begin{tabular}{c}
            \begin{lstlisting}
here is some code
that is not as long
as the other code
            \end{lstlisting}
        \end{tabular}
    \end{minipage}
    \begin{minipage}[t]{0.45\linewidth}
        \centering
        \begin{tabular}{c}
            \begin{lstlisting}
here is some code
that is a bit longer
than the other code
so I would like the
two listings to be
vertically aligned
at the top
            \end{lstlisting}
        \end{tabular}
    \end{minipage}
    \caption{test}
\end{figure}

\end{document}

output of MWE

6
  • Try \parbox[t]... in the place of minipages.
    – Bernard
    Commented Oct 27, 2019 at 0:58
  • It looks like I can't use listings inside of a parbox (at least that's what I'm guessing from the cryptic listings error messages).
    – Anonymous
    Commented Oct 27, 2019 at 1:02
  • tabular also accepts the optional argument [t]. B.t.w., why do you need tabulars here?
    – Bernard
    Commented Oct 27, 2019 at 1:07
  • I've tried adding [t] to the tabular before, but it didn't seem to change anything. I need the tabulars to center the listings; it's the best solution I've found.
    – Anonymous
    Commented Oct 27, 2019 at 2:11
  • What you did is unclear to me: was it \begin{tabular}[t]{c}...?
    – Bernard
    Commented Oct 27, 2019 at 9:26

3 Answers 3

5

This may well not be the most minimal solution but it works, I think.

\documentclass{article}

\usepackage{listings}
\usepackage{caption}
\usepackage{adjustbox}
\begin{document}

\begin{figure}[ht]
    \centering
    \begin{adjustbox}{valign=t,minipage=0.45\linewidth}
        \centering
        \begin{tabular}{c}
            \begin{lstlisting}
here is some code
that is not as long
as the other code
            \end{lstlisting}
        \end{tabular}
    \end{adjustbox}
    \begin{adjustbox}{valign=t,minipage=0.45\linewidth}
        \centering
        \begin{tabular}{c}
            \begin{lstlisting}
here is some code
that is a bit longer
than the other code
so I would like the
two listings to be
vertically aligned
at the top
            \end{lstlisting}
        \end{tabular}
    \end{adjustbox}
    \caption{test}
\end{figure}

\end{document}

enter image description here

1

You can use tcolorbox to put contents side-by-side. Here is an example:

enter image description here

\documentclass{article}
\usepackage[most]{tcolorbox}

\begin{document}

\begin{figure}[ht]
\centering
\begin{tcolorbox}[sidebyside,
sidebyside align=top, 
halign=flush center,
%blankest % uncomment to remove colorbox
]
\centering
\begin{lstlisting}
here is some code
that is not as long
as the other code
\end{lstlisting}
\tcblower
\begin{lstlisting}
here is some code
that is a bit longer
than the other code
so I would like the
two listings to be
vertically aligned
at the top
\end{lstlisting}
\end{tcolorbox}
\caption{test}
\end{figure}

\end{document}

The blankest option of tcolorbox can be used if you don't want the colorbox. Output with blankest option:

enter image description here

0

Just use a [t]op-aligned varwidth with the lstlisting inside:

enter image description here

\documentclass{article}

\usepackage{listings,varwidth}

\begin{document}

\begin{figure}
  \mbox{}\hfill
  \begin{varwidth}[t]{0.45\linewidth}
    \begin{lstlisting}
here is some code
that is not as long
as the other code
    \end{lstlisting}
  \end{varwidth}\hfill
  \begin{varwidth}[t]{0.45\linewidth}
    \begin{lstlisting}
here is some code
that is a bit longer
than the other code
so I would like the
two listings to be
vertically aligned
at the top
    \end{lstlisting}
  \end{varwidth}\hfill\mbox{}%
  \caption{test}
\end{figure}

\end{document}
1
  • Hmm, I'm not getting the same output as you even though I've copied and pasted what you provided and only added the showframe package and frame=lr. I'm still getting the code left-aligned. i.imgur.com/bjT5laI.png
    – Anonymous
    Commented Oct 27, 2019 at 2:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .