3

I use \label to index the number of items in several environments. I wish to perform the sum of the quantities reported by those labels. Here is a working example:

\documentclass{article}
\usepackage{enumitem} % Control enumerate's layout

\newenvironment{myenumerate}{
   \begin{enumerate}[label={\footnotesize\emph{item~\arabic*}},ref=\arabic*] % this is how enumitem lets you set different label and ref, see http://tex.stackexchange.com/questions/286879/customize-label-and-ref
   \setlength{\itemsep}{.1\itemsep}
   \setlength{\parskip}{.1\parskip}
}{\end{enumerate}}

\begin{document}

First environmnent:
\begin{myenumerate}
\item A
\item B
\item C \label{nitems1st}
\end{myenumerate}

Second environmnent:
\begin{myenumerate}
\item D
\item E \label{nitems2nd}
\end{myenumerate}

There are \ref{nitems1st} items in the first and \ref{nitems2nd} in the second. How can I compute the sum?

\end{document}

Any help will be much appreciated.

4

You can added numerical references using a combination of LaTeX3 \calc and refcount:

enter image description here

\documentclass{article}
\usepackage{enumitem} % Control enumerate's layout
\usepackage{xparse,refcount}
\ExplSyntaxOn
  \cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

\newlist{myenumerate}{enumerate}{1}
\setlist[myenumerate]{label={\footnotesize\emph{item~\arabic*}},ref=\arabic*}

\begin{document}

First environmnent:
\begin{myenumerate}
  \item A
  \item B
  \item C \label{nitems1st}
\end{myenumerate}

Second environmnent:
\begin{myenumerate}
  \item D
  \item E \label{nitems2nd}
\end{myenumerate}

There are \ref{nitems1st} items in the first and \ref{nitems2nd} in the second.

There are a total of \calc{\getrefnumber{nitems1st}+\getrefnumber{nitems2nd}} items.

\end{document}

If your reference to the total is always after all enumerations, then you can use xassoccnt to count every step of the list environment items:

\documentclass{article}
\usepackage{enumitem} % Control enumerate's layout
\usepackage{xassoccnt}

\newlist{myenumerate}{enumerate}{1}
\setlist[myenumerate]{label={\footnotesize\emph{item~\arabic*}},ref=\arabic*}

\DeclareAssociatedCounters[autodefine=associated]{myenumeratei}{totalmyenum}

\begin{document}

First environmnent:
\begin{myenumerate}
  \item A
  \item B
  \item C \label{nitems1st}
\end{myenumerate}

Second environmnent:
\begin{myenumerate}
  \item D
  \item E \label{nitems2nd}
\end{myenumerate}

There are \ref{nitems1st} items in the first and \ref{nitems2nd} in the second.

There are a total of \thetotalmyenum{} items.

\end{document}
  • 1
    +0.5 for the general answer and +0.5 for using xassoccnt ;-) -- xassoccnt emulates some features totcount package, it's possible to know the total sum of enumerate items (per level only, of course) in advance after two compilations. – user31729 Jan 30 '16 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.