79

When a minipage is used to box a text paragraph, the first line of the minipaged text and the last line of the previous text paragraph are closer than two consecutive lines in a standard text paragraph; analogously, the last line of text inside the minipage and the first line of text after the minipage are also closer.

The following MWE illustrates this:

\documentclass{article}
\usepackage{lipsum}

\newcommand\Text{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.\textbullet}

\begin{document}

\Text

\noindent\begin{minipage}{\linewidth}
\Text
\end{minipage}

\Text

\end{document}

How can I guarantee a constant baseline-skip even if minipages (or \parboxes) are used?

enter image description here

3
  • Good question. Doesn't adding the [b] option to minipage means to align the bottom of the baselines? Nov 15, 2011 at 21:19
  • that helps only for the base of the minipage
    – user2478
    Nov 15, 2011 at 21:32
  • 1
    After @egreg's answer, this seems related: tex.stackexchange.com/questions/15849/…
    – Werner
    Nov 15, 2011 at 22:31

2 Answers 2

62

For the top use the [t] specification. For the bottom you can reinstate after the minipage the depth of the last row

\documentclass{article}
\usepackage{lipsum}

\newcommand\Text{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.\textbullet}

\begin{document}

\hrule

\begin{minipage}{.45\textwidth}
\Text

\noindent\begin{minipage}[t]{\linewidth}
\Text
\par\xdef\tpd{\the\prevdepth}
\end{minipage}

\prevdepth\tpd
\Text
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\Text

\Text

\Text
\end{minipage}
\hrule
\end{document}

enter image description here

Explanation

When TeX stacks boxes in vertical mode (for example after having finished a paragraph) it inserts interline glue with the purpose of keeping successive baselines a fixed distance apart from each other.

Let's keep as example the standard value of 12pt for the baseline skip b (the fixed distance) for a document at 10pt size. What TeX does when it stacks to boxes is:

  • measure the depth d of the top box, that is, how much characters in it stick below the baseline;

  • measure the height h of the bottom box, that is, how much characters in it stick above the baseline;

  • compute b - h - d.

Then TeX looks at another parameter, the lineskip limit, let's call it l. If b - h - dl, a vertical space (glue) of length b - h - d is inserted between the two boxes. The lineskip limit tells TeX when two boxes are to be considered "too near to each other": in fact, if b - h - d < l, TeX will insert between the two boxes a vertical space of length "lineskip" (another parameter).

However, TeX considers boxes like a minipage as if they were big characters, so they influence the calculations above.

When a minipage is built, its height and depth will be equal to half the total size of the minipage. But if the [t] option, the height will be the same as the height of the first line inside it and all the rest will contribute to the depth.

Therefore, when we have

\Text

\noindent\begin{minipage}{\linewidth}
\Text
\end{minipage}

\Text

the calculation above will certainly give a negative value for b - h - d, because the minipage has a very large depth. So the \lineskip glue will be inserted. There will be no problem above the minipage, because the height TeX will consider is exactly the height of the first line inside.

There's an escape out. Indeed, TeX doesn't really use d, but the value of an internal parameter called \prevdepth which is set to the depth of the last box in a "vertical list" and can be changed.

Thus we measure the value of d for the last line inside the minipage by saying \par (that will terminate the paragraph and contribute it to the vertical list of the minipage) and keep this value inside \tpd:

\xdef\tpd{\the\prevdepth}

stores globally in \tpd the value of \prevdepth. It's necessary to do this globally, as a minipage forms a group (the same would be in a \parbox). Then, before saying \Text, that is, beginning a paragraph, we reinstate the parameter \prevdepth to have the value computed before. Thus TeX will behave as if a box of depth d were above the first line of the new paragraph, forgetting about the big depth of the minipage.

Therefore, the computation of the interline glue will be the same as if the last line of the minipage were above the first line of the paragraph.

6
  • 1
    I'm get frequently amazed by the features of TeX which I stilled did not discovered so far! I really have to read The TeXBook again more closely. I skipped some parts the first time around. Nov 16, 2011 at 11:56
  • @GonzaloMedina You might like the explanation I added.
    – egreg
    Nov 22, 2011 at 17:47
  • @egreg: of couse I like it; I also appreciate you taking the time to write it. Thank you very much! Nov 23, 2011 at 17:09
  • 6
    @MartinScharrer these things look more like bugs and intractable intricacies to me than features.
    – flow
    Nov 25, 2014 at 18:04
  • @egreg. I'm reading this great explanation to figure out the same question of the OP. Was the definition of the value b missed defining?
    – Eric
    Mar 11, 2017 at 13:12
13

Add a \strut at either end of \Text, that usually helps.

4
  • This solution is not perfect, but as you said, it "usually helps".
    – Werner
    Nov 15, 2011 at 21:32
  • @Werner: why is it not perfect? Also IMHO there was no reason to delete your answer. Nov 15, 2011 at 21:39
  • @MartinScharrer: I deleted my answer since @ daleif (at lower reputation) suggested the same (albeit later) and received upvotes, so I felt it duplicated an answer which was unnecessary. It is not perfect since you'll notice a difference in the gaps between the paragraphs if you compile my deleted MWE (as suggested at the end of my answer).
    – Werner
    Nov 15, 2011 at 21:43
  • @daleif: Thanks. I knew about adding \strut; as you said, it helps but it doesn't produce the precise value I was looking for. Nov 15, 2011 at 23:34

You must log in to answer this question.

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