如何在目录中仅显示选定的子部分

如何在目录中仅显示选定的子部分

我正在处理一份包含大量sections、subsections 和subsubsections 的文档。我只打印目录中的sections 和s,以使其不那么杂乱。但是,我需要打印目录中的某些特定 s 。我该如何实现?subsectionsubsubsectiontoc

\documentclass{article}
\usepackage[compact]{titlesec}
\titleformat{\section}{\centering\large}{}{0pt}{\Large\MakeUppercase}
\titleformat{\subsection}{\centering}{}{0pt}{\Large}
\titleformat{\subsubsection}{\centering}{}{0pt}{\bfseries}
\setcounter{tocdepth}{2}
\begin{document}
    \tableofcontents
    \section{Section One}
    \subsection{Subsection A}
    \subsubsection{Subsubsection a}
    \section{Section Two}
    \subsection{Subsection A}
    \subsubsection{Subsubsection a}
    \subsection{Subsection B}
    \subsubsection{Subsubsection a}% Want to print this subsubsection
    \subsubsection{Subsubsection b}% Want to print this subsubsection
    \subsubsection{Subsubsection c}
\end{document}

编辑:我意识到实现此目的的一种简单方法是将所有\subsubsections改为\subsubsection*s,除了我希望出现在 中的那些toc。但是,考虑到文档的长度,这将是一项艰巨的任务。作为补救措施,我尝试在序言中添加以下几行:

\let\subsubsection\oldsubsubsection
\renewcommand{\subsubsection}{\subsubsection*}
%\setcounter{tocdepth}{2}

然后我将特定的子部分称为\oldsubsubsection。但这个技巧也不起作用。请指教我哪里做错了?

答案1

您创建了无限循环,因为\subsubsection*是的另一种形式\subsubsection。正确的做法是:

\documentclass{article}
\usepackage[compact]{titlesec}
\titleformat{\section}{\centering\large}{}{0pt}{\Large\MakeUppercase}
\titleformat{\subsection}{\centering}{}{0pt}{\Large}
\titleformat{\subsubsection}{\centering}{}{0pt}{\bfseries}
\let\oldsubsubsection\subsubsection
\renewcommand{\subsubsection}{\oldsubsubsection*}
\setcounter{tocdepth}{3}
\begin{document}
    \tableofcontents
    \section{Section One}
    \subsection{Subsection A}
    \subsubsection{Subsubsection a}
    \section{Section Two}
    \subsection{Subsection A}
    \subsubsection{Subsubsection a}
    \subsection{Subsection B}
    \oldsubsubsection{Subsubsection a}% Want to print this subsubsection
    \oldsubsubsection{Subsubsection b}% Want to print this subsubsection
    \subsubsection{Subsubsection c}
\end{document}

在此处输入图片描述

相关内容