4

Is there a general formula for offsets of this kind? Or is this a minor bug in the compiler used? The version of pdfTeX (pdflatex) is 1.40.24.

This book states that \parbox[c]{width}{text} vertically aligns the box so that the baseline of the outer text and the baseline of the inner text's middle line coincide. However, they do not.

The following minimal working example is composed to illustrate the mentioned issue.

\documentclass[class=article, 12pt,
               border=1bp]
              {standalone}
\usepackage[utf8]{inputenc}

\def\myrule#1{\rule[-.3\normalbaselineskip]
                   {#1}
                   {\normalbaselineskip}}

\begin{document}

\parbox[t]{1ex}{%
 \myrule{1ex}\\\myrule{1ex}\\\myrule{1ex}}%
\myrule{1ex}%
\parbox[b]{1ex}{%
 \myrule{1ex}\\\myrule{1ex}\\\myrule{1ex}}%
\myrule{1ex}%
\parbox[c]{1ex}{%
 \hspace*{.5ex}\myrule{.5ex}\\%
 \myrule{1ex}\\%
 \hspace*{.5ex}\myrule{.5ex}}%

\end{document}

Here is the rendering of the MWE showing a slight offset between the two baselines. The offset is approximately .007\normalbaselineskip.

MWE rendering, misalignment of the \parbox[c]

Additionally, the generated PDF file contains the following lines:

q
1 0 0 1 1 29.892 cm
0 0 5.147 14.446 re f
Q
q
1 0 0 1 1 15.446 cm
0 0 5.147 14.446 re f
Q
q
1 0 0 1 1 1 cm
0 0 5.147 14.446 re f
Q
q
1 0 0 1 6.147 29.892 cm
0 0 5.147 14.446 re f
Q
q
1 0 0 1 11.295 58.783 cm
0 0 5.147 14.446 re f
Q
q
1 0 0 1 11.295 44.337 cm
0 0 5.147 14.446 re f
Q
q
1 0 0 1 11.295 29.892 cm
0 0 5.147 14.446 re f
Q
q
1 0 0 1 16.442 29.892 cm
0 0 5.147 14.446 re f
Q
q
1 0 0 1 24.163 44.437 cm
0 0 2.574 14.446 re f
Q
q
1 0 0 1 21.589 29.991 cm
0 0 5.147 14.446 re f
Q
q
1 0 0 1 24.163 15.545 cm
0 0 2.574 14.446 re f
Q

Therefore, the expected y-coordinate of a corner should be 29.892 pt, but it is 0.099 pt higher.

2
  • Might be an idea to add some color so one can get an idea of what we're looking at. You'll probably need \leavevmode before the color cmd
    – daleif
    Commented Oct 9 at 14:45
  • 5
    The wikibook is notoriously inaccurate. You should not believe random things you read on the internet. [c] (which is the default) always loses baseline alignment Commented Oct 9 at 15:17

2 Answers 2

4

Sorry, but your reading of the text you cite is mistaken.

The c option is the default for \parbox and it aligns the vertical center of the box to the outer baseline (actually, the vertical center is slightly above the baseline), so half of the box is above and half is below the baseline.

With the t option, the \parbox is set so that the baseline of the first line inside is the same as the outer baseline. With b the last line.

In the examples below, the red line is drawn over the (outer) baseline.

\documentclass{article}

\usepackage{xcolor}

\newcommand{\drawbaseline}{%
  \makebox[0pt]{\color{red}\vrule height 0.05pt depth 0.05pt width 40cm}%
}

\newcommand{\sometext}{\textcolor{green!40!blue}{Some text some text
some text some text some text some text some text some text some text}}

\begin{document}

Abg \parbox{4cm}{\sometext} abg\drawbaseline

\bigskip

Abg \parbox[t]{4cm}{\sometext} abg\drawbaseline

\bigskip

Abg \parbox[b]{4cm}{\sometext} abg\drawbaseline

\end{document}

enter image description here

More precisely, every box TeX builds has a reference point that is placed on the baseline of the box it's being built (normally a line in a paragraph). The different options to \parbox fix the reference point in a way that doesn't depend on height and depth of the box when t or b is used; for c, the reference point is about midway from the top and the bottom (without any connection with inner baselines).

5
  • The question is precisely how this offset relates to the heights and depths of the outer and inner texts.
    – akj
    Commented Oct 9 at 19:19
  • 1
    @akj In no way they're related. TeX aligns boxes based on their reference point; the different options to \parbox fix the reference point in the way I showed. I added a final paragraph.
    – egreg
    Commented Oct 9 at 19:41
  • I'm not sure I'd describe this as a case in which the OP's reading of the cited text was mistaken. you have to read the linked text extremely charitably to come to that conclusion. although the text doesn't quite say what @akj claimed, the claim is strongly implied because the text explains the results just in terms of the number of lines, without mentioning that, of course, it matters that those lines are uniformly spaced.
    – cfr
    Commented Oct 10 at 1:09
  • 1
    Dear @egreg and @David Carlisle and @cfr, I want to express my sincere gratitude for your responsiveness and support. Unfortunately, I am currently unable to understand how the coordinates of the reference point of the box created by the command \parbox[c]{width}{text} are calculated relative to the baseline of the inner text, based on the available materials, including Lamport's LaTeX: A Document Preparation System and Mittelbach's The LaTeX Companion, possibly due to my own oversight. I hope to revisit this question in the future.
    – akj
    Commented Oct 10 at 14:15
  • 2
    @akj With c, the position of the reference point is not computed based on the baselines in the inner box. TeX takes the midpoint between the top and bottom of the box and the reference point is \fontdimen2\textfont2 lower.
    – egreg
    Commented Oct 10 at 14:21
0

Thanks to this comment by egreg, I figured out exactly how the \parbox[c] command works.

\vcenter and \parbox[c]

First of all, in Chapter 21 of Donald Knuth's "The TeXbook" (see also the quote in this answer to the question "A box's reference point"), for the vertical mode there are the following types of boxes: \vbox, \vtop, and \vcenter. The last type is intended for mathematical mode and is mentioned in the following context:

The concept of "axis" is meaningful for TeX only in math formulas, not in ordinary text; therefore TeX allows you to use \vcenter only in math mode. If you really need to center something vertically in horizontal mode, the solution is to say $\vcenter{...}$. (Incidentally, the constructions \vcenter to <dimen> and \vcenter spread <dimen> are legal too, in math mode; vertical glue is always set by the rules for \vbox in Chapter 12. But \vcenter by itself is usually sufficient.)

The mentioned types of boxes are used similarly to \parbox[b], \parbox[t] and \parbox[c]. The following example shows similar behavior regarding vertical alignment:

\documentclass[class=article, 12pt,
               border=1bp]
              {standalone}
\usepackage[utf8]{inputenc}

\def\myrule#1{\rule[-.3\normalbaselineskip]
                   {#1}
                   {\normalbaselineskip}}

\begin{document}

\myrule{1ex}%
\parbox{1ex}{\myrule{1ex}}%
$
 \myrule{1ex}
 \vcenter{
  \hbox to 1ex {\myrule{1ex}}
  }
$

\end{document}

And its rendering: Single-line boxes made with parbox[c] and \vcenter are raised by the same amount of height

A height and a depth of a Vcent atom

In Appendix G of Donald Knuth's "The TeXbook" on page 443, there is Rule 8:

If the current item is a Vcent atom (from \vcenter), let its nucleus be a vbox of height-plus-depth v. Change the height to 0.5v+a and the depth to 0.5va, where a is the axis height, σ22...

This is the sought-after formula. The reference point of boxes created with \vcenter will be raised by the amount of the axis height above the middle of the total height of such a box. One can obtain the axis height from the parameters of the mathematical font using \fontdimen22\textfont2, as suggested by egreg. Other parameters are listed in Appendices F and G of "The TeXbook" (see pages 433, 447) and in this answer to the question "What do different \fontdimen mean".

Vertical alignment adjustment

To align the baseline of an outer text line with a line in the box created using \parbox[c], it is necessary to move the reference point of the box according to the box's height, depth, and the offset of \fontdimen22\textfont2. For example, I use a box of normal height (0.7\normalbaselineskip) and depth (0.3\normalbaselineskip), so the box needs to be raised by 0.2\normalbaselineskip and lowered by \fontdimen22\textfont2.

Example with vertical alignment adjustment:

\documentclass[class=article, 12pt,
               border=1bp]
              {standalone}
\usepackage[utf8]{inputenc}

\def\myrule#1{\rule[-.3\normalbaselineskip]
                   {#1}
                   {\normalbaselineskip}}

\begin{document}

\myrule{1ex}%
\raisebox{\dimexpr 0.2\baselineskip -\fontdimen22\textfont2 \relax}
         {\parbox{1ex}{\myrule{1ex}}}%
\myrule{1ex}

\end{document}

And the corresponding rendering: Vertically adjusted parboxes align along the same baseline

The generated PDF file contains the following lines:

1 0 0 1 1 1 cm
0 0 5.146 14.446 re f
Q
q
1 0 0 1 6.146 1 cm
0 0 5.146 14.446 re f
Q
q
1 0 0 1 11.292 1 cm
0 0 5.146 14.446 re f
Q

Note that the y-coordinates of all the rulers match. I believe this is a fairly general solution.

You must log in to answer this question.

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