# Aligning two minipages - one with lstlisting, one without - on top?

I'm already aware that two minipages with [t] align at the baseline of the top line, not at the top of their respective boxes/containers; however, this is somewhat different: when I use a small minipage with one letter, and a minipage with lstlisting, and try to align them on top as on the below MWE, I get this (using lua-visual-debug): I really don't understand - what is that pushing the listing minipage down, a linebreak? Where did that come from? How could I get these two items aligned on top (such that the A remains where it is - and the listing+minipage move vertically up to match it)?

By the way, notice that in the MWE below - the second example, with two minipages with lstlisting each, works without a problem:

\documentclass{article}

%\usepackage{lua-visual-debug}
\usepackage{listings}

\begin{document}

Test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test ...

\begin{minipage}[t]{1em}
A
\end{minipage}
\hfill
\begin{minipage}[t]{\dimexpr\textwidth-2\parindent-2.2em}%
\begin{lstlisting}
sslg kaha kshd kajshd
asl q7ey q[peo [pdv
sdfk a9su qpowie e
asj asy dqoi poai
\end{lstlisting}
\end{minipage}

Test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test ...

% http://tex.stackexchange.com/questions/36712/how-to-place-two-code-/36737
\begin{minipage}[t]{0.45\linewidth}
\begin{lstlisting}[caption={Some XML Caption}]
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
\end{lstlisting}
\end{minipage}
%
\begin{minipage}[t]{0.45\linewidth}
\begin{lstlisting}[caption={Some Javascript Caption}]
... javascript code ...
... javascript code ...
... javascript code ...
\end{lstlisting}
\end{minipage}

\end{document}


The listings way is to use aboveskip and belowskip which are initially set to \medskipamount:

\begin{lstlisting}[aboveskip=-1.4\medskipamount]


Code:

\documentclass{article}

\tracingonline1
\tracingoutput1
\tracingparagraphs1
\usepackage{lua-visual-debug}
\usepackage{listings}
\usepackage{unravel}
\makeatletter
% \def\rst{\gdef\everypar={\setbox \z@ \lastbox }}
% \def\rst{\gdef\everypar##1{##1\unvbox}}
\def\rst{\leavevmode\unskip \lastbox\hfill} % \unvbox \z@ \lastbox \leavevmode \unhbox\z@
\makeatother

\begin{document}

Test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test ...

\begin{minipage}[t]{1em}
A
% \rst% \unravel{\bgroup
\end{minipage}
\hfill
% \rst
\begin{minipage}[t]{\dimexpr\textwidth-2\parindent-2.2em}%
% }
%\vspace{-1.2\baselineskip}
\begin{lstlisting}[aboveskip=-1.4\medskipamount]
sslg kaha kshd kajshd
asl q7ey q[peo [pdv
sdfk a9su qpowie e
asj asy dqoi poai
\end{lstlisting}
\end{minipage}

Test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test ...

% http://tex.stackexchange.com/questions/36712/how-to-place-two-code-/36737
\begin{minipage}[t]{0.45\linewidth}
\begin{lstlisting}[caption={Some XML Caption}]
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
\end{lstlisting}
% \unravel{\bgroup
\end{minipage}
%
\begin{minipage}[t]{0.45\linewidth}
% }
\begin{lstlisting}[caption={Some Javascript Caption}]
... javascript code ...
... javascript code ...
... javascript code ...
\end{lstlisting}
\end{minipage}

\end{document} • Thanks for that, @HarishKumar - I was not aware there was a listings-specific approach. Cheers! Jul 2, 2014 at 15:32

Well, I got to a manual workaround, which is to use \vspace{-1.2\baselineskip} right after the second {minipage} starts; the output is then: ... which, I guess, is what the [t] + [t] would have been expected to do.

I used the unravel package to step through the critical sections, and compare those logs in meld; the problem seems to reduce to \everypar being different in the two runs (see comments below) - so I experimented with some commands that popped out there (\unvskip, \lastbox - all commands I don't understand and cannot find a quick reference to), but I couldn't control them - so in the end I just added a \vspace in there, and it seemed to work (I've tried \vspace before that, too, but it didn't work as I placed it elsewhere.

Anyways, the code is:

\documentclass{article}

\tracingonline1
\tracingoutput1
\tracingparagraphs1
\usepackage{lua-visual-debug}
\usepackage{listings}
\usepackage{unravel}
\makeatletter
% \def\rst{\gdef\everypar={\setbox \z@ \lastbox }}
% \def\rst{\gdef\everypar##1{##1\unvbox}}
\def\rst{\leavevmode\unskip \lastbox\hfill} % \unvbox \z@ \lastbox \leavevmode \unhbox\z@
\makeatother

\begin{document}

Test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test ...

\begin{minipage}[t]{1em}
A
% \rst% \unravel{\bgroup
\end{minipage}
\hfill
% \rst
\begin{minipage}[t]{\dimexpr\textwidth-2\parindent-2.2em}%
% }
\vspace{-1.2\baselineskip}
\begin{lstlisting}
sslg kaha kshd kajshd
asl q7ey q[peo [pdv
sdfk a9su qpowie e
asj asy dqoi poai
\end{lstlisting}
\end{minipage}

Test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test sentence test ...

% http://tex.stackexchange.com/questions/36712/how-to-place-two-code-/36737
\begin{minipage}[t]{0.45\linewidth}
\begin{lstlisting}[caption={Some XML Caption}]
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
.. xml code ...
\end{lstlisting}
% \unravel{\bgroup
\end{minipage}
%
\begin{minipage}[t]{0.45\linewidth}
% }
\begin{lstlisting}[caption={Some Javascript Caption}]
... javascript code ...
... javascript code ...
... javascript code ...
\end{lstlisting}
\end{minipage}

\end{document}

% diff via unravel 1:
% <| {\par }{}\begingroup \setbox 12=\vbox {\par }\vtop {}\@end@tempboxa
% <| \endgroup  \begingroup \setbox 12=\vbox { ok

% <| {\par }\begingroup \setbox 12=\vbox {\par }\vtop {}\@end@tempboxa
% <| \endgroup  \begingroup \setbox 12=\vbox { nok

% diff via unravel 2 - from:
% <| {\par }
% ||
% |> \unhbox \voidb@x

% to:
% [===== Step 24 =====] \unhbox = \unhbox : \everypar={}
% <| {\par }
% ||
% |> \unhbox \voidb@x \@pboxswfalse \setlength \@tempdima

% [===== Step 37 =====] \unhbox = \unhbox : \everypar={{\setbox \z@ \la...
% <| {\par }
% ||
% |> {\setbox \z@ \lastbox }\everypar {}\@endpefalse \unhbox \voidb@x
{"ft_lang_label":"__label__en","ft_lang_prob":0.6860187,"math_prob":0.43173277,"size":1882,"snap":"2023-40-2023-50","text_gpt3_token_len":511,"char_repetition_ratio":0.2630458,"word_repetition_ratio":0.26666668,"special_character_ratio":0.25557917,"punctuation_ratio":0.20652173,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96341133,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-28T08:03:12Z\",\"WARC-Record-ID\":\"<urn:uuid:885f189e-c6db-4cb4-82dc-090492d3c72b>\",\"Content-Length\":\"155229\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f9a0629a-d954-456e-81f6-9342838b94ad>\",\"WARC-Concurrent-To\":\"<urn:uuid:011a682f-8869-44bc-a501-6a52a7693375>\",\"WARC-IP-Address\":\"104.18.10.86\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/187558/aligning-two-minipages-one-with-lstlisting-one-without-on-top\",\"WARC-Payload-Digest\":\"sha1:KDNCOHL3PZD43ZE5VSGPNT7GP75EWGY3\",\"WARC-Block-Digest\":\"sha1:6VX62MQNIVXJCPREV6RBPSYLDLL4IVFF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510368.33_warc_CC-MAIN-20230928063033-20230928093033-00801.warc.gz\"}"}