Disable table of contents hyperlink for individual entries

hyperreftable of contents

Consider the following MWE that creates a TOC with three entries, all are linked to their respective sections in the document.

\documentclass{scrartcl}
\usepackage{hyperref}

\begin{document}

\tableofcontents
\clearpage
\section{Section with link}
\clearpage
\section{Section without link}
% \section*{Section without link}
% \addcontentsline{toc}{section}{Section without link}

\clearpage
\section{Section without link 2}

\end{document}

I would like to disable the links only for the latter entries. Is there a solution for this that I can't find? I'm glad for any hints!


Background:
In my final document the latter sections will include external PDFs (reference letters). This means I will throw away the page with the actual section headings and merge the document with the other PDFs. This way the page numbers in the TOC will be correct but the link would lead to page one (since the actual page does no longer exist) – therefore I'd rather have no link at all.

I know I could use something like pdfpages to just include the PDFs directly but this destroys hyperlinks in the included documents and therefore is not an option for me.

Best Answer

There is a package option linktoc=none in hyperref that disables links in the TOC. You can turn on this option midway through the TOC by changing the value of the corresponding macro \Hy@linktoc from within the .toc file.

This can be done by using the low-level \addtocontents{ext}{...} macro that writes the second argument into the file with the extension ext. In this case:

\addtocontents{toc}{\let\Hy@linktoc\Hy@linktoc@none}

Full MWE, including \makeatletter and \makeatother:

\documentclass{scrartcl}
\usepackage{hyperref}

\begin{document}
\tableofcontents
\clearpage
\section{Section with link}
\clearpage
\makeatletter
\addtocontents{toc}{\let\Hy@linktoc\Hy@linktoc@none}
\makeatother
\section{Section without link}

\clearpage
\section{Section without link 2}

\end{document}

This creates the following .toc file:

\contentsline {section}{\numberline {1}Section with link}{2}{section.1}%
\let \Hy@linktoc \Hy@linktoc@none 
\contentsline {section}{\numberline {2}Section without link}{3}{section.2}%
\contentsline {section}{\numberline {3}Section without link 2}{4}{section.3}%

The link targets section.2 and section.3 are still generated, but because of the option setting they will not show as actual links.

Result:

enter image description here

Related Question