0

Expected behavior:

  • Whole ToC line becomes link to section

Observed behavior:

  • xelatex - matches expected behavior
  • pdflatex - breaks with "\pdfendlink cannot be used in vertical mode" error

Here is mwe:

\documentclass{article}

\usepackage{hyperref}
\usepackage{titletoc}

\makeatletter   
\titlecontents{section}[0cm]
    {\hyper@linkstart{link}{\Hy@tocdestname}}
    {\bfseries\thecontentslabel\hspace{1em}}
    {\bfseries}
    {\titlerule*[1pc]{.}\contentspage}
    [\hyper@linkend]
\makeatother

\begin{document}
     \tableofcontents

     \section{Test section}
\end{document}

After some googling found, that \protect is needed somewhere, cannot determine where.

2
  • 1
    the problem is that your \hyper@linkend is issued in vertical mode and this is not allowed. It would work if you do something like \leavevmode\hyper@linkend but then you get additional spaces. Commented Aug 29 at 6:22
  • @UlrikeFischer thanks for suggestion. Do you perchance know why pdflatex switches to vertical mode before \hyper@linkend? Commented Aug 29 at 9:08

1 Answer 1

0

After mindlessly trying different configurations, this is most sensible thing i came up with:

\documentclass{article}

\usepackage{hyperref}
\usepackage{titletoc}

\makeatletter   
\titlecontents{section}[0cm]
    {\hyper@linkstart{link}{\Hy@tocdestname}}
    {\bfseries\thecontentslabel\hspace{1em}}
    {\bfseries}
    {\titlerule*[1pc]{.}\thecontentspage\aftergroup\hyper@linkend}
\makeatother

\begin{document}
     \tableofcontents

     \section{Test section}
\end{document}

I do not understand why, but adding \aftergroup before link end and moving link end into same group as separator somehow works

You must log in to answer this question.

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