从目录中排除附录章节、小节和子小节

从目录中排除附录章节、小节和子小节

我想从目录中排除附录中的所有章节、子章节和子子章节。但我需要让“章节”(附录)显示在目录中。

\documentclass[a4paper,12pt,icelandic]{report}
\usepackage[toc,page]{appendix}
\begin{document}
\tableofcontents

\chapter{Chapter 1}
\section{Section 1.1}
\subsection{Section 1.1.1}
\chapter{Chapter 2}
\section{Section 2.1}
\subsection{Section 2.1.1}
\appendix
\chapter{Appendix A}
\section{Appendix A.1}        % Do not show in table of content.
\subsection{Appendix A.1.1}   % Do not show in table of content.
\chapter{Appendix B}
\section{Appendix B.1}        % Do not show in table of content.
\section{Appendix B.2}        % Do not show in table of content.
\end{document}

答案1

您必须在文件\setcounter{tocdepth}{0}中写入发布.toc时间。您可以这样做\appendix

\appendix
\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}

但在序言中这样做是更好的编程风格:

\documentclass[a4paper,12pt]{report}
\usepackage[toc,page]{appendix}

\usepackage{etoolbox}
\appto\appendix{\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}}

% reinstate the correct level for list of tables and figures
\appto\listoffigures{\addtocontents{lof}{\protect\setcounter{tocdepth}{1}}}
\appto\listoftables{\addtocontents{lot}{\protect\setcounter{tocdepth}{1}}}


\begin{document}

\tableofcontents

\chapter{Chapter 1}
\section{Section 1.1}
\subsection{Section 1.1.1}
\chapter{Chapter 2}
\section{Section 2.1}
\subsection{Section 2.1.1}

\appendix
\chapter{Appendix A}
\section{Appendix A.1}        % Do not show in table of content.
\subsection{Appendix A.1.1}   % Do not show in table of content.
\chapter{Appendix B}
\section{Appendix B.1}        % Do not show in table of content.
\section{Appendix B.2}        % Do not show in table of content.
\end{document}

在此处输入图片描述

答案2

这是未来问题的解决方案。感谢@albert

\documentclass[a4paper,12pt,icelandic]{report}
\usepackage[toc,page]{appendix}

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

\begin{document}
\tableofcontents

\chapter{Chapter 1}
\section{1}
\subsection{2}
\chapter{Chapter 2}
\section{1}
\subsection{2}
\subsubsection{3}
\appendix
\chapter{Appendix A}
\tocless\section{Appendix A.1}        % Do not show in table of content.
\tocless\subsection{Appendix A.1.1}   % Do not show in table of content.
\chapter{Appendix B}
\tocless\section{Appendix B.1}        % Do not show in table of content.
\tocless\section{Appendix B.2}        % Do not show in table of content.
\end{document}

答案3

可以将内容插入到目录中间文档中,这将影响目录的打印方式。为此,我创建了两个宏:

  • \stoptocwriting

    将计数器设置tocdepth为 -5,其中上面不存在任何节标题级别。因此,此宏后面的条目将与目录一起被删除;

  • \resumetocwriting

    将计数器重置tocdepth为序言中设置的值。这将恢复 ToC 条目的显示样式。

下面是一个展示上述宏用法的简单示例:

在此处输入图片描述

\documentclass{report}
\usepackage[toc,page]{appendix}
\newcommand{\stoptocwriting}{%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{-5}}}
\newcommand{\resumetocwriting}{%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{\arabic{tocdepth}}}}
\begin{document}
\tableofcontents

\chapter{Chapter 1}
\section{Section 1.1}
\subsection{Section 1.1.1}
\chapter{Chapter 2}
\section{Section 2.1}
\subsection{Section 2.1.1}
\appendix
\chapter{Appendix A}
\stoptocwriting
\section{Appendix A.1}        % Do not show in table of content.
\subsection{Appendix A.1.1}   % Do not show in table of content.
\resumetocwriting
\chapter{Appendix B}
\stoptocwriting
\section{Appendix B.1}        % Do not show in table of content.
\section{Appendix B.2}        % Do not show in table of content.
\resumetocwriting
\end{document}

包含.toc所有原始条目,以及的设置tocdepth

\contentsline {chapter}{\numberline {1}Chapter 1}{2}
\contentsline {section}{\numberline {1.1}Section 1.1}{2}
\contentsline {subsection}{\numberline {1.1.1}Section 1.1.1}{2}
\contentsline {chapter}{\numberline {2}Chapter 2}{3}
\contentsline {section}{\numberline {2.1}Section 2.1}{3}
\contentsline {subsection}{\numberline {2.1.1}Section 2.1.1}{3}
\contentsline {chapter}{\numberline {A}Appendix A}{4}
\setcounter {tocdepth}{-5}
\contentsline {section}{\numberline {A.1}Appendix A.1}{4}
\contentsline {subsection}{\numberline {A.1.1}Appendix A.1.1}{4}
\setcounter {tocdepth}{0}
\contentsline {chapter}{\numberline {B}Appendix B}{5}
\setcounter {tocdepth}{-5}
\contentsline {section}{\numberline {B.1}Appendix B.1}{5}
\contentsline {section}{\numberline {B.2}Appendix B.2}{5}
\setcounter {tocdepth}{0}

相关内容