将两个并排列表居中并垂直对齐

将两个并排列表居中并垂直对齐

我有两个列表,我想将它们并排放置在图中。其中一个列表比另一个长。我希望两个列表都水平居中,并在顶部垂直对齐(这样,在下图中,第一个代码块将向上移动到大约接触红线的位置)。我一直将我的列表环境括起来以使其\begin{tabular}{c} ... \end{tabular}水平居中,并将它们放在两个minipages 中以使其并排,但尝试通过添加[t]到小页面来垂直对齐它们似乎不适用于tabular环境。如果我删除tabular环境,我可以像我想要的那样垂直对齐两个小页面,但列表不再居中。

梅威瑟:

\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}

MWE 的输出

答案1

我认为这可能不是最简单的解决方案,但它是有效的。

\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}

在此处输入图片描述

答案2

您可以使用 tcolorbox将内容并排放置。以下是示例:

在此处输入图片描述

\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}

如果您不想要颜色框,可以使用blankest选项。使用选项输出:tcolorboxblankest

在此处输入图片描述

答案3

只需使用与内部[t]对齐的操作:varwidthlstlisting

在此处输入图片描述

\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}

相关内容