Saturday, September 4, 2010

Computing the maximum width of two boxes

On occasion, one has cause to compare two boxes and compute the maximum of the two widths. If the two boxes are 0 and 2, then the standard way to do that is something like
\ifdim\wd0>\wd2
    \dimen0 \wd0
\else
    \dimen0 \wd2
\fi
Of course, this works perfectly well. But sometimes, you want something that's a little less easy to read. Thus, I propose
\dimen0 \wd\ifdim\wd0>\wd2 0 \else2 \fi
The spaces around the numbers are to keep TeX from reading more input looking for additional digits.

1 comment:

  1. There is another way calculate the maximum width of any number of boxes.

    \setbox0=\hbox{\kern 26pt}
    \setbox1=\hbox{\kern 16pt}
    \setbox2=\hbox{\kern 33pt}
    \setbox3=\vbox{\copy0 \copy1 \copy2}
    \showthe\wd3

    ReplyDelete