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:


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



This report lists \theentrycount\ entries.


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.


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?
    – jub0bs
    Dec 31 '13 at 18:44
  • 1
    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.
    – egreg
    Dec 31 '13 at 19:10

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





This report lists \total{entrycount} entries.

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


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 difficult to achieve."

  • 1
    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

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.