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:

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?

Any help is much appreciated. Thank you in advance.

MWE

\documentclass{article}
\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}


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{lipsum}
\makeatletter
\def\numberline#1{%
\ifx\Hy@tocdestname\ltx@empty
\hb@xt@\@tempdima{#1\hfil}%
\else
\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}


• 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 '20 at 11:10
• 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. – Ulrike Fischer Dec 16 '20 at 11:55