# Counting items in itemize or enumerate environments [duplicate]

Is there anyway to obtain the number of items that appear in an enumeration? My intention is to have that number updated automatically when I add a new item anywhere in my list, so something like this would be ideal:

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}


## marked as duplicate by Mensch, Circumscribe, AndréC, Sebastiano, TiuriJan 26 at 20:02

• Since itemize does not use a counter really, \label (and it's at the wrong position anyway) is useless here. Do you really want to have an itemize or rather an enumerate list? – user31729 Feb 26 '16 at 13:24

itemize does not use a counter (with \refstepcounter)) -- The \label is useless then (and if it should be used, it must after the last item, not before the first one.

I've patched the itemize environment a little bit and added a new counter, that is reset and stepped each time \item is used.

Use \getrefnumber{...} from the refcount package to get an expandable number.

\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}


The following allows for an enumitem interaction with a countlist, and perhaps an easier interface: You specify a countlist environment with a mandatory argument that would reference the list \label. An optional argument is also allows, which is passed to an enumerate environment in typical enumitem fashion.

refcount provides the numeral display of a reference, although it's not really needed in this instance.

\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}


If you want an itemize-like feel for the list, one could use a default label={\textbullet} optional argument for enumerate.

Some creative use of enumitem:

\documentclass{article}
\usepackage{enumitem}

\newcounter{itemcount}
\newenvironment{countitemize}[1]
{\def\countitemizelabel{#1}%
\setcounter{itemcount}{0}%
\itemize[label=\stepcounter{itemcount}\textbullet]}
\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}


If you want to count every instance of \item, then you can do

\documentclass{article}
\usepackage{etoolbox}

\newcounter{itemcount}
\newenvironment{countitemize}[1]
{\def\countitemizelabel{#1}%
\setcounter{itemcount}{0}%
\preto\item{\stepcounter{itemcount}}
\itemize}
\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}


However, the counting would go on in nested environments. It would be possible to solve the issue, in case you need it.

I provide for nested countitemize up to level 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}}
\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}