3

I would like to make all vertical spaces between lines in a minipage the same. I would like to make each as much as the maximum. I have found ways to stop the one creating extra space by using \smash as suggested here:

How to force normal line spacing when inline math in text

and here:

How does one stop automatic line spacing increases when typesetting tall math symbols?

but I prefer to increase the other lines, but not to do so manually (e.g. hardcode a certain length).

Is this possible?

Below I have a MWE:

\documentclass{article}
\begin{document}
\begin{minipage}[t]{1\columnwidth}%
Just some normal text before the math. Go on a bit of a bit longer so
that this wraps into a couple of lines. I wish I had a funny joke to
tell you but I cant remember any. For example, 
$\widehat{\Omega_{YOU}^{\hat{XYZ}}}=DYNAMICS$ is a model that I would
like to examine. OK now lets do some more filler text here to see
another line and check out the spacing. If you read this far you
deserve an award.
\end{minipage}
\end{document}
3

The following example defines a macro \MaximizeBaselineskip, which puts the whole text in a horizontal box and measures the width and height to get an upper limit for the \baselineskip:

\documentclass{article}

\newcommand{\MaximizeBaselineskip}[1]{%
  \sbox0{#1}%
  \baselineskip=\dimexpr\ht0+\dp0\relax
  #1%
}

\begin{document}
\noindent
\begin{minipage}[t]{1\columnwidth}%
\MaximizeBaselineskip{%
Just some normal text before the math. Go on a bit of a bit longer so
that this wraps into a couple of lines. I wish I had a funny joke to
tell you but I cant remember any. For example,
$\widehat{\Omega_{YOU}^{\hat{XYZ}}}=\mathit{DYNAMICS}$
is a model that I would
like to examine. OK now lets do some more filler text here to see
another line and check out the spacing. If you read this far you
deserve an award.}%
\end{minipage}
\end{document}

Result

Optimized version

Starting from the \baselineskip, found above, smaller value can be tested. Depending on the previous box depth, the maximum value might not be necessary. The following example implements a binary search to find a smaller value.

\documentclass{article}

\usepackage{environ}

\makeatletter
\newdimen\minmax@baselineskip
\newif\ifminmax@todo@
\NewEnviron{minmaxminipage}{%
  \begingroup
    \toks@=\expandafter{\@parboxrestore}%
    \let\@minmaxminipage@baselineskip\relax
    \minmax@baselineskip=\normalbaselineskip
    \edef\@parboxrestore{%
      \the\toks@
      \lineskiplimit=0pt %
      \lineskip=10in %
      \baselineskip=\minmax@baselineskip\relax
    }%
    \edef\TESTBODY{%
      \noexpand\begin{minmax@test}%
      \unexpanded\expandafter{\BODY}%
      \noexpand\end{minmax@test}%
    }%
    \edef\BODY{%
      \noexpand\begin{minipage}%
      \unexpanded\expandafter{\BODY}%
      \noexpand\end{minipage}%
    }%
    \sbox0{\TESTBODY}%
    \minmax@baselineskip=\dimexpr\ht0+\dp0\relax
    \typeout{* Max: \the\minmax@baselineskip}%
    \edef\minmax@upper{\the\minmax@baselineskip}%
    \def\minmax@lower{0pt}%
    \sbox0{\BODY}%
    \edef\minmax@HT{\the\dimexpr\ht0+\dp0\relax}%
    % \typeout{* Tr0: \the\minmax@baselineskip\space -> \minmax@HT}%
    \minmax@todo@true
    \loop
    \ifminmax@todo@
      \minmax@baselineskip=.5\dimexpr\minmax@upper+\minmax@lower\relax
      \sbox0{\BODY}%
      \edef\minmax@new@HT{\the\dimexpr\ht0+\dp0\relax}%
      % \typeout{* Try: \the\minmax@baselineskip\space -> \minmax@new@HT}%
      \ifdim\minmax@new@HT>\minmax@HT\relax
        \edef\minmax@lower{\the\minmax@baselineskip}%
        % \typeout{* Low: \minmax@lower}%
      \else
        \ifdim\minmax@new@HT<\minmax@HT\relax
          \edef\minmax@upper{\the\minmax@baselineskip}%
          % \typeout{* Upp: \minmax@upper}%
          \let\minmax@HT\minmax@new@HT
        \else
          \minmax@todo@false
        \fi
      \fi
      \ifdim\minmax@upper=\minmax@baselineskip 
        \minmax@todo@false
      \fi
    \repeat
    \typeout{* Min: \the\minmax@baselineskip}%
    \unhcopy0 
  \endgroup
}
\newenvironment{minmax@test}{}{}
\def\minmax@test#1#{\@gobble}%
\makeatother

\begin{document}
\noindent
\begin{minmaxminipage}[t]{1\columnwidth}%
Just some normal text before the math. Go on a bit of a bit longer so
that this wraps into a couple of lines. I wish I had a funny joke to
tell you but I cant remember any. For example, 
$\widehat{\Omega_{YOU}^{\hat{XYZ}}}=\mathit{DYNAMICS}$
is a model that I would
like to examine. OK now lets do some more filler text here to see
another line and check out the spacing. If you read this far you
deserve an award.%
\end{minmaxminipage}
\end{document}

A smaller value for \baselineskip could be found:

* Max: 16.20827pt
* Min: 15.70175pt

Result minmax

  • What if there are two consecutive lines with excess height or depth and (by Murphy's law) the two big objects are above one another? The interline might not be uniform. You should probably add \lineskip to be sure and set \lineskiplimit to \maxdimen. – egreg Jul 3 '15 at 16:41
  • @egreg By putting the text into an \hbox I automatically get the maximum depth and height, thus with the default zero \lineskiplimit always \baselineskip is set and \lineskip is never used. In the updated answer I have improved this by making \baselineskip as small as possible without setting \lineskip. – Heiko Oberdiek Jul 3 '15 at 16:51
  • I was just thinking to that: if a line having maximum depth is followed by one having maximum height, there are chances the big objects will touch. On the other hand, I believe the problem is a false one: the baseline skip should be set once and for all. – egreg Jul 3 '15 at 16:56
  • This is great. Thank you for such an elegant answer. I will study and learn from this code. – Xu Wang Jul 3 '15 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.