newcounter
我在 LaTeX 文档中设置了:
\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
\textbf{Example~\theexample. #1} \rmfamily}{\medskip}
以便我可以在整个文档中引用它们:
\begin{example}[title]
This is the first example. The counter will be reset at each section.
\end{example}
我的问题是,我如何生成所有示例的列表?与图表列表类似,我希望能够获得示例列表,其中包含它们的标题和它们出现的页码。这可能吗?
答案1
一种“快速而又粗糙”的解决方案,使用\newlistof
来自包的命令tocloft
。
\documentclass{article}%
\usepackage{blindtext}
\usepackage[titles]{tocloft}
\usepackage{etoolbox}
\newcounter{example}[section]
\renewcommand{\theexample}{\arabic{section}.\arabic{example}}%
\newenvironment{example}[1][]{%
\refstepcounter{example}%
\ifblank{#1}{%
}{%
\addcontentsline{exp}{examples}{\protect\numberline{\theexample}#1}%
}%
\par\medskip%
\noindent\textbf{Example~\theexample. #1} \rmfamily}{\medskip}%
\newcommand{\listexampletitle}{List of Examples}
\newlistof[section]{examples}{exp}{\listexampletitle}%
\cftsetindents{examples}{1.5em}{3.0em}%
%\cftpagenumbersoff{examples}
\setlength{\cftexamplesnumwidth}{1.5cm}
\begin{document}
\section{Introduction}%
\listofexamples
\begin{example}[title]
This is the first example. The counter will be reset at each section.
\end{example}
\begin{example}
This is the 2nd example, but has no entry to the toc!
\end{example}
\section{A section}
\blindtext[1]
\begin{example}[Another title]
\blindtext[2]
Another example.
\end{example}
\begin{example}[Yet Another title]
\blindtext[3]%
Yet Another example.
\end{example}
\end{document}
另一个没有章节编号的版本作为示例
免责声明我不建议删除示例的章节编号,因为这将导致更难识别哪个示例指的是哪个示例,特别是如果示例具有相同的标题(由于某种原因)
\documentclass{article}%
\usepackage{blindtext}
\usepackage[titles]{tocloft}
\usepackage{etoolbox}
\newcounter{example}
\renewcommand{\theexample}{\arabic{example}}% To be on the safe side!
\newenvironment{example}[1][]{%
\refstepcounter{example}%
\ifblank{#1}{%
}{%
\addcontentsline{exp}{examples}{\protect\numberline{\theexample}#1}%
}%
\par\medskip%
\noindent\textbf{Example~\theexample. #1} \rmfamily}{\medskip}%
\newcommand{\listexampletitle}{List of Examples}
\newlistof[section]{examples}{exp}{\listexampletitle}%
\cftsetindents{examples}{1.5em}{3em}% Some indentation -- change to 0pt if needed
%\cftpagenumbersoff{examples}
\setlength{\cftexamplesnumwidth}{1.5cm}%
\begin{document}
\section{Introduction}%
\listofexamples
\begin{example}[title]
This is the first example. The counter will be reset at each section.
\end{example}
\begin{example}
This is the 2nd example, but has no entry to the toc!
\end{example}
\section{A section}
\blindtext[1]
\begin{example}[Another title]
\blindtext[2]
Another example.
\end{example}
\begin{example}[Yet Another title]
\blindtext[3]%
Yet Another example.
\end{example}
\end{document}
答案2
使用该包可以很容易地完成此ntheorem
操作:我定义example
为一个新定理,其计数器在每个部分重置,然后使用以下\listtheorems{example}
命令:
\documentclass[12pt,a4paper, x11names]{article}%]
\usepackage[utf8]{inputenc}%
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[thmmarks, amsmath, framed]{ntheorem}%
\theoremstyle{plain}
\theoremheaderfont{\bfseries}
\theorembodyfont{\mdseries}
\theoremseparator{.}
\newtheorem{example}{Example}[section]
\newcommand\listofexamples{\section*{List of Examples}\listtheorems{example}}
\begin{document}
\section{A First Section}
\begin{example}[Title]
This is the first example. The counter will be reset at each section.
\end{example}
\begin{example}[Title the Second]
This is the second example of the first section.
\end{example}
\section{Another Section}
\begin{example}[Title title]
This is the first example of the second section. The counter was reset.
\end{example}
\listofexamples
\end{document}
答案3
另一种方法是使用 KOMA-script 附带的 tocbasic 包。您可以使用简单的键值对轻松设置浮动环境(或者如果您愿意,也可以设置非浮动环境)。您知道的所有宏列表都在那里。该列表看起来就像您的图表和表格列表(如果您以安全的方式修改了它们)。
例如,您可以使用 caption 包来设置环境样式。如果您使用 hyperref 包及其 \autoref 功能,惊喜的是... 也能正常工作 :-)
如果您每节都重置计数器,\thecounter
输出会更长,看起来会很难看。包tocstyle
和它的
tocautoindent
功能会有所帮助。您需要运行几次 LaTeX,直到正确计算出所需的空间。
\documentclass{article}%
\usepackage[tocindentauto]{tocstyle}
\usepackage{blindtext}
\usepackage{tocbasic}
\DeclareNewTOC[%
type=example,
counterwithin=section,
float,
%nonfloat, %If you don't want them to float
name=Example,
listname={List of Examples},
]{xpl}
\usepackage{hyperref}
%\hypersetup{hidelinks}
\begin{document}
\listoffigures
\listofexamples\clearpage
\section{Introduction}%
\begin{example}
\caption{title}
This is the first example. The counter will be reset at each section.
\end{example}
\begin{example}
\caption{example with ducks}
Duck and cover!
\end{example}
\section{Quack}
Ducks are quite nice creatures, find a picture of a duck in
\autoref{xpl:duck}.
\blindtext[1]
\begin{example}
\caption{More Ducks}
\label{xpl:duck}
\blindtext[2]
\rule{5cm}{2cm}
Another example.
\end{example}
\begin{example}
\caption{funny penguins}
\blindtext[3]%
Yet Another example.
\end{example}
\begin{figure}
\caption{figure}
\caption{figure}
\caption{figure}
\end{figure}
\end{document}
使用标准 hyperref 显示链接