3

For some reason I get a linebreak when using my \textlabel command in the table cell. It seems to work fine for everything else.

\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}
\usepackage[pdftex,bookmarks=true,bookmarksnumbered,colorlinks=true]{hyperref} % http://ctan.org/pkg/hyperref
\usepackage{nameref}

\begin{document}

\makeatletter
    \newcommand*{\textlabel}[2]{%
        \edef\@currentlabel{#1}% Set target label
        \phantomsection% Correct hyper reference link
        #1\label{#2}% Print and store label
    }  
\makeatother

\section{Section}
\paragraph*{\textlabel{Paragraph}{paragraph}}
\paragraph*{}
\begin{tabular}{lp{10cm}}
    \hline
    \textbf{Col 1} & \textbf{Col 2} \\ \hline
    Bla1 & \textlabel{Blubb1}{reference1} \\
    & Description1 \\ 
    Bla2 & \textlabel{Blubb2}{reference2} \\
    & Description2 \\ \hline
\end{tabular}
\end{document}

1 Answer 1

5

Avoid doing things in vertical mode, especially at the start of table cells.

\makeatletter
    \newcommand*{\textlabel}[2]{%
        \edef\@currentlabel{#1}% Set target label
        \leavevmode\phantomsection% Correct hyper reference link
         %%%%%%%%%%
        #1\label{#2}% Print and store label
    }  
\makeatother
3
  • Excuse me, but could you elaborate a little further? What exactly does 'vertical mode' mean? I have to admit that I haven't written the command by myself, therefore don't really know how it works, however, am very eager to learn.
    – cherrung
    Jun 9, 2012 at 21:14
  • 2
    (very simple and somehow incorrect explanation) At the beginning of a cell, LaTeX is in "vertical" mode, which means that things "tend to be put atop each other", which makes \phantomsection occupy its own line, even though it does not print anything. By \leavevmode you "leave vertical mode" and enter "horizontal mode", where things are put next to each other and \phantomsection occupies zero horizontal space then.
    – yo'
    Jun 9, 2012 at 21:25
  • I was away from machine but @tohecz gives a good explanation. Searching for "mode" on this site or other tutorials will give other examples of this distinction. Jun 9, 2012 at 21:59

You must log in to answer this question.

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