如何使用 minipage 和 lstlisting 来匹配不同行的代码?

如何使用 minipage 和 lstlisting 来匹配不同行的代码?

我有两个不同的代码并排显示,其中一个比另一个大得多。我想给小代码添加间距,以便它们的背景框完全匹配,我还想知道如何使小文本与大文本处于相同的高度。

所以我有的是:

\begin{minipage}{0.5\textwidth}
    \begin{lstlisting}[style=c]
        void example()
        {
            int i = 0;

            i = 500;
        }
    \end{lstlisting}
\end{minipage}
\begin{minipage}{0.5\textwidth}
    \begin{lstlisting}[style=c]
    struct structure
    {
    unsigned int f1;
    unsigned int f2;
    unsigned int f3;
    unsigned int f4;
    unsigned int f5;
    unsigned int f6;
    unsigned int f7;
    unsigned int f8;
    unsigned int f9;
    unsigned int f10;
    unsigned int f11;
    };

    struct structure example =
    {
    .f1 = 0xe52db004,
    .f2 = 0xe28db000,
    .f3 = 0xe24dd00c,
    .f4 = 0xe3a03000,
    .f5 = 0xe50b3008,
    .f6 = 0xe3a03f7d,
    .f7 = 0xe50b3008,
    .f8 = 0xe1a00000,                     
    .f9 = 0xe28bd000,
    .f10= 0xe49db004,   
    .f11= 0xe12fff1e,
    };
    \end{lstlisting}
\end{minipage}

导致:

在此处输入图片描述

我希望左边的代码显示在顶部,与右边的代码高度相同,然后添加黑色空格,直到与右边的代码匹配。完全不相关,如果你想批评我使用的配色方案,请继续,我鼓励你。

答案1

好吧,如果你决定使用低级 TeX 命令,比如@TH. 的回答,没有必要将这两个列表存储到临时框中,也没有必要提前知道哪一个是最长的——这一点更为重要:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{xcolor}
\usepackage{listings}

\definecolor{OrdinaryColor} {named}{white}
\definecolor{KeyWordColor}  {named}{cyan}
\definecolor{DirectiveColor}{named}{pink}
\definecolor{StringColor}   {named}{yellow}
\definecolor{CommentColor}  {named}{brown}
\definecolor{BackGrndColor} {named}{darkgray}

\lstdefinestyle{myCstyle}{
    language        = [ANSI]C,
    basicstyle      = \ttfamily\small\color{OrdinaryColor},
    columns         = fullflexible,
    keepspaces      = true,
    basewidth       = {\fontdimen2 \font},
    tabsize         = 4,
    aboveskip       = 4pt,
    belowskip       = 4pt,
    xleftmargin     = 4pt,
    % xrightmargin  = 4pt, % had to comment it out -- one of listing's mysteries!
    keywordstyle    = \color{KeyWordColor},
    directivestyle  = \color{DirectiveColor},
    stringstyle     = \color{StringColor}\slshape,
    commentstyle    = \color{CommentColor}\slshape
}

\newlength{\listingFrameWidth} % not strictly necessary



\begin{document}

\lstset{style = myCstyle}

Some text above the listings.

\nobreak \bigskip \vspace{-\baselineskip}

\hbox to \columnwidth \bgroup % make a full-width line
\setlength{\listingFrameWidth}{.45\columnwidth}%
% First typeset the backgrounds:
\begingroup
    \color{BackGrndColor}%
    \vrule width \listingFrameWidth
    \hfill
    \vrule width \listingFrameWidth \relax
    \hspace{-2\listingFrameWidth plus -1fill}% backspace to beginning of line
\endgroup
% Then overprint the two listings
\begin{minipage}[t]{\listingFrameWidth}
    \begin{lstlisting}
void example()
{
    int i = 0;

    i = 500;
}
    \end{lstlisting}
\end{minipage}% <-- beware of stray spaces!
\hfill
\begin{minipage}[t]{\listingFrameWidth}
    \begin{lstlisting}
struct structure
{
    unsigned int f1;
    unsigned int f2;
    unsigned int f3;
    unsigned int f4;
    unsigned int f5;
    unsigned int f6;
    unsigned int f7;
    unsigned int f8;
    unsigned int f9;
    unsigned int f10;
    unsigned int f11;
};

struct structure example =
{
    .f1 = 0xe52db004,
    .f2 = 0xe28db000,
    .f3 = 0xe24dd00c,
    .f4 = 0xe3a03000,
    .f5 = 0xe50b3008,
    .f6 = 0xe3a03f7d,
    .f7 = 0xe50b3008,
    .f8 = 0xe1a00000,                     
    .f9 = 0xe28bd000,
    .f10= 0xe49db004,   
    .f11= 0xe12fff1e,
};
    \end{lstlisting}
\end{minipage}% <-- beware of stray spaces!
\egroup % ends the line

\bigskip

% % Uncomment the following 4 lines to check the details:
% \showboxbreadth = 10000
% \showboxdepth = 2
% \tracingonline = 1
% \showlists

And some text below.

\bigbreak

Again, but this time the first column is the tallest one.

\nobreak \bigskip \vspace{-\baselineskip}

\hbox to \columnwidth \bgroup
\setlength{\listingFrameWidth}{.45\columnwidth}%
\begingroup
    \color{BackGrndColor}%
    \vrule width \listingFrameWidth
    \hfill
    \vrule width \listingFrameWidth
    \hspace{-2\listingFrameWidth plus -1fill}%
\endgroup
\begin{minipage}[t]{\listingFrameWidth}
    \begin{lstlisting}
/*
 *  Example function
 */

#include <stdio.h>

void example()
{
    int i = 0;

    i = 500;
    printf("Hello, world!")
}
    \end{lstlisting}
\end{minipage}%
\hfill
\begin{minipage}[t]{\listingFrameWidth}
    \begin{lstlisting}
void example()
{
    int i = 0;

    i = 500;
}
    \end{lstlisting}
\end{minipage}%
\egroup

\bigskip

Some text below.

\end{document}

这是您在输出中获得的页面:

代码输出

但请注意,这种方法也存在 OP 抱怨的问题此评论

答案2

您可以在 a 中排版长列表,然后在与第一个高度相同的\vbox第二个中排版短列表。然后,您可以使用包中的颜色为两个列表的背景着色。\vbox\colorboxxcolor

你没有给出c样式,所以我没有复制它,而是选择了黄色背景,如下所示迈克的回答

\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{fullpage}
\lstset{language=C}

\newbox\longbox
\newbox\shortbox

\begin{document}
\setbox\longbox=\vbox{
    \hsize=.45\linewidth
    \begin{lstlisting}[linewidth=\hsize]
    struct structure
    {
    unsigned int f1;
    unsigned int f2;
    unsigned int f3;
    unsigned int f4;
    unsigned int f5;
    unsigned int f6;
    unsigned int f7;
    unsigned int f8;
    unsigned int f9;
    unsigned int f10;
    unsigned int f11;
    };

    struct structure example =
    {
    .f1 = 0xe52db004,
    .f2 = 0xe28db000,
    .f3 = 0xe24dd00c,
    .f4 = 0xe3a03000,
    .f5 = 0xe50b3008,
    .f6 = 0xe3a03f7d,
    .f7 = 0xe50b3008,
    .f8 = 0xe1a00000,
    .f9 = 0xe28bd000,
    .f10= 0xe49db004,
    .f11= 0xe12fff1e,
    };
    \end{lstlisting}
    \hrule height0pt
}
\setbox\shortbox=\vbox to\ht\longbox{
    \hsize=.45\linewidth
    \begin{lstlisting}[linewidth=\hsize]
    void example()
    {
        int i = 0;

        i = 500;
    }
    \end{lstlisting}
    \vfill
}
\noindent
\hbox to\linewidth{%
        \colorbox{yellow}{\box\shortbox}%
        \hfil
        \colorbox{yellow}{\box\longbox}%
}

\end{document}

在此处输入图片描述

一些想法:

  • 请注意,此解决方案不使用minipage。根据我的经验,minipage几乎从来没有用过。
  • 这直接使用\vbox和,\hbox而不是更高级的 LaTeX 命令。这样做的原因是,尽管有\hbox(即\mbox\makebox)、\vbox(即\parboxminipage) 和\setbox(即\savebox)的 LaTeX 版本,\savebox但仅适用于水平框,而我们需要保存垂直框。但是,可以省略第二个\vbox(和相应的命名框寄存器,并直接使用。可以用代替。但混合 TeX 基元和 LaTeX 宏对我来说看起来很奇怪。\shortbox\parbox\hbox\makebox
  • 有关盒子的更多信息,请参阅 egreg 的杰出作品回答

答案3

由于要扩展背景框(我猜是环境完成的lstlistings),所以必须将第一个代码扩展足够的空行以匹配长代码,并将选项添加showlineslstlistings短代码的环境中,这样就会打印空行。

\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{language=C,backgroundcolor=\color{yellow}}

\begin{document}
\begin{minipage}{0.45\textwidth}
    \begin{lstlisting}[showlines]
        void example()
        {
            int i = 0;

            i = 500;
        }























    \end{lstlisting}
\end{minipage}
\begin{minipage}{0.45\textwidth}
    \begin{lstlisting}
    struct structure
    {
    unsigned int f1;
    unsigned int f2;
    unsigned int f3;
    unsigned int f4;
    unsigned int f5;
    unsigned int f6;
    unsigned int f7;
    unsigned int f8;
    unsigned int f9;
    unsigned int f10;
    unsigned int f11;
    };

    struct structure example =
    {
    .f1 = 0xe52db004,
    .f2 = 0xe28db000,
    .f3 = 0xe24dd00c,
    .f4 = 0xe3a03000,
    .f5 = 0xe50b3008,
    .f6 = 0xe3a03f7d,
    .f7 = 0xe50b3008,
    .f8 = 0xe1a00000,                     
    .f9 = 0xe28bd000,
    .f10= 0xe49db004,   
    .f11= 0xe12fff1e,
    };
    \end{lstlisting}
\end{minipage}
\end{document}

顺便说一句:请确保您的 LaTeX 代码完整平均能量损失下次。

您的配色方案:好吧,它可能在屏幕上看起来不错,但我怀疑它在纸上看起来是否好看。根据打印机的不同,文本可能几乎不可见。

编辑:另一种没有难看的空行的解决方案是设置的高度minipage并添加\rule具有必要高度的。但这仍然需要计算行数。主要缺点是\vspace{-7pt},它是通过实验确定的,可能需要根据文档类、加载的包和/或设置进行更改。

\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{language=C,backgroundcolor=\color{yellow}}

\begin{document}
\begin{minipage}[t][29\baselineskip]{0.45\textwidth}
    \begin{lstlisting}
        void example()
        {
            int i = 0;

            i = 500;
        }
    \end{lstlisting}
    \vspace{-7pt} % may be a problem
    \textcolor{yellow}{\rule{\linewidth}{23\baselineskip}}
\end{minipage}
\begin{minipage}[t]{0.45\textwidth}
    \begin{lstlisting}
    struct structure
    {
    unsigned int f1;
    unsigned int f2;
    unsigned int f3;
    unsigned int f4;
    unsigned int f5;
    unsigned int f6;
    unsigned int f7;
    unsigned int f8;
    unsigned int f9;
    unsigned int f10;
    unsigned int f11;
    };

    struct structure example =
    {
    .f1 = 0xe52db004,
    .f2 = 0xe28db000,
    .f3 = 0xe24dd00c,
    .f4 = 0xe3a03000,
    .f5 = 0xe50b3008,
    .f6 = 0xe3a03f7d,
    .f7 = 0xe50b3008,
    .f8 = 0xe1a00000,                     
    .f9 = 0xe28bd000,
    .f10= 0xe49db004,   
    .f11= 0xe12fff1e,
    };
    \end{lstlisting}
\end{minipage}
\end{document}

相关内容