2

I'm using Overleaf for my literature review and would like to know how to change the hyperref links of the table of contents. Right now the section numbers and section names are used as link, like so:

enter image description here

However, I find the amount of color on this page too overwhelming, but I want to be consistent with coloring all links. I considered changing the links of the table of contents to page only, by using the linktocpage as stated on p.10 of https://mirror.lyrahosting.com/CTAN/macros/latex/contrib/hyperref/doc/manual.pdf

Now only the page numbers are blue, which is to be expected. However, I would like the section numbers to also be links. How do I achieve that?

I did find the following thread from 2015: Hyperlink only Section Number in Table of Contents, but that didn't solve my problem.

Any help is much appreciated. Thank you in advance.

MWE

\documentclass{article}
\usepackage[hidelinks, colorlinks=true, allcolors=blue]{hyperref}
\usepackage{lipsum}

\begin{document}
    \tableofcontents
    
    \section{First}
    \lipsum[66]
    
    \subsection{First first}
    \lipsum[66]
    
    \section{Second}
    \lipsum[66]
    
    \subsection{Second second}
    \lipsum[66]
\end{document}

1 Answer 1

3

hyperref patches \contentsline for the links. In \contentsline the number and the text is in one argument, the page number is another. It is therefore rather easy for hyperref to add a link around the number and the text, or around the page number (or around both), but it is not so easy to split number and text, as different classes can have different definitions for \numberline (or even the content of the argument).

But your example you can do it like this:

\documentclass{article}
\usepackage[hidelinks, colorlinks=true, allcolors=blue,linktoc=page]{hyperref}
\usepackage{lipsum}
\makeatletter
\def\numberline#1{%
 \ifx\Hy@tocdestname\ltx@empty
  \hb@xt@\@tempdima{#1\hfil}%
 \else 
 \hb@xt@\@tempdima{\hyper@linkstart{link}{\Hy@tocdestname}#1\hyper@linkend\hfil}%
 \fi}
\makeatother
\begin{document}
    \tableofcontents

    \section{First}
    \lipsum[66]

    \subsection{First first}
    \lipsum[66]

    \section{Second}
    \lipsum[66]

    \subsection{Second second}
    \lipsum[66]
\end{document}

enter image description here

2
  • Thank you for your answer. This does seem to do the trick, if the hyperref package is loaded last in the preamble. Would you consider removing the link from the section titles good practice overall? I'm still not quite sure how you came up with this solution. Would you be willing to elaborate a bit more on the code itself? Sorry, I'm not an expert in LaTeX. Appreciate your help!
    – Thomas
    Dec 16, 2020 at 11:10
  • 1
    I would either link everything (but then use a darker color=, or only the page number. And I'm one of the current maintainer of hyperref, so I know the code a bit. Dec 16, 2020 at 11:55

You must log in to answer this question.

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