Store value of a macro when a page is finished

After my previous question was not answered, I have trimmed the MWE to what I think is the real problem. I have a macro \recent that stores information (which is needed for hyperref-links). This macro is regularly updated (i.e. overwritten) when a certain command (\newone) is issued.

I want to store the value of this macro (i.e. \recent) at the end of a page or the begining of a new page in another macro (\previous) for referencing purposes (I need it to put a hyperref-backlink on every page). I meddled with the usual suspects (everypage, evershi, atbegshi; bophook does not seem to like hyperref), but somehow the \previous-macro gets updated to soon or at the wrong time.

Please consider this MWE for an illustration of the problem. I switched to "newtoks" after "renewcommand{\previous}{\recent}" with normal macros did not succeed, but that has not helped matters.

The idea is to have the true "\previous" in the headline. However, as the MWE shows, this is not the case. Probably, there is an extremely simple solution to it, and it probably has to do with shipout-mechanisms, but I am just too blind (and unexperienced) to see it.

Every help is appreciated.

\documentclass[%
12pt,%
twoside=false,%
cleardoublepage=empty,%
open=right,%
parskip=half*,%
numbers=noenddot,%
chapterprefix=true,%
%draft %
]{scrbook}

\usepackage{everypage}
\usepackage{blindtext}

\newtoks{\recent}
\newtoks{\previous}
\global\recent={dummyrecent}
\global\previous={dummyprevious}

previous before EverypageHookoperation: \the\previous\\%
\global\previous={\the\recent}%
previous after EverypageHookoperation: \the\previous
}

\newcommand{\newone}[1]{%
my argument is: #1\\
previous is: \the\previous\\
recent was:  \the\recent\\
\global\recent={#1}
recent is: \the\recent
}

\begin{document}
Starting:\\
previous is: \the\previous\\
recent is:  \the\recent

\newone{number1}

\blindtext[5]

\newone{number2}

And now lots of text:\\

\blindtext[10]

\newone{number3}
\end{document}


Edit: Sorry for causing confusion. Hopefully, the following MWE shows better what I want. My concern are not page numbers, but link labels. I am trying to explain the concept in the "answer to question 1" in the MWE. Probably the whole approach is a mess; if someone has a better idea, please do not hesitate to comment.

\documentclass[
12pt,
twoside=false,
cleardoublepage=empty,
open=right,
parskip=half*,
numbers=noenddot,
chapterprefix=true,
%draft,
]{scrartcl}

\usepackage{everypage}
\usepackage{blindtext}
\usepackage{marginnote}
\usepackage{ifthen,ragged2e}
\usepackage{hyperref}

\ifthenelse{\boolean{JumpBackOn}}%
\hyperref[\previous]{\previous}}% Hyperlink only, if jumpback is on
{\pagemark}% %otherwise, i.e. in the questions section, just the
% pagemark
}

%% The jump commands to set the hyperlinks

\newboolean{JumpBackOn}  % Switch on for jumpback hyperlinks
\setboolean{JumpBackOn}{false} % but not in the first (questions) section

\newcommand{\jumpdown}[1]{\marginnote{\RaggedRight\tiny{\hyperref[#1]{Down
to
#1}}}} % Hyperlink to jump from the question in the questions
% section to the label at the start of the answer in
% the answer section; #1 is this label

\newcommand{\jumpback}[1]{% Jump from the answer to the label at the start of the corresponding question in the questions section; #1 is this label.
\gdef\recent{#1}% Remember this label, it is the label to the question that is currently being discussed.
\marginnote{\RaggedRight\tiny{\hyperref[#1]{Up to
#1}}} % place the hyperlink in the margin
}

%%%% When a page is finished and shipped out: remember the label in
%%%% \recent *at this time* by storing it in \previous

% globally make \previous the same as the current \recent
\global\let\previous=\recent
}

\begin{document}

\section{Questions for students (sometimes very
long)}\setboolean{JumpBackOn}{false}

\begin{enumerate}
\item[1.] Question 1 \label{q:1}\jumpdown{a:1}

\blindtext

\item[2.] Question 2 \label{q:2}\jumpdown{a:2}

\blindtext

\item[3.] Question 3 \label{q:3}\jumpdown{a:3}

\blindtext

\item[4.] Question 4 \label{q:4}\jumpdown{a:4}

\blindtext

\end{enumerate}

\clearpage

This is the answer text to question 1. In the margin, a label links
back to question 1; this link is correct. However, as this answer text
extends over a few pages, students should have a quick link in the
headline of each page to jump back to question 1 until answer 1 is
finished. The link should be at the same position in the top or maybe
bottom region of every page; it does not have to be the header, if
this causes trouble.

The link in the headline (or somewhere else) should point to the label
of the question that is (still) discussed \emph{at the beginning of
each page}. In our case, that is question 1 for page 3, 4 and 5.
However, on page 5 there is a link to question 2, not 1. Similarly, on
page 6 there is a link to question 3, not 2, although this question
starts at the top of the next page.

Again, at page 7, the link points to question 4, even though this
question starts at the top of the next page. The link on page 8 is
also wrong, although it appears right: If the previous-linklabel is
fixed at the end of page 7 and this label would then (correctly) point
to question 3, it should point to question 3 at the top of page 8 as
well, although this is not what I want: It is a design failure on my
part, but I have no better idea at the moment. If anyone knows how to
approach the whole thing in a better way, I would be grateful.

\blindtext[9]

\blindtext[7]

\blindtext[4]

Some space to fill the page:

\vfill

\blindtext

\end{document}

• \protected@write uses an interesting apporach. The get the right page number it includes \let\thepage\relax which apparently delays expanding \thepage until shipout. BTW, the usually empty argument can be used to \relax other macros. Apr 25 '20 at 16:54
• your example is very confusing, you imply you want this information in the page head (the one place where it is easy to access the page number) but your example code appears to be trying to access current and previous page number in the page body which is much harder requiring a two pass solution due to the asynchronous nature if tex's page breaking routines. But it isn't really clear what output you want, Apr 25 '20 at 20:00

\global\previous={\the\recent} will not deliver the current content of \recent. You should do

\global\previous=\expandafter{\the\recent}


But I think that using \global\let is better.

\documentclass[
12pt,
twoside=false,
cleardoublepage=empty,
open=right,
parskip=half*,
numbers=noenddot,
chapterprefix=true,
%draft,
]{scrbook}

\usepackage{everypage}
\usepackage{blindtext}

\def\recent{dummyrecent}
\def\previous{dummyprevious}

previous before EverypageHookoperation: \previous\\%
% globally make \previous the same as the current \recent
\global\let\previous=\recent
previous after EverypageHookoperation: \previous
}

\newcommand{\newone}[1]{%
my argument is: #1\\
previous is: \previous\\
recent was:  \recent\\
\gdef\recent{#1}%
recent is: \recent
}

\begin{document}
Starting:\\
previous is: \previous\\
recent is:  \recent

\newone{number1}

\blindtext[5]

\newone{number2}

And now lots of text:

\blindtext[10]

\newone{number3}
\end{document}

• Thanks egreg, this is really helpful. Still, the MEW does not want what I want it to do: The headline on Page 2 should read "number1" (last argument on page one); the value is correctly displayed as "previous" when \newone is called for the second time (number2). I suppose I have got the "AddEverypageHook" wrong, but I cannot figure out why. Apr 25 '20 at 16:38
• @Cyprinius You should be more specific on what you expect where. Apr 25 '20 at 16:43
• Ok, sorry, I'll try to give context. Eventually, \newone is a command that places a hyperlink; the labelname is its argument. \recent remembers the labelname of the last call of \newone. I want the headline of every page to show the value of \recent at the end of the previous page. Therefore, I want to copy this value to \previous when a page is shipped out (AddEveryPageHook}, so that the headline command can use \previous even when \recent changes on the new current page. Thus, the headline on page 2 shoud read "number 1", because this was the last value of "\recent" at the end of page 1. Apr 25 '20 at 17:25
• @Cyprinius I understand less, sorry. Comments are unsuitable for long explanations. Apr 25 '20 at 17:29
• egreg, thanks for your patience. I will try to post a new MWE that clarifies things. Sorry, I am new here. Apr 25 '20 at 18:31