Use counter before its value is set

I'd like to use a counter in the text which however is only set at a later point. Like so:

\newcounter{howmany}
There are \thehowmany items in this list:
\begin{enumerate}
\item one
\item two
\item three
\setcounter{howmany}{\theenumi}
\end{enumerate}


And it should say "There are 3 items in this list". But that doesn't work. Well, sure. I need to use \refstepcounter to get that save-functionality. But \refstepcounter only increments it by one, and there doesn't seem to be a \refsetcounter ;) Any suggestions? A loop after the enumerate over \refstepcounters? Redefining \item to include \refstepcounter? There gotta be a simpler way, no?

Thanks!

• Can't you just use \label{foo} and \ref{foo}? May 9 '20 at 15:22
• @cgnieder If the list counter uses \alph reference style for instance, the \ref command would not show the number of items. May 9 '20 at 15:33
• @frougon true but with enumitem one could set ref=\arabic* and it would work again. Only if you had a differently enumerated list and need them referenced that way, too, this becomes a problem May 9 '20 at 15:44
• @cgnieder If the items are labeled a, b, c, d or i, ii, iii, iv, etc., it doesn't make sense to refer to them as 1, 2, 3, 4, etc. in the text. Thus, in such cases, your trick can't be used. It only works in the particular case where labels start at 1 and use decimal notation (or you never use \ref for them except for counting them). This is good to think about, but unnecessarily restrictive IMHO. May 9 '20 at 15:48
• @frougon of course. I never said it would work in any and each case. But it sure works for the MWE of the OP, doesn't it? Any other more complicated case one can still use totcount, or define a command which writes the informantion to the aux file, or … May 9 '20 at 15:57

Welcome to TeX.SE. This is a job for the totcount package. You need to compile twice, since totcount uses the .aux file to store the last value assigned to the counter in the document.

Note that I use \value{enumi} instead of \thenumi in the \setcounter call in case \thenumi doesn't expand to an integer denotation (\thenumi might use \roman or \alph formatting, etc.).

\documentclass{article}
\usepackage{totcount}

\begin{document}

\newtotcounter{howmany}
There are \total{howmany}~items in this list:
\begin{enumerate}
\item one
\item two
\item three
\setcounter{howmany}{\value{enumi}}
\end{enumerate}

\end{document}


If there is already a \newcounter command for the counter in question and you don't want to, or cannot change this command into a \newtotcounter call, you can register the counter with the totcount package using \regtotcounter{countername} (this can be useful if the counter is defined by a package).

This also works with non-decimal labels

This technique works as well if you use non-decimal label formatting:

\documentclass{article}
\usepackage{totcount}

\renewcommand{\theenumi}{\alph{enumi}}

\begin{document}

\newtotcounter{howmany}
There are \total{howmany}~items in this list; their labels are
\ref{first-item}, \ref{second-item}, and \ref{third-item}.
\begin{enumerate}
\item \label{first-item}one
\item \label{second-item}two
\item \label{third-item}three
\setcounter{howmany}{\value{enumi}}
\end{enumerate}

\end{document}


• Quick, thorough, useful: thank you very much :)
– rfl
May 9 '20 at 15:36
• You're welcome, glad to help. :-) May 9 '20 at 15:38

You can wrap your list to be counted in an environment:

\documentclass{article}

\makeatletter
\newenvironment{countitems}[1]
{% #1 is the label for referring to the count
\def\countitems@label{#1}%
}
{%
\edef\@currentlabel{\arabic{enum\romannumeral\numexpr\@enumdepth+1}}%
\label{\countitems@label}%
\ignorespacesafterend
}
\makeatother

\begin{document}

The following list has \ref{firstlist} items
\begin{countitems}{firstlist}
\begin{enumerate}
\item one
\item two
\item three
\begin{countitems}{innerlist}
\begin{enumerate}
\item A
\item B
\end{enumerate}
\end{countitems}
\end{enumerate}
\end{countitems}
The inner list has \ref{innerlist} items.

\end{document}


Alternatively, you can use a command after the relevant \end{enumerate}

\documentclass{article}

\makeatletter
\newcommand{\countitems}[1]{%
\edef\@currentlabel{\arabic{enum\romannumeral\numexpr\@enumdepth+1}}%
\label{#1}%
\ignorespaces
}
\makeatother

\begin{document}

The following list has \ref{firstlist} items
\begin{enumerate}
\item one
\item two
\item three
\begin{enumerate}
\item A
\item B
\end{enumerate}\countitems{innerlist}
\end{enumerate}\countitems{firstlist}
The inner list has \ref{innerlist} items.

\end{document}

• Interesting solution :)
– rfl
May 10 '20 at 11:47

Just copying @cgnieder's answer, given in the comments above, for reference. See the discussion in the comments above for pros and cons, but it sure is a simple possibility:

There are \ref{countforme} items in this list:
\begin{enumerate}
\item one
\item two
\item \label{countforme} three
\end{enumerate}