Background: I am editing the transcription of a historical manuscript using the memoir class. The transcription includes the folio numbers of the original manuscript, which are displayed in bold within square brackets at the points where each folio starts. These starting points can be anywhere on the transcribed page. There can be zero, one, or two new folio numbers on a transcribed page. The folio "numbers" are not purely numeric, so cannot be stored in a counter. The folio number is inserted with a custom command in the text, \folio.
Desired outcome: For each page, I want to have the running head display the range of numbers for the folios whose text appears on that page. So if the first text on a page comes from folio 5a, and then a few lines down folio 6 starts and takes up the rest of the page, I want the running head to display "folios 5a-6."
The MWE below comes close to working, but not quite. It runs into two problems.
First -- I used AtBegShi to place the last folio number on each page into a variable. But page shipout runs only after the last line on a page has been fully processed. If that line continues onto the following page, and contains a \folio command that displays on that following page, the folio range of the former page will be printed incorrectly, as on pages (1) and (8) of the output.
Second -- look at page (10), which starts with folio number [10], but shows a folio range starting with 9 in the header because the page technically starts before the folio number [10] is processed.
How can I solve these two problems?
Essentially the same questions have been asked and answered before (links below), but the answers are ten years old now and use different packages and classes, some obsolete. In any case I could not figure out how to apply any of the previous solutions to my problem.
Saving the value of a text macro at the start of the page.
Displaying the range of sections on the page in the header
section numbers (\thesection) and names have next page's values
How to define macros that are executed at begin and end of the page body or text column(s)?
\documentclass[a4,12pt]{memoir}
\usepackage{lipsum}
\usepackage{extramarks}
\usepackage{atbegshi}
% define page style with folio range in header and page number in footer
\makepagestyle{mystyle}
\makeevenhead{mystyle}{}{\lastleftmark}{}
\makeoddhead{mystyle}{}{\lastrightmark}{}
\makeevenfoot{mystyle}{(\thepage)}{}{}
\makeoddfoot{mystyle}{}{}{(\thepage)}
% command to write folio range in header
\NewDocumentCommand{\foliomarks}{}%
{%
\markboth{\hfill Folios \protect\PrevFolio--\protect\CurrFolio \hfill}{\hfill Folios \protect\PrevFolio--\protect\CurrFolio \hfill}%
}
% command to print folio number in text and save value for header
\NewDocumentCommand{\folio}{m}%
{%
{\textsf{\large{[#1.]}}}%
\xdef\CurrFolio{#1}%
\foliomarks{}%
}
\begin{document}
% initialize folio variables
\xdef\PrevFolio{1}
\xdef\CurrFolio{0}
% copy current folio number to previous folio number at top of each page
\AtBeginShipout{%
\xdef\PrevFolio{\CurrFolio}
}
\pagestyle{mystyle}
\folio{1}
\lipsum[1]\folio{2}
\lipsum[1-3]\folio{3}
\lipsum[1-2]\folio{4}
\lipsum[1]\folio{5}
\lipsum[1-2]\folio{5a}
\lipsum[1]\folio{6}
\lipsum[1-2]\folio{7}
\lipsum[1]\folio{8}
\lipsum[1-20]\folio{9}
\lipsum[1]\clearpage\folio{10}
\lipsum[1]
\end{document}
\leftmarkand\rightmarkor the underlying\topmark,\firstmarkand\botmarkare designed for exactly this – David Carlisle Mar 13 '22 at 16:20\topmarkandbotmarkbefore! Using them solves the first problem I mentioned and you are so right that I didn't need to maintain the previous folio number "by hand." My second problem remains -- cases where the first text on a page is destined for a mark, but\topmarkholds the previous mark. I'm looking at previous discussions of this issue ... – 35royan2 Mar 13 '22 at 17:16\topmarkand\botmarkbut it doesn't solve the first problem, as \topmark does not update properly when\mark{}is issued from within environments. It works in the MWE I gave, but not in more complex documents. Indeed I see many references to\topmarknot working correctly in LaTeX. Then there is still the second problem (getting top marks to reflect sections that start at the beginning of a page). There's a brief discussion of this issue in Javier Bezos's docs for thetitlepspackage, section 5 (Marks). I haven't figured out whether using titleps would help. – 35royan2 Mar 14 '22 at 12:07\markbothusage as shown in the question. You just need to ensure that your macros issue the marks at a point that they can work. – David Carlisle Mar 14 '22 at 12:35