# How to refer to a (La)TeX counter early in the document?

I am programmatically typesetting a long document from database contents. For reasons of efficiency, I would like to output all generated LaTeX code as early as possible. I would also like to provide a title page for the report which quotes the number of entries reported and have the title page appear as the first page of the generated pdf. In principle, what I'm doing right now looks roughly like this:

\documentclass[a4paper]{article}
\newcounter{entrycount}
\begin{document}

\stepcounter{entrycount} helo world\par
\stepcounter{entrycount} here are\par
\stepcounter{entrycount} some entries\par
\stepcounter{entrycount} from the database\par

\clearpage\centering

Title

This report lists \theentrycount\ entries.

\end{document}


here I refer to the counter after the rest of the document has been printed out; obviously, in order to make it so that the title page can come first I'd have to (1) use a command that inserts the title page into the front of the other pages (not sure whether that is possible) or (2) somehow refer to the counter's final value before it is available (certainly possible, at least when running LaTeX several times?).

I'm aware there are other solutions, including moving the title in the printed stack manually to the top and counting DB entries before outputting any LaTeX code; however, such a simple thing must be possible to do within LaTeX, no? I tried references but didn't hit upon a workable solution so far.

Update

Having looked up the link provided by @Werner, I tried

This report lists \ref{ec} entries.

\section{helo world         }\label{ec} \par
\section{here are           }\label{ec} \par
\section{some entries       }\label{ec} \par
\section{from the database  }\label{ec} \par


which sort of works (never mind the fact the output isn't right—the principle apparently is). But when I do

\stepcounter{entrycount}\label{ec} helo world\par
\stepcounter{entrycount}\label{ec} here are\par
\stepcounter{entrycount}\label{ec} some entries\par
\stepcounter{entrycount}\label{ec} from the database\par


I get nothing but a blank in the output. My guess is I'd have to promote my counter to act specially so it'll get captured by the labels. But how?

• Why no capital letters? Dec 31 '13 at 18:44
• The only way you can achieve this is via multiple compilations of your .tex document and using, for example, a \label-\ref system (as described in Understanding how references and labels work). Are you able to compile at least twice in your environment?
– Werner
Dec 31 '13 at 18:52
• @Jubobs: because Grimm 1854: „den gleichverwerflichen misbrauch groszer buchstaben für das substantivum, der unserer pedantischen unart gipfel heiszen kann, habe ich abgeschüttelt.” (de.wikipedia.org/wiki/Kleinschreibung) it's a stylistic choice.
– flow
Dec 31 '13 at 19:03
• The totcount package should be what you're looking for. Dec 31 '13 at 19:10

@egreg put me on the right track. a fully working minimal example:

\documentclass[a4paper]{article}
\usepackage{totcount}
\newcounter{entrycount}\regtotcounter{entrycount}

\begin{document}

{\centering

Title

This report lists \total{entrycount} entries.
}

\clearpage
\stepcounter{entrycount}helo world\par
\stepcounter{entrycount}here are\par
\stepcounter{entrycount}some entries\par
\stepcounter{entrycount}from the database\par

\end{document}


be it said that even simple things are often hard in TeX. as the totcount manual says: "Referring to the total number of sections, pages, citations, list items, or anything else in a document can be difficult to achieve."

• The reason for it being "difficult" is because many of these things are numbered within other units (sections within chapters, pages within front/main/back matter, citations within sections, list items within lists...
– Werner
Dec 31 '13 at 19:44
• let me put it this way: the solution is "easy" (in the sense of infoq.com/presentations/Simple-Made-Easy) once you know about it. it is not "simple" when you consider that totcount.tex consists of ≈100 LOC, rife with @ specials; apparently, you cannot use your own counter with a simple \ref—IOW, (La)TeX's \label/\ref mechanism doesn't scale too well in terms of reappropriation. for these reasons i'm currently dabbling with PerlTeX. i hope to write a piece soon where i show how to script TeX with JavaScript / CoffeeScript with relative ease ("simple" the solution is not).
– flow
Jan 1 '14 at 14:10