有没有办法获取枚举中出现的项目数?我的目的是当我在列表中的任何位置添加新项目时自动更新该数字,因此像这样的东西将是理想的:
Here are the \numitems{mylist} points I wanted to address:
\begin{itemize}\label{mylist}
\item important issue;
\item another important issue;
\item oh, and yet another critical thing.
\end{itemize}
答案1
itemize
不使用计数器(带有\refstepcounter)
)——那么\label
是无用的(并且如果它应该使用,它必须在该last
项目之后,而不是在第一个之前。
我对itemize
环境进行了一些修补,并添加了一个新计数器,每次\item
使用时都会重置并步进。
\getrefnumber{...}
从包中使用refcount
以获取可扩展的数字。
\documentclass{article}
\usepackage{xpatch}
\usepackage{refcount}
\newcommand{\numitems}[1]{\getrefnumber{#1}}
\newcounter{itemcntr}
\AtBeginEnvironment{itemize}{%
\setcounter{itemcntr}{0}%
\xapptocmd{\item}{\refstepcounter{itemcntr}}{}{}%
}
\begin{document}
Here are the \numitems{mylist} points I wanted to address:
\begin{itemize}
\item important issue;
\item another important issue;
\item oh, and yet another critical thing. \label{mylist}
\end{itemize}
Here are the \numitems{otherlist} points I wanted to address:
\begin{itemize}
\item important issue;
\item another important issue;
\item almost forgotten stuff;
\item oh, and yet another critical thing. \label{otherlist}
\end{itemize}
\end{document}
答案2
下面允许enumitem
与 的交互countlist
,也许还有更简单的界面:您指定一个countlist
具有强制参数的环境,该参数将引用列表。还允许使用可选参数,该参数以典型方式\label
传递给环境。enumerate
enumitem
refcount
提供参考的数字显示,尽管在这种情况下实际上不需要。
\documentclass{article}
\usepackage{enumitem,refcount}
\newcounter{countlist}
\makeatletter
\newenvironment{countlist}[2][]
{\begin{enumerate}[#1]
\setcounter{countlist}{0}%
\def\countname{#2}%
\let\olditem\item
\renewcommand{\item}{\stepcounter{countlist}\olditem}}
{ \renewcommand{\@currentlabel}{\arabic{countlist}}%
\label{\countname}%
\end{enumerate}}
\makeatother
\newcommand{\numitems}[1]{\getrefnumber{#1}}
% Alternatively...
%\newcommand{\numitems}[1]{\ref{#1}}
\begin{document}
Here are the \numitems{mylist} points I wanted to address:
\begin{countlist}{mylist}
\item important issue;
\item another important issue;
\item oh, and yet another critical thing.
\end{countlist}
Here are the \numitems{otherlist} points I wanted to address:
\begin{countlist}[label={(\Alph*)}]{otherlist}
\item important issue;
\item[(b*)] another important issue;
\item almost forgotten stuff;
\item oh, and yet another critical thing.
\end{countlist}
\end{document}
如果您想要itemize
列表具有 -like 的感觉,可以使用 的默认label={\textbullet}
可选参数enumerate
。
答案3
一些有创意的用法enumitem
:
\documentclass{article}
\usepackage{enumitem}
\newcounter{itemcount}
\newenvironment{countitemize}[1]
{\def\countitemizelabel{#1}%
\setcounter{itemcount}{0}%
\itemize[label=\stepcounter{itemcount}\textbullet]}
{\addtocounter{itemcount}{-1}%
\refstepcounter{itemcount}\label{\countitemizelabel}%
\enditemize}
\begin{document}
Here are the \ref{mylist} points I wanted to address:
\begin{countitemize}{mylist}
\item important issue;
\item another important issue;
\item oh, and yet another critical thing.
\end{countitemize}
Let's check the formatting
\begin{itemize}
\item important issue;
\item another important issue;
\item oh, and yet another critical thing.
\end{itemize}
\end{document}
如果你想计算每一个的实例\item
,那么你可以做
\documentclass{article}
\usepackage{etoolbox}
\newcounter{itemcount}
\newenvironment{countitemize}[1]
{\def\countitemizelabel{#1}%
\setcounter{itemcount}{0}%
\preto\item{\stepcounter{itemcount}}
\itemize}
{\addtocounter{itemcount}{-1}%
\refstepcounter{itemcount}\label{\countitemizelabel}%
\enditemize}
\begin{document}
Here are the \ref{mylist} points I wanted to address:
\begin{countitemize}{mylist}
\item important issue;
\item another important issue;
\item oh, and yet another critical thing.
\end{countitemize}
Let's check the formatting
\begin{itemize}
\item important issue;
\item another important issue;
\item oh, and yet another critical thing.
\end{itemize}
\end{document}
但是,在嵌套环境中计数仍会继续。如果您需要,可以解决这个问题。
我提供countitemize
最多 3 级嵌套。
\documentclass{article}
\usepackage{etoolbox}
\newcounter{itemcounti}
\newcounter{itemcountii}
\newcounter{itemcountiii}
\makeatletter
\newif\ifnestedcountitemize
\newenvironment{countitemize}[1]
{\ifnestedcountitemize\else
\preto\item{\stepcounter{itemcount\romannumeral\@listdepth}}%
\nestedcountitemizetrue
\fi
\begin{itemize}%
\def\countitemizelabel{#1}%
\setcounter{itemcount\romannumeral\@listdepth}{0}}
{\addtocounter{itemcount\romannumeral\@listdepth}{-1}%
\refstepcounter{itemcount\romannumeral\@listdepth}\label{\countitemizelabel}%
\end{itemize}}
\makeatother
\begin{document}
Here are the \ref{mylist} points I wanted to address:
\begin{countitemize}{mylist}
\item important issue;
\item another important issue;
\item oh, and yet another critical thing.
\begin{countitemize}{innerlist}
\item With a nested list
\item again
\end{countitemize}
\end{countitemize}
There is an inner list with \ref{innerlist} items.
\end{document}