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?
- 13,603
- 23
- 116
- 219
4 Answers
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.
- 402
- 2
- 14
- 1,121,712
-
6Package
totcountused\AtEndDocumentwhile 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\AfterClosingMainAuxor\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
-
@Schweinebacke I ran into exactly the problem when I tried to define this in a preamble, https://tex.stackexchange.com/questions/538566/how-to-set-a-counter-like-last-page-so-i-can-call-its-last-value . It's showing an error as ! Missing number, treated as zero.
\c@mycount@totc – CasperYC Apr 14 '20 at 15:00
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.
- 26,336
-
-
@CasperYC Why do you think so?
scrlfileis still part of KOMA-Script (see the link in my answer). – Schweinebacke Aug 23 '23 at 09:24 -
Not sure why I posted that...maybe at that time 3 years ago, MikTeX just wouldn't find it... – CasperYC Aug 25 '23 at 05:35
-
@CasperYC So maybe you should delete this comment, because the reply is surely: It still exists. – Schweinebacke Sep 01 '23 at 09:37
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)
- 62,301
-
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.counttable 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 00:21
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}

.auxfile, 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:12totcountpackage. 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 08:46