创建各种迷你目录无法打印所需的目录

创建各种迷你目录无法打印所需的目录

我将参考下面的 mwe:

\documentclass[11pt,twoside,openany]{book}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{xparse}
\let\oldsubsection\subsection
\RenewDocumentCommand{\subsection}{s o m}{%
  \IfBooleanTF{#1}
    {\oldsubsection*{#3}%
     \addcontentsline{ptc}{subsection}{#3}}
    {\IfValueTF{#2}
       {\oldsubsection[#2]{#3}}
       {\oldsubsection{#3}}}
}
\usepackage{lipsum}

\begin{document}
\tableofcontents
\chapter{Content only from section}
The ToC below should not contain the subsection* title class rather the sections only.
\startcontents[chapters]
\printcontents[chapters]{}{1}{} % allows printing of subsection only
\section{A test section}
\lipsum[1-2]
\section*{Content from subsection*}
\startcontents[sections]
\printcontents[sections]{}{2}{} % allows printing of subsection only

\clearpage

\section{Test section in ToC}\lipsum[1]
\subsection*{Test subsection now in ToC}\lipsum[2]
\subsection*{Test subsection in ToC}\lipsum[3]
\stopcontents[chapters]
\end{document}

这个问题很可能与我之前提出的问题有关。以下是我希望实现的目标:

Chapter 1
mini ToC with sections only
Section 1
mini ToC with subsections* only

看起来,使用\stopcontents[mytoc]\stopcontents[mytocc]可能有助于想象我正在处理的文档是一个包含许多章节、许多节和许多小节的文档*。

怎样才能达到想要的效果呢?

答案1

此命令将会很有用:

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

您可以使用它从内容列表中删除某个部分或小节或...,如下所示:

\tocless\section{a section}

该部分在内容列表中消失。

答案2

这是一个解决方案。我们使用

\printcontents[chapters]{}{1}{\setcounter{tocdepth}{1}}

对于 chapter minitoc 仅显示章节和

\printcontents[sections]{}{2}{
\setcounter{tocdepth}{2}
\section*{Content from subsection*}}

对于 minitoc 部分,仅显示子部分

请注意,我\section*{Content from subsection*}在这里使用\printcontents我认为这样更好。

\documentclass[11pt,twoside,openany]{book}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{xparse}
\let\oldsubsection\subsection
\RenewDocumentCommand{\subsection}{s o m}{%
  \IfBooleanTF{#1}
    {\oldsubsection*{#3}%
     \addcontentsline{ptc}{subsection}{#3}}
    {\IfValueTF{#2}
       {\oldsubsection[#2]{#3}}
       {\oldsubsection{#3}}}
}
\usepackage{lipsum}

\begin{document}
\tableofcontents
\chapter{Content only from section}
The ToC below should not contain the subsection* title class rather the sections only.
\startcontents[chapters]
\printcontents[chapters]{}{1}{\setcounter{tocdepth}{1}} 
\section{A test section}
\lipsum[1-2]
\startcontents[sections]
\printcontents[sections]{}{2}{
\setcounter{tocdepth}{2}
\section*{Content from subsection*}} % allows printing of subsection only

\clearpage

\section{Test section in ToC}\lipsum[1]
\subsection*{Test subsection now in ToC}\lipsum[2]
\subsection*{Test subsection in ToC}\lipsum[3]
\stopcontents[chapters]
\end{document}

相关内容