# Count items later in the document [duplicate]

The question is simple enough: How can I make the following output of my counter (\themycounter) say 6?

\documentclass{article}
\newcounter{mycounter}
\newcommand{\myitem}{\refstepcounter{mycounter}}
\begin{document}
I have some reason to tally up some items here\myitem \myitem \myitem.
But how many items are there in total in this document?
Are there {\themycounter} items?
Surely there are more since I am tallying up some more items now\myitem \myitem \myitem.
\end{document}


I'm quite confident that the answer doesn't need to be as complicated as the one given in Displaying a later value of a counter (which is why I'm taking the liberty to ask here).

## marked as duplicate by Werner, Sverre, Stefan Kottwitz♦Sep 10 '13 at 21:30

• Well, it's Gonzalo who's actually showing how to use the totcount package, so this thread is definitely to prefer over the original. – Sverre Sep 10 '13 at 20:00
• I reference two answerd, but can only pick one as a possible duplicate source. The latter one shows exactly what is needed. – Werner Sep 10 '13 at 20:02
• Oh, you're right. Well, both Gonzalo and jfbu provided excellent answers below (don't know how I can choose one over the other). Feel free to close as duplicate (but I hope the question will remain here, since Gonzalo's and jfbu's answers are easier for novice users like me to follow). – Sverre Sep 10 '13 at 20:06
• Since the question is active and has answers, it will remain if found by others so no worries. You can even accept an answer as usual (Gonzalo's is more general and doesn't require a \label, which has to be placed after the last \myitem for it to work). Even you could cast a vote-to-close if you feel that one of the mentioned posts are duplicates. – Werner Sep 10 '13 at 20:08
• Good. I see now that egreg's answer doesn't show how to step up the counter (granted, I know how to do that, but since Gonzalo's answer includes that too, it's better). I prefer MWE answers :) – Sverre Sep 10 '13 at 20:10

You can use the totcount package; you create and register the counter with \newtotcounter{<counter>} and then use \total{<counter>} (needs two or three runs):

\documentclass{article}
\usepackage{totcount}
\newtotcounter{mycounter}
\newcommand{\myitem}{\refstepcounter{mycounter}}

\begin{document}
I have some reason to tally up some items here\myitem \myitem \myitem.
But how many items are there in total in this document?
Are there {\themycounter} items? No, there are \total{mycounter}, since I am tallying up some more items now\myitem \myitem \myitem.
\end{document}


• I took the liberty to simplify your code with the single command \newtotcounter, which both creates and registers the counter. – Sverre Sep 11 '13 at 9:51
• @Sverre no problem. Edits to improve code are always welcome. Thanks. – Gonzalo Medina Sep 11 '13 at 13:06

Following egreg answer to the question you linked too: (see also better variant next)

\documentclass{article}
\newcounter{mycounter}
\newcommand{\myitem}{\refstepcounter{mycounter}}
\begin{document}
I have some reason to tally up some items here\myitem \myitem \myitem.
But how many items are there in total in this document?
Are there \ref{finalmyitem} items?
Surely there are more since I am tallying up some more items now\myitem \myitem \myitem\label{finalmyitem}.
\end{document}


This method (originating in the comments) avoids having to add manually a \label after the last \myitem:

\documentclass{article}
\newcounter{mycounter}

%%   \newcommand{\myitem}{\refstepcounter{mycounter}}

% again a variant:
\newcommand{\myitem}{\stepcounter{mycounter}}

\begin{document}
I have some reason to tally up some items here\myitem \myitem \myitem.
But how many items are there in total in this document?
Are there {\ref{finalmyitem}} items?
Surely there are more since I am tallying up some more items now\myitem \myitem \myitem.
\end{document}


• I am assuming you want a 6 to be printed, not a 3, and the this is what the code proposed here does. – user4686 Sep 10 '13 at 20:00
• I didn't understand egreg's original answer. Thanks for providing a much simplified version! :) – Sverre Sep 10 '13 at 20:03
• Perhaps one can even do \AtEndDocument{\label{finalmyitem}} so there's no need to manually place the label. – Gonzalo Medina Sep 10 '13 at 20:04
• @jfbu Ah, yes, you're right. Disregard my comments; in fact, I'll delete them shortly. – Gonzalo Medina Sep 10 '13 at 20:06
• @GonzaloMedina however one could do \AtEndDocument{\myitem\label{finalmyitem}} and arrange to manually decrease by one when referencing. (untested, to be implmented) – user4686 Sep 10 '13 at 20:07