如何在目录中仅包含命名的章节或子章节

如何在目录中仅包含命名的章节或子章节

我在一篇文章中有很多小章节和子章节。有些有标题,有些没有。是否可以创建一个目录,其中只显示命名的章节/子章节?

\chapter{My chapter}
\section{Alice}
\section{}
\subsection{}
\subsection{Ava}
\section{Bob}
\section{}
\section{}
\section{Charles}

Alice, Ava, Bob, Charles我只希望目录中显示的章节和小节的名称。

答案1

标准班你可以修补\@sect

\documentclass{report}
\usepackage{xpatch}
\let\originaladdcontentsline\addcontentsline
\makeatletter
\xpatchcmd\@sect
  {\addcontentsline {toc}{#1}{\ifnum #2>\c@secnumdepth \else \protect \numberline {\csname the#1\endcsname }\fi #7}}
  {%
    \if!#7!\else
      \originaladdcontentsline {toc}{#1}{\ifnum #2>\c@secnumdepth \else \protect \numberline {\csname the#1\endcsname }\fi #7}%
    \fi
  }{}{\PatchFailed}
\xpatchcmd\@sect
  {\addcontentsline {toc}{#1}{\ifnum #2>\c@secnumdepth \else \protect \numberline {\csname the#1\endcsname }\fi #7}}
  {%
    \if!#7!\else
    \originaladdcontentsline {toc}{#1}{\ifnum #2>\c@secnumdepth \else \protect \numberline {\csname the#1\endcsname }\fi #7}%
    \fi
  }{}{\PatchFailed}
\makeatother
\begin{document}
\tableofcontents
\chapter{My chapter}
\section{Alice}
\section{}
\subsection{}
\subsection{Ava}
\section{Bob}
\section{}
\section{}
\section{Charles}
\end{document}

结果:

在此处输入图片描述

但你也可以使用KOMA-Script 类。那么默认情况下,空部分标题将没有目录条目:

\documentclass{scrreprt}
\begin{document}
\tableofcontents
\chapter{My chapter}
\section{Alice}
\section{}
\subsection{}
\subsection{Ava}
\section{Bob}
\section{}
\section{}
\section{Charles}
\end{document}

结果:

在此处输入图片描述

答案2

您需要重新定义呈现 toc 条目的 tex 宏。也就是说,您需要编辑的内容取决于文档类和包含在文档中的包。对于最简单的 latex 书,没有使用任何包,这将是类似

\makeatletter
\def\l@section#1#2{%
  \global\let\oldnumline\numberline
  \def\numberline##1{}%
  \if!#1!
    \global\let\numberline\oldnumline
  \else
    \global\let\numberline\oldnumline
    \@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}%
  \fi}
\def\l@subsection#1#2{%
  \global\let\oldnumline\numberline
  \def\numberline##1{}%
  \if!#1!
    \global\let\numberline\oldnumline
  \else
    \global\let\numberline\oldnumline
    \@dottedtocline{2}{3.8em}{3.2em}{#1}{#2}%
  \fi}
\makeatother

您需要将其放在之前\begin{document}

答案3

etoc您可以同时拥有两种类型的目录(使用标准类)。

\documentclass{report}

\usepackage{etoc}

\newcommand{\trimmedtableofcontents}{%
\begingroup
\etocsetstyle{chapter}
     {}
     {\ifx\etocthename\empty
      \else
        \etocifnumbered
        {\etocsavedchaptertocline
           {\numberline{\etocnumber}\etocname}{\etocpage}}
        {\etocsavedchaptertocline{\etocname}{\etocpage}}%
      \fi
     }%
     {}
     {}
\etocsetstyle{section}
     {}
     {\ifx\etocthename\empty
      \else
        \etocifnumbered
        {\etocsavedsectiontocline
           {\numberline{\etocnumber}\etocname}{\etocpage}}
        {\etocsavedsectiontocline{\etocname}{\etocpage}}%
      \fi
     }%
     {}
     {}
\etocsetstyle{subsection}
     {}
     {\ifx\etocthename\empty
      \else
        \etocifnumbered
        {\etocsavedsubsectiontocline
           {\numberline{\etocnumber}\etocname}{\etocpage}}
        {\etocsavedsubsectiontocline{\etocname}{\etocpage}}%
      \fi
     }%
     {}
     {}
\tableofcontents
\endgroup
}

\begin{document}
\trimmedtableofcontents
\renewcommand\contentsname{Full table of contents}
\tableofcontents
\chapter{My chapter}
\section{Alice}
\section{}
\subsection{}
\subsection{Ava}
\section{Bob}
\section{}
\section{}
\section{Charles}
\end{document}

第一页:

在此处输入图片描述

第二页:

在此处输入图片描述

相关内容