5

I have tried to use the code from How to make a hyperlink cover the whole line in table of contents including the dots? but it seems to make my entire document hyperlinked probably due to my custom style. I am using the tocloft package. I was wondering if I could have the entire line hyperlinked like

[1 Introduction ...... 1]

Where everything in between [] is linked

I can only seem to get either the title, the number, or both, but not the entire line.


Here is my MWE. I included all the current packages I will be using in case there's a conflict between some of them.

\documentclass{ut-thesis}

\usepackage[backend=biber]{biblatex}
\addbibresource{Thesis.bib}
\usepackage{graphicx}
\graphicspath{{Pictures/}}
\usepackage{float}
\usepackage{caption}
\usepackage{blindtext}
\usepackage[titles]{tocloft}

\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} % for chapters

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

\degree{Masters}
\department{Deparment}
\gradyear{2013}
\author{John Doe}
\title{Thesis Title}

\setcounter{tocdepth}{2}

\flushbottom

%%%%%%%%%%%%      MAIN  DOCUMENT      %%%%%%%%%%%%

\begin{document}

    \begin{preliminary}

        \maketitle

        \begin{abstract}
        \addcontentsline{toc}{chapter}{Abstract}
            This is my abstract!
        \end{abstract}

        %Add Figure into the list of figures and tables
        \renewcommand{\cftfigpresnum}{Figure\ }
        \renewcommand{\cfttabpresnum}{Table\ }
        \newlength{\mylenf}
        \settowidth{\mylenf}{\cftfigpresnum}
        \setlength{\cftfignumwidth}{\dimexpr\mylenf+1.5em}
        \setlength{\cfttabnumwidth}{\dimexpr\mylenf+1.5em}

        \renewcommand\contentsname{Table of Contents}
        \tableofcontents

        \listoffigures
        \addcontentsline{toc}{chapter}{List of Figures}

        \listoftables
        \addcontentsline{toc}{chapter}{List of Tables}

    \end{preliminary}

    \chapter{Introduction}
    \section{Motivation}

    This is a section

    \printbibliography
    \addcontentsline{toc}{chapter}{Bibliography}

\end{document}

It seems that the problems are coming from

\renewcommand\contentsname{Table of Contents}
\tableofcontents`

And obviously if I delete it I can't see if it's working since I no longer print the table of contents.


EDIT

I've made no progress and I would still like some help if possible.

  • 4
    Can you post an MWE? If something in that custom style is messing things over, we won't be able to tell until we see it :) – Sean Allred Aug 23 '13 at 15:37
2

hyperref generates and transfers the hyperlinks verbatim to whichever package you use for setting your ToC. This allows hyperref to be compatible with a host of packages. Since it's known that you're using the combination

\usepackage[titles]{tocloft}
\usepackage[linktoc=all]{hyperref}

we can knowing update what hyperref transfers and update the way tocloft sets the ToC entries.

Let's look at how ToC-related entries are processed under hyperref by looking at a generic ToC entry:

\contentsline {<type>}{<title>}{<page>}{<hyperlink>}

hyperref translates \contentsline{<type>} to be equivalent to \csname l@<type>\endcsname (or \l@<type>). Additionally, it inserts a hyperlink start and end group around both <title> and <page> using the <hyperlink> marker. Effectively, using HTML-link tags, the above is translated into

<a href="<hyperlink>"><title></a> . . . <a href="<hyperlink>"><page></a>

You want to change the above to

<a href="<hyperlink>"><title> . . . <page></a>

so we need to intervene before hyperref passes the hyperlinks to tocloft, otherwise it isn't possible to combine the two separate <title> and <page> hyperlinks into one. Here are the steps:

  1. Remove the insertion of hyperlinks performed by hyperref. Since we know you're using the linktoc=all option, an override of \contentsline that resembles the following suffices:

    \makeatletter
    \def\contentsline#1#2#3#4{%
      \begingroup
        \Hy@safe@activestrue
      \edef\x{\endgroup
        \def\noexpand\Hy@tocdestname{#4}%
      }\x
      % ...removed a bunch of conditionals knowing you're using 'linktoc=all'
      \csname l@#1\endcsname{%
        %\hyper@linkstart{link}{\Hy@tocdestname}{#2}\hyper@linkend
        #2%
      }{%
        %\hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
        #3%
      }%
    }
    \makeatother
    

    You'll note that the hyperlink start and end surrounding #2 and #3 respectively have been removed, and we just pass #2 and #3 as-is to tocloft.

  2. Manually insert the removed contents above as part of toclofts formatting for chapters and sections (additional levels can be added, but since you're using \setcounter{tocdepth}{2} I didn't add these):

    \makeatletter
    % Update ToC hyperlinks for Chapters
    \patchcmd{\l@chapter}% <cmd>
      {{\cftchapfont #1}}% <search>
      {\hyper@linkstart{link}{\Hy@tocdestname}{}{\cftchapfont #1}}% <replace>
      {}{}% <success><failure>
    \patchcmd{\cftchapfillnum}{\par}{\hyper@linkend\par}{}{}
    % Update ToC hyperlinks for Sections
    \patchcmd{\l@section}% <cmd>
      {{\cftsecfont #1}}% <search>
      {\hyper@linkstart{link}{\Hy@tocdestname}{}{\cftsecfont #1}}% <replace>
      {}{}% <success><failure>
    \patchcmd{\cftsecfillnum}{\par}{\hyper@linkend\par}{}{}
    \makeatother
    

    Each ToC-related entry has two etoolbox patches associated with it. The first inserts the hyperlink start, while the second inserts the hyperlink end before there is a \paragraph break (hyperlinks can't be inserted in vertical mode).

Here is a complete minimal example showing your setup:

enter image description here

\documentclass{ut-thesis}

\usepackage[titles]{tocloft}

\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} % for chapters

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

\degree{Masters}
\department{Deparment}
\gradyear{2013}
\author{John Doe}
\title{Thesis Title}

\setcounter{tocdepth}{2}

\makeatletter
\def\contentsline#1#2#3#4{%
  \begingroup
    \Hy@safe@activestrue
  \edef\x{\endgroup
    \def\noexpand\Hy@tocdestname{#4}%
  }\x
  \csname l@#1\endcsname{%
    %\hyper@linkstart{link}{\Hy@tocdestname}{#2}\hyper@linkend
    #2%
  }{%
    %\hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
    #3%
  }%
}
% Update ToC hyperlinks for Chapters
\patchcmd{\l@chapter}% <cmd>
  {{\cftchapfont #1}}% <search>
  {\hyper@linkstart{link}{\Hy@tocdestname}{}{\cftchapfont #1}}% <replace>
  {}{}% <success><failure>
\patchcmd{\cftchapfillnum}{\par}{\hyper@linkend\par}{}{}
% Update ToC hyperlinks for Sections
\patchcmd{\l@section}% <cmd>
  {{\cftsecfont #1}}% <search>
  {\hyper@linkstart{link}{\Hy@tocdestname}{}{\cftsecfont #1}}% <replace>
  {}{}% <success><failure>
\patchcmd{\cftsecfillnum}{\par}{\hyper@linkend\par}{}{}
\makeatother

\begin{document}

\begin{preliminary}

  \maketitle

  \begin{abstract}
  \addcontentsline{toc}{chapter}{Abstract}
    This is my abstract!
  \end{abstract}

  %Add Figure into the list of figures and tables
  \renewcommand{\cftfigpresnum}{Figure\ }
  \renewcommand{\cfttabpresnum}{Table\ }
  \newlength{\mylenf}
  \settowidth{\mylenf}{\cftfigpresnum}
  \setlength{\cftfignumwidth}{\dimexpr\mylenf+1.5em}
  \setlength{\cfttabnumwidth}{\dimexpr\mylenf+1.5em}

  \renewcommand\contentsname{Table of Contents}
  \tableofcontents

  \listoffigures
  \addcontentsline{toc}{chapter}{List of Figures}

  \listoftables
  \addcontentsline{toc}{chapter}{List of Tables}

\end{preliminary}

\chapter{Introduction}
\section{Motivation}

This is a section

\section{This is a very long section title 
         this is a very long section title 
         this is a very long section title
         this is a very long section title}

\end{document}

Hyperlinks spanning multiple lines are not formatted well, but that's just the way it is. I'd suggest using

\<type>[<ToC title>]{<main title>}

instead to format a <ToC title> that better fits the ToC width.

  • 1
    Very thorough, and nicely explained! – Mico Feb 3 '16 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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