Thanks Wolfgang. I am experimenting with your code and there has arisen a
problem. The book I am typesetting has some pages before the text on
page-1 starts (cover and so on). Therefore the code
\def\mypageref[#1]{\setpagestate[hvdm][#1]\color[red]{page=\pagenumber\
and pageref=\pagestaterealpage{hvdm}{#1}}}
produces "page=29 and pageref=31". Not as intended. Do I
change the macro \pagestaterealpage into what macro? I tried
\pagestatepage, but that wasn't the right idea.
Now what further to change?