TeX - LaTeX Asked on February 3, 2021
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?
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}
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
defnumberline#1{%
ifxHy@tocdestnameltx@empty
hb@xt@@tempdima{#1hfil}%
else
hb@xt@@tempdima{hyper@linkstart{link}{Hy@tocdestname}#1hyper@linkendhfil}%
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}
Answered by Ulrike Fischer on February 3, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP