7

If a tabular column of type p (or a few others such as tabularx's X) starts with a write (for example because it starts with \index), this inserts some vertical space. MWE:

\documentclass{article}
%\usepackage{array}
\newcommand{\writethis}{\write 1{}}
\begin{document}
\begin{tabular}{lp{4em}}
  \hline
  middle &o\writethis k\\ \hline
  start &\writethis foo\\ \hline
  end &foo\writethis \\ \hline
  alone &\writethis \\ \hline
\end{tabular}
\end{document}

The “start” line has some extra vertical space above “foo”. If the array package is loaded, the “alone” box also has extra vertical space.

I understand part of what's going on, but not all. The \write command inserts a whatsis, which is a kind of box; when that box is “typeset” by the output routine, it causes the data to be written to the file, with no visual effect. No visual effect — except that somehow this causes a box with nonzero height to be typeset.

I have a solution for my use case: add \leavevmode before \write. I think this causes the whatsis to be a horizontal box instead of a vertical one and so I get a zero-width horizontal space instead of a nonzero-height vertical space, but I'm not sure. I'd like to understand what's going on. So I have several closely related questions:

  1. Is my interpretation of the direction of the whatsis box correct?
  2. Why does this box seemingly have nonzero height? What determines the height, or if the spurious vertical space isn't due to the whatsit box, what's causing it?
  3. Why does the array package make a difference?
  4. Is leavevmode a correct solution here (\write at the beginning of a LaTeX table cell)?
  • I haven’t got time to write an answer, but I can offer you a few hints. 1. the p specifier is implemented by means of a \vtop. 2. Read the rules for the height of a \vtop in the last paragraph of page 81 of The TeXbook, in particular rule (2). 3. Put \showboxbreadth=10 \showboxdepth=1000 \showlists right after \end{tabular} and you’ll see very clearly what’s happening, with and without the array package. Hope this can be of help, otherwise wait for someone who’s got more time than I have… :-) – GuM Jun 1 '16 at 2:13
6

So, expanding what @GustavoMezzetti (whom I thank) suggests, here is an explanation. I will quote and paraphrase the TeX Book during this post.

Introduction

Let's start to understand what happens without array, ie at the basic TeX/LaTeX level. As Gustavo suggests, the p column is created using a vtop.

First we have to consider vtop's sibling vbox. Please remark that vbox is a vertical box (containing other boxes) which has the same baseline as the last box inside of it. vtop is a similar structure but has the same baseline as the first box inside of it.

TeX has to do some kind of calculations to do for vertical material since it has two dimensions in that direction (height and depth), opposed to the single width when doing horizontal boxes.

Vboxes

So, TeX, when constructing a vbox is basically concerned about establishing its depth, in order to make its baseline the same as the last (from top) box inside of it. Here are the steps that TeX undertakes in determining this:

  1. If no boxes are inside the vbox then its depth d is 0pt
  2. If there is at least one box, but it's followed by kern or glue, "with possibly intervening penalties or other things" (sic.) the depth d is 0pt
  3. If there's at least one box and the last of them isn't followed by kern or glues, the depth d is set equal to the depth of that box

Note: there is a max depth value boxmaxdepth (its default value is 16384pt) which cannot be exceeded by the vbox depth. In case the depth calculated above is more than that quantity, the depth is set to \boxmaxdepth and the height is increased accordingly (practically this corresponds to moving the baseline down in order to obey the depth rules).

Vtops

Now, when TeX is concerned with vtops, it's searching how to align the vertical box baseline with the first of the boxes inside of it, thus it will be concerned with the height of the box. It proceeds with the following steps:

  1. Make a vbox, with the aforementioned rules. Let d and h be its depth and height
  2. The final height x is calculated this way:
    1. If the first element of the vbox is not a box, then the height is 0pt
    2. otherwise the height will be the height of the first box

Now the vtop gets build by shifting the vbox references so that the final height is x calculated in point 2, and the resulting box depth will be h+d-x

Our case

The problem now is quite clear: when using the \writethis at the beginning of the p column, the vtop construction in step 2 sets the height of the vbox to be exactly 0pt, so its reference point will be at the very top of it, thus messing up the alignment. Conceptually all of the content is in its depth, below the baseline.

Array

The array package places a strut inside the cells of the table. This has an height of around 8.4pt, with no depth set. The normal hbox containing the characters have an height of around 6.9pt and depth of around 3.6pt. So while in the previous (strut-less) example the h+d of the vbox (pre-rearrangement due to vtop) is ~10.5pt, when the strut comes into play, this is increased to ~12pt (the strut height plus the hbox depth). When rearranged, though, the total height of the vtop generated box is still 0pt since its first element is not a box: this gives the extra space you have.

  • Beautifully explained! But the part concerning the array package should be improved… ;-) – GuM Jun 7 '17 at 21:20
  • @GustavoMezzetti any suggestions about that? I had quite a hard time to gather all of the other informations. What do you feel missing from the array part? – Moriambar Jun 7 '17 at 21:28
  • \showlists will tell you (you want to deserve the reputation points you’ve earned, don’t you? ;-) – GuM Jun 7 '17 at 21:31
  • @GustavoMezzetti I'm afraid showlists does not tell me anything more than what I just wrote, especially about the differences between "standard" and "array". I see nothing apart the strut – Moriambar Jun 7 '17 at 21:44
  • What I meant is that you negected to cover the peculiar case of the “alone” line, which is slightly different from the others, as \showlists will show you. – GuM Jun 8 '17 at 23:21

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.