0

The following minimal example doesn't do what I expect:

\documentclass{article}
\begin{document}
\begin{tabular}{p{40mm}p{60mm}}\label{blah}
a & b \\
c & d
\end{tabular}
\end{document}

The output looks like this:

enter image description here

I don't understand why the b is displaced upward. If I remove the label, the problem goes away. Can anyone explain?

  • 1
    Try removing the label. Anyway, there's no table environment, hence no table counter. – Bernard Apr 22 '20 at 19:24
  • @Bernard: I want to be able to refer to the table by page number, just like if I put a label in any random piece of text. Is there some reason why it needs to be placed somewhere else, like outside the table, to avoid this vertical space? –  Apr 22 '20 at 19:28
  • 2
    ...we've had this before somewhere... – Werner Apr 22 '20 at 19:35
  • 2
    @BenCrowell - For \label to work, LaTeX has to be able to associate the argument of \label with a counter. The \caption command, when used inside a table (not tabular) environment, increments a counter called table. If you place \label after \caption, LaTeX will make the correct connection. In contrast, your particular use of \label is guaranteed to achieve nothing useful -- and, in fact, mess things up in unexpected ways. Interestingly, if one embeds your tabular environment (sans the \label directive) inside a table environment, the problem vanishes. – Mico Apr 22 '20 at 19:56
  • Thanks, all. Moving the label outside the tabular does work. @Mico: In contrast, your particular use of \label is guaranteed to achieve nothing useful Again, as I explained in the earlier comment to Bernard, this does achieve something useful. I want to be able to refer to the page number where the material occurs. In case this is still causing confusion, my text where I refer to it is something like this: "We now apply the system of mathematical analogies described on p.~\pageref{blah}." –  Apr 22 '20 at 20:19
  • So it seems to me that this is a plain old bug in latex. It does something totally wrong in what is I suppose an unexpected use case. I don't see any reason why a \label{} command should ever change the rendered output. –  Apr 22 '20 at 20:23
  • @BenCrowell - You wrote, "I want to be able to refer to the page number where the material occurs". Fortunately, you can achieve this noble goal without placing a \label instruction in a place where it doesn't belong. I guess viewpoints will diverge on whether or not the issue you've encountered is a bug. Knuth has memorably stated that he never intended TeX to be cracker-proof. If you insist on taking LaTeX in directions that were never intended to be taken -- congratulations. you may call yourself a certified LaTeX cracker. – Mico Apr 22 '20 at 20:45
  • 1
  • 1
    @Mico it is definitely not a bug more a documented feature (you see the same with other things, notably \color) and so long as you don't need \ref it's Ok to use \label here. By design it does something more or less sensible even if no counter is active. – David Carlisle Apr 22 '20 at 23:20

1 Answers1

2

You need to put the \label somewhere safer:

enter image description here

\documentclass{article}
\begin{document}

\hrule

\begin{tabular}{p{40mm}p{60mm}}\label{blah}
a & b \\
c & d
\end{tabular}


\hrule

\begin{tabular}{p{40mm}p{60mm}}
a\label{blah2} & b \\
c & d
\end{tabular}

\hrule

\begin{tabular}{p{40mm}p{60mm}}
a & b \\
c & d
\end{tabular}\label{blah3}

\hrule

\end{document}

A \label is not like \typeout that simply writes to the terminal it has to delay the writing to the aux file until the page number is known. The resulting write node is in many ways like a box of height and width 0pt and that is the effect that you see on the layout: the parbox (vtop) is aligning on the first node in its vertical list which is the invisible write node.

David Carlisle
  • 757,742