5

I am placing a label inside a cell in a tabular environment, with the "p" column type. If hyperref is included the cell contains unwanted vertical space messing up the formatting. Without hyperref, the format is correct.

I am guessing this is related to the problem described here and here but I am unable to adapt the solutions to those questions to my situation. How do I obtain the correct spacing without ditching hyperref?

\documentclass{article} 
\usepackage{hyperref}

\newcounter{foo}
\renewcommand{\thefoo}{T\arabic{foo}}
\newcommand{\foofoo}{\refstepcounter{foo}\textbf{\thefoo}}

\begin{document}
\begin{tabular}{|p{5mm}|p{80mm}|} \hline
\foofoo\label{foo:jeez} & Jeez \\\hline
& Words words wordsWords words wordsWords words words\\\hline
\end{tabular}

\end{document}

With hyperref:

enter image description here

Without hyperref:

enter image description here

(The code for the second example is as above, but with hyperref commented out.)

2 Answers 2

6

The vertical reference position of a cell with column type p is the reference position of the top most element in the cell, e.g.:

  • baseline of the top most row,
  • baseline of the top most rule,
  • above a vertical space,
  • the position of a whatsit (\label, \special, \write, ...)

\refstepcounter also creates an anchor with hyperref. This is internally implemented by whatsits (e.g. \special). Therefore, this anchor whatsit is aligned with the baseline of the next cell's top line.

In principle, you do not want the anchor in the row. At this time, it is unknown, which heights the next cells will have (there could be a large image, ...). It is better to put the anchor out of alignment right before the row starts. This can be achieved with \noalign before a new row starts:

\noalign{\refstepcounter{foo}}

However, this also constitutes a group, the internal commands, \label relies on, are lost afterwards. Therefore, the \label also has to go at this place. And the \label also creates a whatsit, a write request to the .aux file.

\noalign{\refstepcounter{foo}\label{foo:jeez}}
\textbf{\thefoo}

Full example:

\documentclass{article}
\usepackage{booktabs}
\usepackage[colorlinks]{hyperref}

\newcounter{foo}
\renewcommand{\thefoo}{T\arabic{foo}}

\begin{document}
\begin{tabular}{p{5mm}p{80mm}}
\toprule
\noalign{\refstepcounter{foo}\label{foo:jeez}}
\textbf{\thefoo}
& Jeez \\
& Words words wordsWords words wordsWords words words\\
\bottomrule
\end{tabular}

\medskip
Link to \ref{foo:jeez}.
\end{document}

Result

2

Add something to start the paragraph, e.g.

\documentclass{article}
\usepackage{hyperref}

\newcounter{foo}
\renewcommand{\thefoo}{T\arabic{foo}}
\newcommand{\foofoo}{\leavevmode\refstepcounter{foo}\textbf{\thefoo}}

\begin{document}
\begin{tabular}{|p{5mm}|p{80mm}|} \hline
\foofoo\label{foo:jeez} & Jeez \\\hline
& Words words wordsWords words wordsWords words words\\\hline
\end{tabular}

\end{document}

You must log in to answer this question.

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