在编写 LaTeX 代码时,我经常会连续进行更改,例如添加或删除项目。我希望 TeX 自动计数并更新列表中的项目数量,如下所示
There are \numItemsNextList ways how a parameter can be given:
\begin{itemize}
\item By a constant expression.
\item By user interaction.
\item From another database.
\end{itemize}
(接下来发生的事情当然是我意识到还有另一种方法来提供参数并将其添加到列表中)
我可以推出自己的预处理器来计算\item
以下\begin{...} ... \end{...}
块中的命令数量。但是是否也有 TeX 解决方案?
答案1
这里有一种方法:我们itemize
用etoolbox
工具修改环境;每个\item
命令都会步进一个计数器;每个环境都会步进另一个计数器。可能需要运行几次 LaTeX 才能使数字稳定下来。
\documentclass{article}
\usepackage{etoolbox,refcount}
\newcounter{countitems}
\newcounter{nextitemizecount}
\newcommand{\setupcountitems}{%
\stepcounter{nextitemizecount}%
\setcounter{countitems}{0}%
\preto\item{\stepcounter{countitems}}%
}
\makeatletter
\newcommand{\computecountitems}{%
\edef\@currentlabel{\number\c@countitems}%
\label{countitems@\number\numexpr\value{nextitemizecount}-1\relax}%
}
\newcommand{\nextitemizecount}{%
\getrefnumber{countitems@\number\c@nextitemizecount}%
}
\makeatother
\AtBeginEnvironment{itemize}{\setupcountitems}
\AtEndEnvironment{itemize}{\computecountitems}
\begin{document}
There are \nextitemizecount{} ways how a parameter can be given:
\begin{itemize}
\item By a constant expression.
\item By user interaction.
\item From another database.
\end{itemize}
There are \nextitemizecount{} ways how a parameter can be given:
\begin{itemize}
\item By a constant expression.
\item By user interaction.
\item From another database.
\item Foo.
\end{itemize}
The next doesn't count.
\begin{itemize}
\item A
\item B
\end{itemize}
There are \nextitemizecount{} ways how a parameter can be given:
\begin{itemize}
\item By a constant expression.
\item By user interaction.
\end{itemize}
\end{document}
答案2
我不确定如何使用itemize
。但对于enumerate
环境,您可以label
在最后添加一个item
。
\documentclass{article}
\begin{document}
There are \ref{lst:num} ways how a parameter can be given:
\begin{enumerate}
\item By a constant expression.
\item By user interaction.
\item From another database.
\label{lst:num}
\end{enumerate}
\end{document}
答案3
在enumitem
包中,您可以基于 定义一个列表enumerate
,但其行为与itemize
标签有关。这样,就可以放置\label
s,特别是最后一个项目。以下示例说明了这种定义(针对具有四级嵌套的列表环境),并显示了该方法适用于嵌套:
\documentclass{article}
\usepackage{enumitem}
\newlist{citemize}{enumerate}{4}
\setlist[citemize,1]{label=$\bullet$,ref=\arabic*}
\setlist[citemize,2]{label=--,ref=\arabic*}
\setlist[citemize,3]{label=$\ast$,ref=\arabic*}
\setlist[citemize,4]{label=$\cdot$,ref=\arabic*}
\begin{document}
There are~\ref{lst:num} ways how a parameter can be given, and user interaction has~\ref{lst:subnum} possibilities:
\begin{citemize}
\item By a constant expression.
\item By user interaction.
\begin{citemize}
\item First subitem.
\item Second subitem.
\label{lst:subnum}
\end{citemize}
\item From another database.
\label{lst:num}
\end{citemize}
\end{document}
答案4
软件包xcntperchap
可以通过一些“简单”的设置,使用虚拟轨道级别计数器(dummycntr
)来实现这一点。由于itemize
本身不使用计数器,因此需要另一个计数器(因为enumerate
,这会容易得多,只需说出来\RegisterCounters{dummycntr}{enumi}
即可。
itemize
当前版本目前仅适用于第一级嵌套。
\documentclass{article}
\usepackage{xpatch}
\usepackage{xcntperchap}
\newcounter{dummycntr}
\newcounter{itemi}
\RegisterCounters{dummycntr}{itemi}
\xpretocmd{\item}{\stepcounter{itemi}}{}{}
\xpretocmd{\itemize}{\stepcounter{dummycntr}}{}{}
\begin{document}
There are \ObtainTrackedValue[1]{dummycntr}{itemi} ways how a parameter can be given, but see also that there are
\ObtainTrackedValue[2]{dummycntr}{itemi} other ways:
\begin{itemize}
\item By a constant expression.
\item By user interaction.
\item From another database.
\end{itemize}
Other ways:
\begin{itemize}
\item This
\item Is
\item Some
\item Other
\item Itemization
\end{itemize}
\end{document}
的未来版本xcntperchap
将会更容易支持此类功能!