# [texhax] Incrementing a counter on a page boundary?

Adrian F. Clark alien at essex.ac.uk
Thu Aug 19 15:34:20 CEST 2004

Robin Fairbairns <Robin.Fairbairns at cl.cam.ac.uk> writes:

> or use a label, as someone else has suggested.  they'll multiply
> themselves, of course.

Well, I've been exploring the \label route as that seems most tractable.
I have something working in as much as I have the labels appearing
correctly, certainly an improvement; but in doing so, I've broken the
cross-referencing.  Maybe I could briefly pick your brain?  You'll
probably spot a solution in about 5 minutes.

The thing I'm trying to do is get a kind of literate programming tool
working.  My aim is for LaTeX to do the cross-referencing; I know it's
possible as noweb does it, though noweb.sty is full of arcania and
ludicrously long.

Here's my basic approach.  I've simplified it a little, though I don't
think that affects the logic.  The document contains sections of
(machine-generated) LaTeX that look like:

\begin{pulpcode}{Landing geometry}{pulpchunk:1}
\verb!float range = 4941.0, height = 274.0, speed = 79.74;!\\
\verb!float runway_width = 49.0;!\\
\pulpendcode
\pulpmacused{}{\pulpchunkref{pulpchunk:25}}
\end{pulpcode}

(The tool is called pulp'.)  The first argument to the environment is
the title of the chunk of code and the second is a unique label string,
to be used for cross-referencing.  The environment is currently defined as

\newenvironment{pulpcode}[2]{\begin{flushleft}\small
$\langle$\textbf{#1}$\rangle=$\nopagebreak
\begin{list}{}{\topsep 0pt\leftmargin 1.5em\itemsep=0pt}
\begin{minipage}{\PULPcodewidth}
\item}
{\end{minipage}\end{list}\end{flushleft}\ignorespaces}

(It can probably stand some improvements.)

As you can see, \begin{pulpcode} invokes \PULPlabel to generate the
label for this chunk of code, and that is typeset in the left-hand
margin.  The remainder of the code is concerned with typesetting the
title and body of the section of code and need not concern us further.

With regard to setting the label, I have the following code:

\RequirePackage{perpage}

\newcounter{pulpchunknumber}
\MakePerPage{pulpchunknumber}

\newcommand{\PULPlabel}[1]{\refstepcounter{pulpchunknumber}\label{#1}%
\pageref{#1}\alph{pulpchunknumber}}

This seems to do what I want, in that \PULPlabel generates "1a",
"1b"... "2a", "2b"...  and the \label command is squirrelling it away in
the .aux file.  However if I use \ref with the appropriate label, I
don't get "1b", only something like "2", which I suspect is the chunk
number within that page.  Looking in the .aux file, I find lines like:

\newlabel{pulpchunk:3}{{1b}{1}{The Landing Geometry\relax }{pulpchunknumber.2}{}}

which tends to confirm my suspicion.  However, I see that this line does
have the "1b" in it -- I just don't know how to extract it!

Any suggestions?  I'm completely flummoxed!  And feeling very stupid.

`