Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The linktoc=all option of hyperref package makes both the reference text and page number clickable hyperlinks. However, the dots are still not hyperlinks. This is not very important but I'm asking out of curiosity to know if it is a limitation in hyperref, noting that MS Word & OpenOffice as well as other tools allow to have the dots clickable?

May be it's not recommended?

share|improve this question
    
Somewhat related question: How to make chapter numbers in TOC as non-link (hyperref package) –  diabonas Feb 15 '12 at 23:04
add comment

2 Answers

up vote 12 down vote accepted

Hyperlinks in the table of contents (TOC) are generated by the \contentsline command defined in hyperref.sty. The linktoc=all option setting makes \contentsline add separate links to the title and page number. A third link can be applied to the leader between the title and page number by patching the appropriate leader-generating command locally using etoolbox. Enclosing the whole TOC entry in a hyperlink would be much tidier, but this is a relatively difficult problem.

For TOCs formatted by latex internals, the leader is generated by the command \@dottedtocline defined in latex.ltx. The following patch simply encloses the leader in a hyperlink pointing to the current TOC entry's location, which is passed as the fourth argument to \contentsline.

\documentclass{report}
\usepackage[linktoc=all]{hyperref}
\usepackage{etoolbox}

\makeatletter
\pretocmd{\contentsline}
  {\patchcmd{\@dottedtocline}
     {\leaders}
     {\hyper@linkstart{link}{#4}\leaders}
     {}
     {}%
   \patchcmd{\@dottedtocline}
     {\hfill}
     {\hfill\hyper@linkend}
     {}
     {}}
  {}
  {}
\makeatother

\begin{document}
\tableofcontents
\chapter{First Chapter} \pagebreak
\section{First Section} \pagebreak
\subsection{First Subsection}
\chapter{Second Chapter} \pagebreak
\section{Second Section} \pagebreak
\subsection{Second Subsection}
\end{document}

The result looks terrible with link borders, but not so bad with colorlinks=true. enter image description here

This patch is general enough to work with TOCs formatted via package-defined user commands so long as the leader is generated by \@dottedtocline. KOMA-Script's tocstyle is one example of such a package - it redefines \@dottedtocline.

tocloft and memoir issue the command \cftdotfill instead of \@dottedtocline. The code below demonstrates a patch that will work for both the tocloft package and the memoir document class.

\documentclass{memoir}
\usepackage[linktoc=all]{hyperref}
\usepackage{etoolbox}

\makeatletter
\pretocmd{\contentsline}
  {\patchcmd{\cftdotfill}
     {\leaders}
     {\hyper@linkstart{link}{#4}\leaders}
     {}
     {}%
   \patchcmd{\cftdotfill}
     {\hfill}
     {\hfill\hyper@linkend}
     {}
     {}}
  {}
  {}
\makeatother

\setcounter{tocdepth}{2}
\renewcommand*{\cftdot}{\ensuremath{\ast}}
\renewcommand*{\cftsectionfont}{\itshape}
\renewcommand*{\cftsectionleader}{\cftdotfill{\cftsectiondotsep}}
\renewcommand*{\cftsubsectionfont}{\scshape}
\renewcommand*{\cftsubsectiondotsep}{9}
\renewcommand*{\cftsubsectionleader}{\cftdotfill{\cftsubsectiondotsep}}

\begin{document}
\tableofcontents
\chapter{First Chapter} \pagebreak
\section{First Section} \pagebreak
\subsection{First Subsection}
\chapter{Second Chapter} \pagebreak
\section{Second Section} \pagebreak
\subsection{Second Subsection}
\end{document}

enter image description here

share|improve this answer
add comment

I guess one reason hyperref doesn't make the leader dots clickable even if the linktoc=all option has been set is that not all lines in a table of contents have dots. For example, in the default definitions of the article document class, sub- and subsub-sections have dots, whereas sections do not.

These default settings are not immutable and can be changed, of course, using either LaTeX's own commands or -- much more easily -- a custom-written package such as tocloft. Worse, the "style" of the leader dots (e.g., their density, or their type: dots, asterisk, heart shapes, etc) can also be changed. As far as I know, these dot leaders do not constitute a separate group that could be manipulated globally (so that hyperref could access them somehow); this is in contrast, obviously, to the situation of the page numbers and the sectioning titles. Hyperref probably doesn't (and shouldn't, in my view) make assumptions about which other parts of the code may be changing the table of contents' style and hence (wisely, I'd say) does not attempt to do anything with the dots. :-)

share|improve this answer
    
From your explanation Mico, this seems not feasible, at least not without a hack. However I think it's not impossible anyway (with some coordination between LaTeX & hyperref I guess). I'm not knowledgeable about the internals of these two though. –  John Kirollos Sep 19 '11 at 10:23
    
Indeed, that would be the correct interpretation of my "answer". :-) If you feel strongly that having the dots be part of the hyperlink, you may want to consider sending an email to the maintainer of the hyperref package, Heiko Oberdiek, and ask him if this is something he'd be willing and able to implement. –  Mico Sep 19 '11 at 12:04
    
Thanks Mico, I might consider doing that some day :) –  John Kirollos Sep 19 '11 at 13:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.