Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
3  
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
add comment

3 Answers

up vote 25 down vote accepted

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.

share|improve this answer
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
add comment

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.

share|improve this answer
add comment

You don't need any external package in counter. \lastcounter[...] gives the final value of the counter

\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)
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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