# How to match codes with different lines with minipage and lstlisting?

I have 2 different codes that I am displaying side by side, one is significantly larger than the other. I'd like to add spacing to the small code so that their background boxes match identically, i'd also like to know how to make it so that the small text is at the same height as the big text.

So what I have is:

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


Resulting in:

What I wish is for that left code to be displayed at the top, at the same height as the right code and then append black space until it matches the right code. Completely unrelated, if you want to criticize the color scheme I am using go ahead, I encourage you.

You can typeset the long listing inside a \vbox and then typeset the short listing inside a second \vbox that has the same height as the first. Then you can use \colorbox from the xcolor package to color the background of both listings.

You didn't give the c style and rather than duplicate it, I just went with a yellow background as in Mike's answer

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


Some thoughts:

• Note that this solution does not use minipage. In my experience, minipage is almost never useful.
• This uses \vbox and \hbox directly rather than higher-level LaTeX commands. The reason for this is that although there is a LaTeX versions of \hbox (namely \mbox or \makebox), \vbox (namely \parbox and minipage), and \setbox (namely \savebox), \savebox only works for horizontal boxes and we need to save a vertical box. However, the second \vbox (and the corresponding named box register \shortbox could be omitted and a \parbox could be used directly. The \hbox could be replaced with \makebox. But mixing the TeX primitives and the LaTeX macros looks strange to me.
• I have been testing this and it's almost good enough. But if I add a title to the listings it will be inside the box, and since the box it's black it will not be visible, how could I make it so that the box starts below the tile of the listings? – Makogan Jun 28 '17 at 23:53
• You may be better off just creating two text files that have the same number of lines and using \lstinputlisting with the appropriate options to color the background. – TH. Jun 29 '17 at 2:38

Well, if you decide to have recourse to low-level TeX commands, as in @TH.’s answer, there’s no need to store the two listings into temporary boxes, neither—and this is much more important—to know in advance which of them is the longest one:

% 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,
}

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


This is the page you obtain in the output:

Note, however, that this approach too suffers from the problem the OP complains about in this comment.

• Using a \vrule here is quite clever! – TH. Jun 29 '17 at 22:41

Since you want to extend the background box (I guess it's done by the lstlistings environment), you have to extend the first code by enough empty line to match the long one and add the option showlines to the lstlistings environment of the short code, so the empty lines are printed.

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


Your color scheme: well, it may look good on screen, but I doubt it looks good on paper. Depending on the printer the text may be barely visible.

Edit: Another solution without the ugly empty lines is to set the height of the minipage and add a \rule with the necessary height. But it still requires counting the lines. The main downside is the \vspace{-7pt}, which was determined experimentally and may have to be changed depending on the document class, loaded packages and/or setup.

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

• adding white space like that seems, ugly... as far as the source code goes :p – Makogan Jun 27 '17 at 23:27
• I removed that huge white space, my eyes stumbled at it at a first sight. – AboAmmar Jun 27 '17 at 23:38
• @AboAmmar: well, they were there for a reason, because without them the background box doesn't have the same height as the one for the long code (as asked by the OP). So don't mind if I put them back in. – Mike Jun 27 '17 at 23:55
• @Makogan: Yes. I added another solution, which has another downside (see edit). – Mike Jun 28 '17 at 0:09
• Is there a way to measure the size of a mini page? that'd probably be enough to set up the other one no? – Makogan Jun 28 '17 at 0:21