1

I have a long list in an enumerate environment. I would like to put as header to the page (or in any case to save in a variable to use elsewhere) the number of the last item appearing on it.

A minimal example of what I would like to do is in the following code in luatex (where the only use I make of lua is to have 120 consecutive items to get what I want).

\documentclass{article}
\usepackage[absolute]{textpos}
\let\mycounter=0

\AddToHook{shipout/before}{ \begin{textblock}{40}(0,0) Last entry: \mycounter \end{textblock}}

\AddToHook{cmd/item/before}{\let\beginitmpage=\thepage} \AddToHook{cmd/item/after}{\let\enditmpage=\thepage \ifnum \thepage=\beginitmpage \let\mycounter=\theenumi\fi} \begin{document} \begin{enumerate} \directlua{ for i=1,120,1 do tex.print("\item Dummy text from page \beginitmpage\ to page \enditmpage") end} \end{enumerate} \end{document}

As it can be seen, at the end of the first page the counter is already at 32 (but the last line printed is 28). Entries 29-32 "believe" to be on page 1 even if they are formatted on page 2.

I know that tex has already processed part of the text which is to appear on the second page and that this is normal, but this is exactly what I would like to avoid. Similar problems occur with the remaining pages.

Is there any simple way to implement what I would like to get in a "simple" way? (i.e. not to get the value of the counter at shipout, but the value of the counter used to construct the page).

lucag73
  • 83
  • 1
    Easiest is to check if the page number changes between \items. The question is then whether the current \item split over two pages or started fresh on the new page. – John Kormylo Jul 02 '23 at 00:30
  • @JohnKormylo It is a good idea, but unfortunately It does not work. The entries on the second page report to be still on page 1. I update the tex code in the question with an example which shows this. – lucag73 Jul 02 '23 at 09:06
  • Thanks. I misunderstood their role. Actually I believe there might be some cases in which the values of the page in the before and after hooks are different (I think had some of them where the algorithm moved the whole paragraph to a new page to avoid orphans), but, in any case, what I was doing was wrong. – lucag73 Jul 02 '23 at 13:05
  • Do you want each item to be able to say "I am on page XXX", or do you want to say elsewhere "Page 1 has items 1--XXX" or both? Or is this only happening in the header to the page, where you want to say "This page has items 1--XXX"? – Teepeemm Jul 02 '23 at 19:32
  • My goal is just to have an header like "Page 1 has items 1--XXX" for each page. Having also the pagenumber within the text of the item would be nice, but it is not needed. – lucag73 Jul 02 '23 at 20:41

1 Answers1

0

Following the suggestions of @John and after a bit of debugging I have finally got a satisfactory result. Here is the code (printing both the number of both the first and the last entry of the list on each page).

\documentclass{article}
\usepackage{luacode}
\usepackage[absolute]{textpos}
\usepackage{lipsum}

\def\lastcounter{\relax} \def\firstcounter{\relax} % ref_page from https://tex.stackexchange.com/questions/659916/get-page-number-of-label-in-lualatex

\begin{luacode} function ref_page(l) local r = token.get_macro('r@'..l) if r == nil then return 0 end local sec, page = r:match("{([^}])}{([^}]*)}") return page end

toSwap=0 function process() currPage=tonumber(tex.count['c@page']) if toSwap==1 and Bpage==currPage then tex.sprint('\global\def\firstcounter{'..nextval..'}') toSwap=0 end enumi=tonumber(tex.count['c@enumi']) Apage=tonumber(ref_page('Q'..tostring(enumi-1))) Bpage=tonumber(ref_page('Q'..tostring(enumi))) if (Apage<Bpage and toSwap==0) then nextval=enumi toSwap=1 print("S") end if (Apage<=Bpage and Bpage==currPage) then tex.sprint('\global\def\lastcounter{'..(enumi)..'}') end print("ref #="..enumi.." on page "..Apage.."/"..Bpage.." cpage="..currPage) end \end{luacode*} \AddToHook{shipout/before}{ \begin{textblock}{40}(0,0) {\rm First entry: \firstcounter} \ {\rm Last entry: \lastcounter} \end{textblock} }

\AddToHook{cmd/item/after}{\expandafter\label{Q\theenumi}\directlua{process()}}

%%% SAMPLE TEXT \begin{document} \begin{enumerate} \directlua{ for i=1,120,1 do tex.print("\item {\bf Dummy text on page \pageref{Q"..(i).."} } \lipsum[2]" ) end} \end{enumerate} \end{document}

lucag73
  • 83