TransWikia.com

How to suppress hyperlinks for custom headers in toc but still keep hyperlink for entries and page numbers?

TeX - LaTeX Asked on March 23, 2021

I would like to use hyperlinks for page numbers and the appropriate entries in my table of contents, which works great using usepackage[linktoc=all]{hyperref} for me. Only problem I have is, that I am trying to divide my entries into different "sections" which represent headers using the tocloft package and cftaddtitleline. Each section can contain multiple entries like this:
enter image description here

For some reason, my headers have a hyperlink aswell which is not desired. I would like to have the hyperlinks only for entries and page numbers, not for the headers. Now I know I can suppress the hyperlink for the headers using usepackage[linktocpage=true]{hyperref}, but this removes my hyperlinks for the entries aswell. This is not desired, "First entry", "Second entry" and "0.1 Third Entry" should still have a hyperlink.
enter image description here

This is the desired output, which I was not able to recreate in LaTeX:
enter image description here

Here’s my code. For reusage you will have to create a folder named "folder" containing two different pdf-files named "file1.pdf" and "file2.pdf" in the local directory where your tex-file is stored. Or you can of course just use section instead of includepdf (and of course remove the appropriate arguments).

documentclass{scrreprt} % notice this is a KOMA-script class

usepackage{pdfpages}
usepackage{tocloft}
usepackage[linktoc=all]{hyperref}

begin{document}
    
    % Dummy Page
    chapter*{Dummy Page}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
    
    % Table of contents linking to pdfs
    clearpage
    tableofcontents
    
    % Assign the TOC entries to different sections of TOC
    cftaddtitleline{toc}{part}{First section of table of content}{}    
    includepdf[pages=-, addtotoc={
        1,addsec,1,First entry,p1}]{folder/file1.pdf}
    
    
    cftaddtitleline{toc}{part}{Second section of table of content}{}   
    includepdf[pages=-, addtotoc={
        1,addsec,1,Second entry,p1}]{folder/file2.pdf}
    
    section{Third entry}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
    
end{document}

Thanks for any help in advance!

One Answer

I was able to solve my problem based on this answer here: https://tex.stackexchange.com/a/301490/206040

This is the new complete code using addtocontents and contentsline before hyperref transforms them to make hyperlinks possible. For the headers I've created a new command called tocheader which can be given the header name as an argument. Also this solution does not require the usage of the tocloft package anymore.

documentclass{scrreprt} % notice this is a KOMA-script class

usepackage{pdfpages}

letorigcontentslinecontentsline
letorigaddtocontentsaddtocontents
usepackage[linktoc=all]{hyperref}

newcommand{tocheader}[1]{origaddtocontents{toc}{protectorigcontentsline{chapter}{#1}{}}}

begin{document}
    
    % Dummy Page
    chapter*{Dummy Page}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
    
    % Table of contents linking to pdfs
    clearpage
    tableofcontents
    
    % Assign the TOC entries to different sections of TOC
    tocheader{First section of table of content}
    includepdf[pages=-, addtotoc={
        1,addsec,1,First entry,p1}]{folder/file1.pdf}
    
    
    tocheader{Second section of table of content}
    includepdf[pages=-, addtotoc={
        1,addsec,1,Second entry,p1}]{folder/file2.pdf}
    
    section{Third entry}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
    
end{document}

The output now looks just like I've wanted it to look like:

enter image description here

Correct answer by owmal on March 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP