在目录中添加注释-缩进

在目录中添加注释-缩进

可能重复:
带有条目附加说明的目录

我想在目录中为每一章添加一些注释。请查看以下最小编译示例以了解这个想法。

\documentclass{article}
\usepackage{blindtext}

\newcommand{\summary}[1]{\addtocontents{toc}{\textit{#1}}}
\begin{document}
\tableofcontents

\section{First Section}
\summary{This is the introduction. It will make the reader want to continue reading.}

\subsection{Subsection 2.1}
\summary{Here we will explain in detail why this is scientificly important.}


\subsubsection{SubSubsection}
\summary{There are several elements of the argument. This is the first.}

\end{document}

这很有效,只是我想让注释和它们所属的目录条目一样缩进。

怎么做?

答案1

您可以添加与最后一个目录条目级别相同的目录条目,并附上注释。但如果最后一个条目是一个部分,则必须删除注释前的垂直空格。因此,也许:

\documentclass{article}

\usepackage{etoolbox}
\newcommand*{\lastlevel}{section}
\pretocmd{\section}{\def\lastlevel{section}}{}{}
\pretocmd{\subsection}{\def\lastlevel{subsection}}{}{}
\pretocmd{\subsubsection}{\def\lastlevel{subsubsection}}{}{}

\makeatletter
\newcommand{\l@summary}[2]{%
  \begingroup
    \let\@dotsep\@M
    \csname l@#2\endcsname{#1}{}%
  \endgroup
}

\newcommand{\summary}[1]{%
  \ifdefstring{\lastlevel}{section}{%
    \addtocontents{toc}{\protect\vspace{-1.0em plus -1pt}}%
  }{}%
  \addtocontents{toc}{%
    \protect\contentsline{summary}{\protect\numberline{}\normalfont\itshape
  #1}{\lastlevel}}%
}
\makeatother

\begin{document}
\tableofcontents

\section{First Section}
\summary{This is the introduction. It will make the reader want to continue reading.}

\subsection{Subsection 2.1}
\summary{Here we will explain in detail why this is scientificly important.}


\subsubsection{SubSubsection}
\summary{There are several elements of the argument. This is the first.}

\end{document}

如果您不想缩进到文本而只希望缩进到数字,只需删除\protect\numberline{}

\pretocmd\ifdefstring是包的命令电子工具箱。有关 的更多信息,请参阅包手册。如果您使用其他类,则可能必须更改-1.0em plus -1pt

在此处输入图片描述

相关内容