Page LayoutProblem with counters referencing the page

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
bakalamaka
Posts: 3
Joined: Sun May 17, 2009 10:45 pm

Problem with counters referencing the page

Post by bakalamaka »

Dear all,

I have a problem with some custom-defined counters. The idea is to define the counter which will reset on each page increment and to produce labels like 1a, 3c where the number corresponds to the page number and the letter corresponds to the counter within that page. Now, a simple test like this works flawlessly:

Code: Select all

\documentclass{article}

\usepackage{hyperref}
\usepackage{fontspec}

\newcounter{chunk}[page]
\setcounter{chunk}{0}
\renewcommand\thechunk{$\arabic{page}\alph{chunk}$}

\begin{document}

Some text

{
\refstepcounter{chunk}\label{chunk: 1}

Chunk \thechunk :  data
}

{
\refstepcounter{chunk}\label{chunk: 2}

Chunk \thechunk :  data
}

\newpage

Some text

{
\refstepcounter{chunk}\label{chunk: 3}

Chunk \thechunk : data
}

{
\refstepcounter{chunk}\label{chunk: 4}

Chunk \thechunk :  data
}

\begin{enumerate}
	\item \ref{chunk: 1} at page \pageref{chunk: 1}
	\item \ref{chunk: 2} at page \pageref{chunk: 2}
	\item \ref{chunk: 3} at page \pageref{chunk: 3}
	\item \ref{chunk: 4} at page \pageref{chunk: 4}
\end{enumerate}

\end{document}
Unfortunately, when I use an essentially identical markup for the production document (which is auto-generated and thus not appropriate for inclusion) the page numbers and hyperlinks are all wrong. More specifically, the hyperlinks seem to recognize only the letter part, for example clicking at 5b or 7b will point me to 1b (the first "b" chunk in the document).

I found some info suggesting that using \thepage might be dangerous because the page number is not always known a particular point of time. Maybe this has something to do with my problem, I have no idea how to deal with it however. I also noticed that the aux entries for labels appear differently. The "correct" file (code above) has aux labels set in form:

Code: Select all

\newlabel{chunk: 1}{{$1a$}{1}{\relax }{chunk.1.1}{}}
...
\newlabel{chunk: 1}{{$1a$}{1}{\relax }{chunk.2.1}{}}
The "production" code has labels like

Code: Select all

\newlabel{chunk: 1}{{$1a$}{1}{\relax }{chunk.1}{}}
...
\newlabel{chunk: 1}{{$1a$}{1}{\relax }{chunk.1}{}}
The last declarations obviously miss the page component of the counter.

Thank you for any helpful suggestion...

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Post Reply