0

EDIT:

I solved most of my original problems so I am rewriting the question which is still fundamentally the same but now I can post a much nicer MWE.

The problem I am having is to make the dots in the table of contents into links that link to the relevant section/chapter. Essentially I want the clickable link to span from the text of the section all the way through to the page number, so that it includes the dots in between. I have tried a number of potential solutions but can't seem to get it to work.

Ultimately my goal is to mimic the example page given here, where I believe I have managed to do so except for some minor formating that I'm still finishing off (eg removing numbers in subsections and correcting some indenting). The only piece I can't seem to figure out is making the dots clickable along with the text and page number.

Here is the self-contained MWE: (Keep in mind it's a bit long because part of the problem is the package interaction, so I need to include them all with their settings to make sure any potential solutions avoid package conflicts).

\documentclass{report}
\makeatletter
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage[titles]{tocloft}
\usepackage[linktoc=all]{hyperref}% Use this to provide intra-pdf hyperlinking and better toc
                                  % linktoc=all used to extend the hyperlink across the dotted lines.... in theory.
\hypersetup{%               % Setup the coloring of the links. 
%                           % Currently the only necessary one is "colorlinks=true" and "linkcolor=blue".
    colorlinks   = true,    % Colours links instead of ugly boxes
    urlcolor     = blue,    % Colour for external hyperlinks
    linkcolor    = blue,    % Colour of internal links
    citecolor    = blue     % Colour of citations, could be ``red''
    }

\usepackage{titlesec}% Use this to manipulate default chapter styling
\usepackage[margin=1in]{geometry}
\raggedright
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand\headrulewidth{0pt}
\lhead{}\chead{}\rhead{}
\cfoot{\vspace*{1.5\baselineskip}\thepage}


\usepackage[nodisplayskipstretch]{setspace}%
\doublespacing% Make everything double spaced by default.
\preto\longtable{\par\singlespacing}% Pre-append using longtables to make sure that tables are single spaced.
\preto\itemize{\par\singlespacing}% Pre-append singlespace to itemize to account for single space bullet point lists.


\titleformat{\chapter}[hang]{\large}{}{0pt}{\centering\singlespacing\MakeUppercase}{\raggedright\doublespacing}
\titleformat{\part}[hang]{\large}{}{0pt}{\centering\singlespacing\MakeUppercase}{\raggedright\doublespacing}
\renewcommand{\contentsname}{TABLE OF CONTENTS}%

\renewcommand{\cftdotsep}{1}

\renewcommand\cftchapfont{\normalfont}%         Remove Bold font from TOC
\renewcommand\cftchappagefont{\normalfont}%     Remove Bold font from TOC
\renewcommand\cftpartfont{\normalfont}%         Remove Bold font from TOC
\renewcommand\cftpartpagefont{\normalfont}%     Remove Bold font from TOC
\setlength{\cftbeforechapskip}{1pt}%
\setlength{\cftbeforepartskip}{1pt}%
\setlength{\cftbeforesecskip}{-6pt}%
\setlength{\cftbeforesubsecskip}{-6pt}%
\setlength{\cftbeforesubsubsecskip}{-6pt}%
\def\cftchapleader{\hyperlink{\mylabel}{\cftdotfill{\cftsecdotsep}}}% Add in the dotted lines between the name and page number in TOC

\begin{document}
\setcounter{secnumdepth}{-1}% We don't want chapter numbers until later, 
                            % So let's kill off the table of contents depth detector until we want to start counting.

\chapter{DEDICATION}
Some dedication stuff goes here.

\chapter{ACKNOWLEDGEMENTS}
\label{Acknowledgements}
\hypertarget{acknowledgements}{}
\edef\mylabel{acknowledgements}

Some Acknowledgements Text here.

\tableofcontents

\listoftables
\addcontentsline{toc}{chapter}{LIST OF TABLES}
\hypertarget{LOT}{}
\edef\mylabel{LOT}
%\addtocontents{toc}{\protect\contentsline{chapter}{LIST OF TABLES}{}{\@currentHref}}

\listoffigures
\addcontentsline{toc}{chapter}{LIST OF FIGURES}
\hypertarget{LOF}{}
\edef\mylabel{LOF}
%\addtocontents{toc}{\protect\contentsline{chapter}{LIST OF FIGURES}{}{\@currentHref}}


%\def\cftchapleader{\hyperlink{abbreviations}{\cftdotfill{\cftsecdotsep}}}%
\hypertarget{abbreviations}{}
\chapter{ABBREVIATIONS}
\label{abbreviations}
\edef\mylabel{abbreviations}

Some Abbreviation text would go here.


\chapter{ABSTRACT}
\label{abstract}

Some Abstract text goes here.


\addtocontents{toc}{\protect\contentsline{part}{CHAPTER}{}{}}
\setcounter{secnumdepth}{3}% Now we want toc numbering.

\chapter{First real chapter}
Some intro or something else here

\section{A `section', toc should be indented some.}

Some section info.

\subsection{A `subsection', toc should be indented even more.}

\chapter{Second real chapter}
More text here.

\setcounter{secnumdepth}{-1}% We don't want the Appendix to have a number, but it should be linked.
\chapter*{APPENDIX}
\label{appendix}

\addtocontents{toc}{\protect\contentsline{part}{APPENDIX}{}{\@currentHref}}
\setcounter{secnumdepth}{3}% Now we number again.


\chapter{REFERENCES}
\label{references}

\chapter{BIOGRAPHY}
\label{biography}

\end{document}

To be clear, the specific line of interest is \def\cftchapleader{\hyperlink{\mylabel}{\cftdotfill{\cftsecdotsep}}}% which is my last attempt at trying to get the dots to link to the correct spot using \hyperlink from the hyperref package. But even trying to update the \mylabel each section doesn't seem to allow links to be formed correctly, and neither does the \@currentHref. I suspect this is an expansion issue due to when the actual ToC is formed versus when the labels and things are formed.

  • 1
    Sorry I won't wander through the internet to get a class and a template and then spend an hour to sort out your code. Minimize your code, put all relevant data into your question and ask a specific question. – Ulrike Fischer Feb 9 '19 at 19:02
  • "A solution would be great, but explaining how \addtoconents and \contentsline commands work above (and why we need a \protect command in there too?) would be even better" Question: Where can I find useful docs on syntax for these commands. "Here is the relevant portion of my cls file" Already minimized the code. "You can find the full cls file here on github" Is a direct link to the cls file, no 'wandering of the internet' required. Sarcasm really wasn't necessary. If you didn't want to answer it, then don't. If you want me to cut down my question, then at least be polite about it. – Jason Feb 9 '19 at 20:04
  • I'll add a MWE shortly as I have actually solved almost all of my original problems in this question now. But to answer your question about the use of \At(Begin/End)Document commands... The document must be made out of certain files (one file for each "piece") and those elements must be compiled in a specific order. Rather than having a large template that requires people to make sure they use the \include commands in the correct order, I am taking in file names, and then using the \At(Begin/End)Document commands to force the compile in the correct order. – Jason Feb 9 '19 at 22:26
  • Your dots don't link to the correct pages. – Ulrike Fischer Feb 9 '19 at 23:51
  • Sorry for the confusion Ulrike; that is the question I'm asking. I generated the dots, and I can make them linked "somewhere" but I can't figure out how to link them to the correct page. – Jason Feb 9 '19 at 23:58
1

Use this to link the chapter dots to the correct pages:

\makeatletter
\def\cftchapleader{\hyper@linkstart{link}{\Hy@tocdestname}{\cftdotfill{\cftsecdotsep}}\hyper@linkend}
\makeatletter

You can remove all the hypertargets and \mylabel definitions etc.

| improve this answer | |

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.