插入两个目录时从目录中排除标题

插入两个目录时从目录中排除标题

我想在同一个文档中添加两个目录,但我想排除使用 添加的标题\addcontentsline{toc}{chapter}{Conclusion},将其从其中一个中排除。

\documentclass{report}

\usepackage{hyperref}

\begin{document}

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

{\expandafter\def\csname @starttoc\endcsname#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} 

答案1

我已经定义了一种specialchapter可以根据需要包含或排除的条目类型。

\documentclass{report}

\usepackage{hyperref}

\begin{document}

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

\makeatletter
\newcommand\excludespecialchapter{\def\l@specialchapter##1##2{}}
\newcommand\includespecialchapter{\let\l@specialchapter\l@chapter}
\makeatother

{\expandafter\def\csname @starttoc\endcsname#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} 

在此处输入图片描述

在此处输入图片描述

答案2

另一种方法:

\documentclass{report}
\usepackage{hyperref}

\newif\iftocdetailed
\makeatletter
\def\startDetailedOnly{
    \write\@auxout{\protect\@writefile{toc}{\protect\csname @startDetailedOnly\protect\endcsname}}
}

\def\endDetailedOnly{
    \write\@auxout{\protect\@writefile{toc}{\protect\csname @endDetailedOnly\protect\endcsname}}
}

\def\@startDetailedOnly{
    \unless\iftocdetailed
        \xdef\restoreTocDepth{\arabic{tocdepth}}
        \setcounter{tocdepth}{-1}
    \fi
}
\def\@endDetailedOnly{
    \unless\iftocdetailed
        \def\settocval##1{    
            \setcounter{tocdepth}{##1}
        }
        \expandafter\settocval\expandafter{\restoreTocDepth}
    \fi
}

\makeatother

\begin{document}

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

{\expandafter\def\csname @starttoc\endcsname#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}

当您使用 时\startDetailedOnly,所有\chapter\section和诸如此类的内容直到以下内容\endDetailedOnly都将被记录,但只有当您设置 时才会显示在目录中\tocdetailedtrue。这是通过更改tocdepth为非详细目录来实现的。

相关内容