I want to write the absolute pagenumber of a reference to a file. therefore i cant use \immediate\write. The problem is now i think that \write expands its parameters when a page is finished, but then i of course get the same counter number for all invocations on that page. There my question is: How do i pass the counter value from the time of the expansion of the command to \write?
\documentclass{article}
\usepackage{zref-savepos}
\usepackage{zref-abspage}
\usepackage{zref-user}
\newcounter{acnt}
\newwrite\tempfile
\immediate\openout\tempfile=tmp.txt
\makeatletter
\newcommand\test{
\stepcounter{acnt}
A \zsavepos{pos\arabic{acnt}}
\write\tempfile{Nr \arabic{acnt} is on page \zref@extract{pos\arabic{acnt}}{abspage}}
}
\makeatother
\begin{document}
\test
\test
\newpage
\test
\test
\test
\end{document}
And the output is:
Nr 2 is on page 0
Nr 2 is on page 0
Nr 5 is on page 0
Nr 5 is on page 0
Nr 5 is on page 0
But it should be:
Nr 1 is on page 1
Nr 2 is on page 1
Nr 3 is on page 2
Nr 4 is on page 2
Nr 5 is on page 2