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?
.texdocument and using, for example, a\label-\refsystem (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:52totcountpackage should be what you're looking for. – egreg Dec 31 '13 at 19:10