3

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).

Sverre
  • 20,729
  • 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

2 Answers2

5

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}

enter image description here

Sverre
  • 20,729
Gonzalo Medina
  • 505,128
3

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}}
%% \AtEndDocument{\addtocounter{mycounter}{-1}\myitem\label{finalmyitem}}

% again a variant:
\newcommand{\myitem}{\stepcounter{mycounter}}
\AtEndDocument{\addtocounter{mycounter}{-1}\refstepcounter{mycounter}\label{finalmyitem}}

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

6 items

  • I am assuming you want a 6 to be printed, not a 3, and the this is what the code proposed here does. –  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) –  Sep 10 '13 at 20:07
  • @jfbu That sounds better, but an \myitem in a delayed float caption, for example, might still cause problems. – Gonzalo Medina Sep 10 '13 at 20:08
  • @GonzaloMedina would my updated answer have this problem with a delayed float caption? –  Sep 10 '13 at 20:12
  • @jfbu I don't know for sure. I am not sure exactly at what point delayed floats are processed. Perhaps someone else can answer this (Werner, if I recall correctly, once needed something like that, so he or egreg, or David) – Gonzalo Medina Sep 10 '13 at 20:15
  • @GonzaloMedina: Look at the atveryend package for this. – Werner Sep 10 '13 at 20:22