4

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):

test02b.png

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}

2 Answers 2

1

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}

enter image description here

1
  • Thanks for that, @HarishKumar - I was not aware there was a listings-specific approach. Cheers!
    – sdaau
    Commented Jul 2, 2014 at 15:32
1

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:

test02c.png

... 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

You must log in to answer this question.

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