40

At the beginning of my document, I have created a counter using \newcounter. Over the course of the document, this counter is increases using \stepcounter. How can I display the final value of the counter in a place prior to the the final occurrence of \stepcounter, such as at the beginning of the document?

  • 4
    You need to write the value into the .aux file, then you can read it back at the beginning. I'm sure someone will post some code for that soon. – Martin Scharrer Nov 14 '11 at 12:12
  • 1
    As it stands, the question doesn't even mention ConTeXt, and only shows LaTeX commands. I guess that having two answers with a total of 17 upvotes is receiving enough attention. – egreg May 10 '12 at 8:43
  • The question is many months old and already solved, but I found I have the same problem, but in ConTeXt, which does not have the totcount package. Would it have been better if I had started a new, identical question, but specific to ConTeXt, rather than add a bounty? – Village May 10 '12 at 8:46
  • @Village: You could have asked a new ConTeXt specific question. A bounty is also ok, but I wouldn't have chosen the "This question has not received enough attention" reason. – Martin Scharrer May 10 '12 at 14:56
42

The totcount package does just it:

\usepackage{totcount}
\newtotcounter{mycount}

With \newtotcounter{mycount} you define a new counter and also register it; if the counter is already defined (say chapter), then

\regtotcounter{chapter}

will simply register it to totcount system.

You can print the final value of the counter (as it was on the preceding LaTeX run) by \total{mycount}. There is also \totvalue{mycount} which is the analog of \value{mycount}. A couple of LaTeX runs are necessary to get synchronization.

  • 4
    Package totcount used \AtEndDocument while defining a new counter. Because of this, it may fail, e.g. try \AtEndDocument{\blindtext[10]My counter is \stepcounter{mycounter}\themycounter.} after \newtotcounter{mycounter}. It would be better, if the package would use something like \AfterClosingMainAux or \AfterLastShipout (package atveryend). Nevertheless your answer is the better one. – Schweinebacke Nov 14 '11 at 14:06
  • 1
    @Schweinebacke I agree, although this is a bit contrived example. Maybe you can ask the author about this. – egreg Nov 14 '11 at 14:14
17

Here is some code for Martin's comment:

\documentclass{article}
\usepackage{scrlfile}% KOMA-Script package with it's own documentation

\makeatletter
\newcommand*{\demomycounter}{% This is only for demonstration and not part of the suggestion
  \@whilenum \value{page}<2\do {%
    Usage of mycounter with value \stepcounter{mycounter}\themycounter.\par
  }%
}
\newcommand*\storecounteratend[1]{% command to tell LaTeX to save the last value for the next run
  \BeforeClosingMainAux{%
    \immediate\write\@auxout{%
      \string\restorecounteratbegin{#1}{\csname the#1\endcsname}%
    }%
  }%
}
\newcommand*{\restorecounteratbegin}[2]{% used at the aux file
  \expandafter\gdef\csname stored@#1\endcsname{#2}%
}
\newcommand*{\storedcounter}[1]{% user command to ask for the value
  \@ifundefined{stored@#1}{???}{\csname stored@#1\endcsname}%
}
\makeatother

\newcounter{mycounter}
\storecounteratend{mycounter}

\begin{document}
Last value of mycounter was \storedcounter{mycounter}.
\demomycounter
\end{document}

Instead of using \BeforeClosingMainAux from package scrlfile, you may use \AfterLastShipout with package atveryend.

Another solution would be to use \refstepcounter instead of \stepcounter. In this case, you simply have to write a label at the end of the document and may use \ref:

\documentclass{article}
\usepackage{scrlfile}

\newcounter{mycounter}
\BeforeClosingMainAux{\label{mycounter}}

\makeatletter
\newcommand*{\demomycounter}{% This is only for demonstration and not part of the suggestion
  \@whilenum \value{page}<2\do {%
    Usage of mycounter with value \refstepcounter{mycounter}\themycounter.\par
  }%
}

\begin{document}
Last value of mycounter was \ref{mycounter}.
\demomycounter
\end{document}

If you need not a reference to the counter but the value, you may combine the second suggestion with package refcount.

13
+50

In ConTeXt, you don't need any external package to display the final value of a counter: it is available by using the macro \lastcounter[...]. For example:

\definecounter[countername][numberconversion=Characters]

\starttext

There are \lastcounter[countername] counters.

\dorecurse{5}
  {\convertedcounter[countername] randomtext \incrementcounter[countername] \crlf }

\stoptext

To access the last value of the counter at the Lua end, use

structures.counters.last("countername", 1)
  • In Lua, I have tried many combinations, tex.count.lastcounter[countername], tex.lastcounter.countername, tex.count["lastcounter[countername]"], etc., but cannot find a syntax which is correct. – Village May 10 '12 at 23:54
  • tex.count table is only for counters defined using \newcount. I edited the answer to show how to access the counters defined in ConTeXt using Lua. – Aditya May 11 '12 at 0:21
1

The xassoccnt package defines similar strategies (although not yet in its full functionality like totcount package) with \NewTotalDocumentCounter for new counters or \RegisterTotalDocumentCounter for already existing counters.

The macro \TotalValue is expandable (see the example) and can be used to retrieve the value of a counter at any position within the document body after the 2nd compilation run.

\documentclass{article}

\usepackage[nonumberofruns]{xassoccnt}
\usepackage{blindtext}

\NewTotalDocumentCounter{foocntr}

\RegisterTotalDocumentCounter{section}

\begin{document}

The total value of section is \TotalValue{section}, whereas there are \TotalValue{foocntr} foos. 

The real name is \TotalCounterInternalNameExp{section}%


\ifnum\TotalValue{section} > 1\relax Hooray \else only one section found\fi
\blinddocument

\setcounter{foocntr}{100}

\blinddocument


\setcounter{foocntr}{8472}



\end{document}

enter image description here

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.