4

How could I in the following document replace the X by the correct per-page-total? (29, 31, 20 in my compilation).

\documentclass{article}

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{on this page are X objects}

\usepackage{zref-perpage,expl3}
\newcounter{mycounter}
\zmakeperpage{mycounter}

\begin{document}\pdfsetrandomseed 4
\ExplSyntaxOn
\int_step_inline:nnnn {1 }{1}{80}
{\refstepcounter{mycounter}\themycounter.~an~object \\
 \int_if_odd:nT {\fp_eval:n { randint( 9 ) }}{some~randomness\\}
}
\ExplSyntaxOff
\end{document}
3

I suggest to store the expanded number of objects per page in a \prop list and extract the value of that list in the header, where the property name is just the page number (hopefully not reset in between); even if the number of objects has to be evaluated more than once (in the loop), this is will update the key - value only (since property - lists have a unique key - value relationship, the potential older value is overwritten)

I tested this with a loop of 5000 iterations (resulting in 169 pages) and the results are correct. (Takes a few seconds, however)

enter image description here

\documentclass{article}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{zref-perpage,expl3}
\newcounter{mycounter}
\zmakeperpage{mycounter}


\lhead{on this page are \propextract\ objects}



\begin{document}\pdfsetrandomseed 4

\ExplSyntaxOn
\cs_generate_variant:Nn \prop_item:Nn {No,Nx}
\cs_generate_variant:Nn \prop_gput:Nnn {Nxx}
\cs_new:Npn \propextract{%
  \prop_item:Nx \g_ufischer_object_counter {\thepage}
}
\prop_new:N \g_ufischer_object_counter 
\int_step_inline:nnnn {1 }{1}{80}
{\refstepcounter{mycounter}\themycounter.~an~object \\
 \int_if_odd:nT {\fp_eval:n { randint( 9 ) }}{some~randomness\\}
 \prop_gput:Nxx \g_ufischer_object_counter {\thezpage} {\themycounter}

}
\ExplSyntaxOff
\end{document}
| improve this answer | |
  • It fails when different page numberings systems are used, you need to use \thepage instead of \number\value{page}. – Ulrike Fischer Mar 30 '18 at 12:53
  • @UlrikeFischer: That's a design decision then -- your original setup did not have other page numberings – user31729 Mar 30 '18 at 13:45
  • 1
    But your code is not consistent. You are using \thezpage which is a formatted counter to store the values but the unformatted \number\value{page} to retrieve them. But beside this your answer is fine and helped me to solve my actual problem! – Ulrike Fischer Mar 30 '18 at 13:54
  • @UlrikeFischer: I was in a hurry. I replaced \number\value{page} with \thepage – user31729 Mar 30 '18 at 18:26

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.