如何获取目录中的“示例列表”?

如何获取目录中的“示例列表”?

我希望我自己定义的示例在目录中显示为“示例列表”,并面临以下两个问题。

首先,在使用时tocloft,我收到错误(请参阅下面的“问题 1:”)。为什么抱怨tocloft示例环境已经定义?(这不是默认情况吗,我已经定义了一个环境并希望它出现在目录中?)。

其次,我想避免目录中章节号之前的不必要的缩进(请参阅下面的“问题 2:”;这已经适用于图表,但我想基于 tocloft- 的方法可能更简洁)。还请注意,章节/章节/小节编号与图表列表中的文本之间的间距最好更大(注释掉示例列表以查看图表列表的问题)。

注意:解决方案不一定必须与 tocloft 配合使用(请参阅下面的“历史记录”),我只是认为它可能更简单。如果有其他解决方案,那也很好,但它应该与定义的示例环境(amsthm/amstools)配合使用。最后,图表列表和示例列表应该看起来相同。

历史/背景

对于问题 1,我最初遵循的解决方案这里 但正如正确指出的那样这里 这与使用 Komascript 时给出的图表列表格式不同。另外,我没能调整它来解决问题 2。

\documentclass[listof=totoc]{scrbook}
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{tocloft}

% For examples
\declaretheoremstyle[%
  spaceabove=0.5em,
  spacebelow=0.5em,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont,
  postheadspace=\newline
]{myexstyle}
\declaretheorem[style=myexstyle, numberwithin=section]{example}

% For list of examples in toc
% Problem 1: The following fails with error "Package tocloft Error: example has been previously defined."
\newcommand{\listexamplename}{List of examples}
\newlistof[section]{example}{loe}{\listexamplename}

% For list of figures in toc
\numberwithin{figure}{section}
\renewcommand*{\listfigurename}{List of figures}
% Remove overall left indentation of all figures in "List of figures"
% Problem 2: The following works but as numbers get too large, there is not enough space
\makeatletter
\renewcommand*\l@figure{\@dottedtocline{1}{0em}{2.3em}}
\let\l@table\l@figure
\makeatother

\begin{document}
\tableofcontents
\listofexamples
\listoffigures
\clearpage
% Dummy content
\setcounter{chapter}{1}
\setcounter{section}{2}
\setcounter{example}{3}
\begin{example}
  Dummy example 1.
\end{example}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 1.}
\end{figure}
\setcounter{chapter}{10}
\setcounter{section}{11}
\setcounter{example}{12}
\begin{example}
  Dummy example 2.
\end{example}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 2.}
\end{figure}
\end{document}

更新

这是 Pietvo 首次建议的解决方案之后的修订版本。它显示除示例之外的环境也已列出。如何避免这种情况?另外,为什么 lof 和 loe 没有出现在目录中?

\documentclass[listof=totoc]{scrbook}
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{tocloft}

% For examples
\declaretheoremstyle[%
  spaceabove=0.5em,
  spacebelow=0.5em,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont,
  postheadspace=\newline
]{myexstyle}
\declaretheorem[style=mythmstyle, numberwithin=section]{definition}
\declaretheorem[style=myexstyle, sibling=definition]{example}

% Kill the counter; see https://tex.stackexchange.com/questions/332398/how-to-obtain-list-of-examples-in-the-toc
\makeatletter
\let\c@example\relax
\makeatother

% For list of examples in toc
\newcommand{\listexamplename}{List of examples}
\newlistof[section]{example}{loe}{\listexamplename}
\cftsetindents{example}{0mm}{4em}% omit left indent

% Adapted from https://tex.stackexchange.com/questions/16494/generating-lists-of-custom-environment
\makeatletter
\def\ll@example{%
  \protect\numberline{\csname the\thmt@envname\endcsname}%
  \thmt@shortoptarg}
\makeatother

% For list of figures in toc
\numberwithin{figure}{section}
\cftsetindents{fig}{0mm}{4em}% omit left indent

\begin{document}
\tableofcontents
\listofexample
\renewcommand*{\listfigurename}{List of figures}% needs to be here to be respected
\listoffigures
\clearpage
% Dummy content
\setcounter{chapter}{1}
\setcounter{section}{2}
\setcounter{example}{3}
\begin{definition}[Dummy definition header]
  Dummy definition.
\end{definition}
\begin{example}[Dummy example header 1]
  Dummy example 1.
\end{example}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 1.}
\end{figure}
\setcounter{chapter}{10}
\setcounter{section}{11}
\setcounter{example}{12}
\begin{example}[Dummy example header 2]
  Dummy example 2.
\end{example}
\begin{definition}[Dummy definition header 2]
  Dummy definition.
\end{definition}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 2.}
\end{figure}
\end{document}

答案1

此包tocloft破坏了一些 KOMA-Script 功能,例如选项listof=totoc。请注意,此包thmtools将所有定理写入扩展名为的文件中loe。因此,定义也已列出。

以下是需要当前 KOMA-Script 版本 3.21(或更新版本)的建议:

\documentclass[listof=totoc]{scrbook}[2016/06/14]% needs at least KOMA-Scritp version 3.21
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}

% For examples
\declaretheoremstyle[%
  spaceabove=0.5em,
  spacebelow=0.5em,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont,
  postheadspace=\newline
]{myexstyle}

\declaretheorem[numberwithin=section]{definition}
\declaretheorem[style=myexstyle, numberwithin=section]{example}

\newcommand*\listentrynumwidth{4.3em}
\DeclareNewTOC[
  type=example,
  tocentryindent=0pt,
  tocentrynumwidth=\listentrynumwidth
]{loe}
\DeclareTOCStyleEntry[level=1]{gobble}{definition}% remove definitions
\newcaptionname{american}{\listexamplename}{List of examples}

\numberwithin{figure}{section}% needs package amsmath
\renewcaptionname{american}{\listfigurename}{List of figures}
\DeclareTOCStyleEntry[
  indent=0pt,
  numwidth=\listentrynumwidth
]{default}{figure}
\DeclareTOCStyleEntry[
  indent=0pt,
  numwidth=\listentrynumwidth
]{default}{table}

\begin{document}
\tableofcontents
\listofexamples
\listoffigures

% Dummy content
\chapter{Example chapter 1}
\setcounter{section}{2}
\setcounter{example}{3}

\begin{definition}[Dummy definition header]
  Dummy definition.
\end{definition}
\begin{example}[Dummy example header 1]
  Dummy example 1.
\end{example}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 1.}
\end{figure}

\setcounter{chapter}{9}
\chapter{Example chapter 2}
\setcounter{section}{11}
\setcounter{example}{12}
\begin{example}[Dummy example header 2]
  Dummy example 2.
\end{example}
\begin{definition}[Dummy definition header 2]
  Dummy definition.
\end{definition}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 2.}
\end{figure}
\end{document}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

如果应该有一个示例列表您可以使用的定义列表

\documentclass{scrbook}
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}

% For examples
\declaretheoremstyle[%
  spaceabove=0.5em,
  spacebelow=0.5em,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont,
  postheadspace=\newline
]{myexstyle}

\declaretheorem[style=myexstyle, numberwithin=section]{definition}
\declaretheorem[style=myexstyle, numberwithin=section]{example}

\usepackage{scrwfile}
\TOCclone{loe}{def}
\TOCclone{loe}{exm}

\makeatletter
\doforeachtocfile{\setuptoc{\@currext}{totoc}}
\makeatother
\unsettoc{toc}{totoc}

\DeclareTOCStyleEntry[level=1]{gobble}{definition}
\DeclareTOCStyleEntry[level=1]{gobble}{example}

\newcaptionname{american}{\listexamplename}{List of examples}
\newcaptionname{american}{\listdefinitionname}{List of definitions}

\newcommand*\listentrynumwidth{4.3em}

\newcommand*\listofdefinitions{%
  \AfterTOCHead[def]{%
    \DeclareTOCStyleEntry[
      indent=0pt,
      numwidth=\listentrynumwidth
    ]{default}{definition}
  }%
  \listoftoc[{\listdefinitionname}]{def}
}
\newcommand*\listofexamples{%
  \AfterTOCHead[exm]{%
    \DeclareTOCStyleEntry[
      indent=0pt,
      numwidth=\listentrynumwidth
    ]{tocline}{example}
  }
  \listoftoc[{\listexamplename}]{exm}
}

\numberwithin{figure}{section}
\renewcaptionname{american}{\listfigurename}{List of figures}
\DeclareTOCStyleEntry[
  indent=0pt,
  numwidth=\listentrynumwidth
]{default}{figure}
\DeclareTOCStyleEntry[
  indent=0pt,
  numwidth=\listentrynumwidth
]{default}{table}

\begin{document}
\tableofcontents
\listofdefinitions
\listofexamples
\listoffigures

\cleardoublepage
% Dummy content
\chapter{Example chapter 1}
\setcounter{section}{2}
\setcounter{example}{3}
\begin{definition}[Dummy definition header]
  Dummy definition.
\end{definition}
\begin{example}[Dummy example header 1]
  Dummy example 1.
\end{example}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 1.}
\end{figure}

\setcounter{chapter}{9}
\chapter{Example chapter 2}
\setcounter{section}{11}
\setcounter{example}{12}
\begin{example}[Dummy example header 2]
  Dummy example 2.
\end{example}
\begin{definition}[Dummy definition header 2]
  Dummy definition.
\end{definition}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 2.}
\end{figure}
\end{document}

答案2

基本上,这是 tocloft 和 thmtools 之间的冲突。最简单的解决方案是删除 \declaretheorem 创建的计数器,并让 \newlistof 创建一个新计数器。tocloft 具有更好的可定制性... 布局列表。

这是一个可行的版本。

\documentclass[listof=totoc]{scrbook}
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{tocloft}

% For examples
\declaretheoremstyle[%
  spaceabove=0.5em,
  spacebelow=0.5em,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont,
  postheadspace=\newline
]{myexstyle}
\declaretheorem[style=myexstyle, numberwithin=section]{example}

% kill the counter
\makeatletter
\let\c@example\relax
\makeatother

% For list of examples in toc
% Problem 1: The following fails with error "Package tocloft Error: example has been previously defined."
\newcommand{\listexamplename}{List of examples}
\newlistof[section]{example}{loe}{\listexamplename}

% For list of figures in toc
\numberwithin{figure}{section}
\renewcommand*{\listfigurename}{List of figures}
% Remove overall left indentation of all figures in "List of figures"
% Problem 2: The following works but as numbers get too large, there is not enough space

\cftsetindents{fig}{0mm}{4em}
\cftsetindents{example}{0mm}{4em}

\begin{document}
\tableofcontents
\listofexample
\listoffigures
\clearpage
% Dummy content
\setcounter{chapter}{1}
\setcounter{section}{2}
\setcounter{example}{3}
\begin{example}
  Dummy example 1.
\end{example}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 1.}
\end{figure}
\setcounter{chapter}{10}
\setcounter{section}{11}
\setcounter{example}{12}
\begin{example}
  Dummy example 2.
\end{example}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 2.}
\end{figure}
\end{document}

相关内容