TransWikia.com

Exclude headings from the table of contents when you insert two TOCs

TeX - LaTeX Asked on July 9, 2021

I want to added two TOCs within the same document, but I want to exclude the headings added with addcontentsline{toc}{chapter}{Conclusion}, to be excluded from one of them.

documentclass{report}

usepackage{hyperref}

begin{document}

setcounter{tocdepth}{1}
setcounter{secnumdepth}{3}

{expandafterdefcsname @starttocendcsname#1{InputIfFileExists{jobname.#1}{}{}}%
tableofcontents}

renewcommand*contentsname{Contents (detailed)}

setcounter{tocdepth}{3}

tableofcontents

chapter{XXX}
chapter*{Conclusion}%
addcontentsline{toc}{chapter}{Conclusion}%to be excluded
section{YYY}
subsection{WER}
subsubsection{ABC}
subsubsection{DEF}
subsection{ZTR}
subsubsection{OIU}
subsubsection{OIZ}
subsubsection{POI}
section{YYY}
subsection{WER}
subsubsection{EDC}
subsubsection{RFV}
subsection{TGB}
subsubsection{ZHN}
subsubsection{OUH}
subsubsection{WEK}
chapter{ZZZ}
chapter{AAA}

end{document} 

2 Answers

I have defined a specialchapter entry type that can be included or excluded on demand.

documentclass{report}

usepackage{hyperref}

begin{document}

setcounter{tocdepth}{1}
setcounter{secnumdepth}{3}

makeatletter
newcommandexcludespecialchapter{defl@specialchapter##1##2{}}
newcommandincludespecialchapter{letl@specialchapterl@chapter}
makeatother

{expandafterdefcsname @starttocendcsname#1{InputIfFileExists{jobname.#1}{}{}}%
includespecialchapter
tableofcontents}

renewcommand*contentsname{Contents (detailed)}

setcounter{tocdepth}{3}

excludespecialchapter
tableofcontents

chapter{XXX}
chapter*{Conclusion}%
addcontentsline{toc}{specialchapter}{Conclusion}%to be excluded
section{YYY}
subsection{WER}
subsubsection{ABC}
subsubsection{DEF}
subsection{ZTR}
subsubsection{OIU}
subsubsection{OIZ}
subsubsection{POI}
section{YYY}
subsection{WER}
subsubsection{EDC}
subsubsection{RFV}
subsection{TGB}
subsubsection{ZHN}
subsubsection{OUH}
subsubsection{WEK}
chapter{ZZZ}
chapter{AAA}

end{document} 

enter image description here

enter image description here

Correct answer by Pieter van Oostrum on July 9, 2021

Another approach:

documentclass{report}
usepackage{hyperref}

newififtocdetailed
makeatletter
defstartDetailedOnly{
    write@auxout{protect@writefile{toc}{protectcsname @startDetailedOnlyprotectendcsname}}
}

defendDetailedOnly{
    write@auxout{protect@writefile{toc}{protectcsname @endDetailedOnlyprotectendcsname}}
}

def@startDetailedOnly{
    unlessiftocdetailed
        xdefrestoreTocDepth{arabic{tocdepth}}
        setcounter{tocdepth}{-1}
    fi
}
def@endDetailedOnly{
    unlessiftocdetailed
        defsettocval##1{    
            setcounter{tocdepth}{##1}
        }
        expandaftersettocvalexpandafter{restoreTocDepth}
    fi
}

makeatother

begin{document}

setcounter{tocdepth}{1}
setcounter{secnumdepth}{3}

{expandafterdefcsname @starttocendcsname#1{InputIfFileExists{jobname.#1}{}{}}%
tableofcontents}

renewcommand*contentsname{Contents (detailed)}

setcounter{tocdepth}{3}

tocdetailedtrue
tableofcontents

chapter{XXX}

startDetailedOnly

chapter*{Conclusion}%
addcontentsline{toc}{chapter}{Conclusion}%to be excluded
section{YYY}
subsection{WER}
subsubsection{ABC}
subsubsection{DEF}
subsection{ZTR}
subsubsection{OIU}
subsubsection{OIZ}
subsubsection{POI}
section{YYY}
subsection{WER}
subsubsection{EDC}
subsubsection{RFV}
subsection{TGB}
subsubsection{ZHN}
subsubsection{OUH}
subsubsection{WEK}

endDetailedOnly

chapter{ZZZ}
chapter{AAA}

end{document}

When you use startDetailedOnly, all chapter's, section's and such until the following endDetailedOnly will be recorded, but only be shown in a TOC if you set tocdetailedtrue. This is done by changing tocdepth for non-detailed TOC's.

Answered by Vincent Kuhlmann on July 9, 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