2

I have two TOC in my document, one for the hole document ("main" TOC) and one for only the appendix. I want to remove all sections and subsections from appendix in the "main" TOC but not in the TOC for appendix. Is this possible without re-coding everything. I have tied using:

\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egrou

with

\tocless\section{section name}

But with this i lose the subsection in the smaller TOC for the appendix. I have tried different placement of the command above but that does't work.

This is the code I have now that generate my two TOC:

\usepackage{titletoc}

\documentclass[a4paper, 12pt]{book}

\begin{document}   

%first TOC at beginning of document:

\tableofcontents

%Second TOC just before Appendix for the Appendix:

\appendix\addcontentsline{toc}{chapter}{Appendix}        
\startcontents
\renewcommand{\contentsname}{Appendix}
\printcontents{}{0}{%
\setcounter{tocdepth}{2}%
\tableofcontents}

Anybody know how to solve this? J

  • Why not use the starred version of section* subsection* in the documents appendix, which does not give a TOC entry? – Bernhard Kleine May 19 '16 at 14:15
  • This does not solve my problem what i can see. If i use section* and subsection* these disappears from both TOC's. I want them to remain in the TOC for the appendix but not in the main TOC... – mwasberg May 19 '16 at 14:24
  • Have you ever used the minitoc package? there are different tocdepth versus minitocdepth possible. – Bernhard Kleine May 19 '16 at 14:38
  • See the manual, sec. 4.2. – Javier Bezos May 19 '16 at 14:42
3

Here is a solution.

Use

\addcontentsline{toc}{chapter}{Appendix}
\addtocontents{toc}{\setcounter{tocdepth}{0}}    %for main toc
\addtocontents{ptc}{\setcounter{tocdepth}{2}}    %for appendix toc

Update because of expansion (issue with calc package see comments) it's better to use

\addcontentsline{toc}{chapter}{Appendix}
\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
\addtocontents{ptc}{\protect\setcounter{tocdepth}{2}}

\documentclass[a4paper, 12pt]{book}
\usepackage{titletoc}
\usepackage{blindtext}
\begin{document}
\tableofcontents
\blinddocument
\blinddocument

%Second TOC just before Appendix for the Appendix:
\cleardoublepage
\appendix
\addcontentsline{toc}{chapter}{Appendix}
\addtocontents{toc}{\setcounter{tocdepth}{0}}
\addtocontents{ptc}{\setcounter{tocdepth}{2}}
\startcontents
\renewcommand{\contentsname}{Appendix}
\printcontents{}{0}{%
\setcounter{tocdepth}{2}%
\tableofcontents}
\blinddocument
\blinddocument

\end{document}

main toc

enter image description here

Appendix toc

enter image description here

| improve this answer | |
  • Big thanks for this, but i cant get it to work as above. When using: \addtocontents{toc}{\setcounter{tocdepth}{0}} and \addtocontents{ptc}{\setcounter{tocdepth}{2}} i get warnings. If i skip the TOC for the appendix part and only use \addtocontents{toc}{\setcounter{tocdepth}{0}} just after \appendix i still get warnings. Is there a usepackage i'm missing, because after this command logical this should work and only show appendix heading on level 0 (chapter?) in the main TOC. – mwasberg May 20 '16 at 7:33
  • I also had \stopcontents at the end of the document, is that needed? – mwasberg May 20 '16 at 7:38
  • This is the two warnings i get: ! Undefined control sequence.\@calc@post@scan ...st@scan \else \def \calc@next{\calc@error #1}\fi \fi \f... ...ocontents{toc}{\setcounter{tocdepth}{1}} and ! Undefined control sequence.\@calc@post@scan ...fi \fi \fi \fi \fi \calc@next ...ocontents{toc}{\setcounter{tocdepth}{1}} – mwasberg May 20 '16 at 8:02
  • in this case (end of document) \stopcontents is optional. – touhami May 20 '16 at 8:12
  • 1
    As I can see it seems to be a problem with calc package please try with \addtocontents{toc}{\protect\setcounter{tocdepth}{0}} \addtocontents{ptc}{\protect\setcounter{tocdepth}{2}} – touhami May 20 '16 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.
https://tex.stackexchange.com/questions/310446/remove-section-subsection-from-one-of-two-toc
tex.stackexchange.com
crawl=CC-MAIN-2020-24
152328
application/http; msgtype=response
sha1:FRNE2O3WLHG2CM7D6KRYDEBKB7HHBZ6M
<urn:uuid:771b3428-ab21-4328-a951-870118db4a01>
2020-05-28T13:18:57Z
151.101.1.69
text/html
sha1:GEKIQ4FIPLBQWQ6LHD5BYB6MYEINRBSP
<urn:uuid:4f026de0-c585-4e61-8979-47795a98a973>
https://tex.stackexchange.com/questions/310446/remove-section-subsection-from-one-of-two-toc
response
<urn:uuid:2382034e-cc23-4d3d-b146-6bc2f2e08df7>
isPartOf: CC-MAIN-2020-24
publisher: Common Crawl
description: Wide crawl of the web for May/June 2020
operator: Common Crawl Admin (info@commoncrawl.org)
hostname: ip-10-67-67-182.ec2.internal
software: Apache Nutch 1.16 (modified, https://github.com/commoncrawl/nutch/)
robots: checked via crawler-commons 1.1-SNAPSHOT (https://github.com/crawler-commons/crawler-commons)
format: WARC File Format 1.1
conformsTo: http://iipc.github.io/warc-specifications/specifications/warc-format/warc-1.1/