Skip to content

A running head shows the document's last mark on every page: marks do not travel with page breaking #727

Closed

Description

@tannevaled

A running head shows the document's last mark on every page, not the mark in force on that page. It became visible with #726, which draws the head at all; before that the band was reserved and left empty, so the content was never seen to be wrong.

Witness

fontcmp -pagefill's headings document has 26 sections. Page 1's head reads 26 SECTION NUMERO 26, against the oracle's 1 SECTION NUMERO 1 1.

Not off by one — the last section in the document, on the first page.

Why

There is one live pair and it is global (classkernel.go:633-637):

\gdef\gotex@leftmark{}
\gdef\gotex@rightmark{}
\def\markboth#1#2{\protected@xdef\gotex@leftmark{#1}\protected@xdef\gotex@rightmark{#2}}

and this engine assembles pages after the whole document has run, so the live pair at assembly time is the final mark the document set. classkernel.go:626-632 already says so, and says what is missing: "real LaTeX carries the pair through TeX's \mark / \firstmark / \botmark … The difference needs marks to travel with page breaking, which this page builder does not do yet."

What TeX does, which is the shape of a fix

tex.web:7864-7866 defines the three:

top_mark_code=0    {the mark in effect at the previous page break}
first_mark_code=1  {the first mark between top_mark and bot_mark}
bot_mark_code=2    {the mark in effect at the current page break}

and tex.web:19718-19729 maintains them at every break:

if bot_mark<>null then
  begin … top_mark:=bot_mark; … first_mark:=null; end;
<Put the optimal current page into box 255, update first_mark and bot_mark, …>
if (top_mark<>null)and(first_mark=null) then
  begin first_mark:=top_mark; … end;

So: the previous page's end becomes top_mark; first_mark is cleared and then filled by scanning this page's mark nodes; and a page carrying no mark inherits top_mark — which is what keeps a head correct across a page with no new section.

LaTeX reads them through those names (latex.ltx:13360-13361):

\def\leftmark{\expandafter\@leftmark\botmark\@empty\@empty}
\def\rightmark{\expandafter\@rightmark\firstmark\@empty\@empty}

\ps@headings puts \rightmark in \@oddhead (latex.go:898), so a running head asks for \firstmark — the first mark on the page being shipped. We answer with the document's last.

It needs a mark node riding the vertical list, the way pageStyleNode already rides it for \thispagestyle (pagenum.go:28-35), and first/bot computed per page where the break is chosen.

How much it matters

population over .tex,.cls,.sty for pagestyle\{(headings|myheadings)\}: 27 of the 154 measured papers. Those are the ones whose head tracks sections; a head set once — a title, a preprint banner — is unaffected, since the single live pair is then the right one.

⚠ Worth being honest about the direction: before #726 those 27 drew no head, so the words were absent; now they are present and, for the section-tracking ones, wrong. That is not obviously worse for a word-recall metric, and it is clearly better geometrically (#726 recovers 31.7pt of text per page). But it is a visible defect where there was a visible absence, and that is the argument for doing this one next.

Activity

  1. tannevaled commented on Nov 7, 2026

    @tannevaled
    ContributorAuthor

    This is not a refinement of the head — for amsart it is the ONLY route to one

    I filed this as "the head draws, with the wrong content: the document's last mark on every page". That is true for our own \ps@headings. For the class that 27 of the 128 papers in #426 use, it understates the issue: there is no head at all without marks, because amsart reads its heads straight out of TeX's marks (texmf/amsart.cls:325-326):

    \def\leftmark{\expandafter\@firstoftwo\topmark{}{}}
    \def\rightmark{\expandafter\@secondoftwo\botmark{}{}}

    It also brings its own \markboth, which emits \mark{{#1}{#2}} (:335-340). So amsart never reads \@themark back — it only writes it — and #728 (\@themark under the kernel's name) does nothing for it. Measured, not inferred: 2111.13456 rendered with #726 and #728 still shows only the folio in the band, y=98.99 size=7 "2".

    And the engine has no mark primitives whatsoever:

    $ git grep -n 'topmark\|botmark\|firstmark' -- '*.go'
    

    returns five hits, all inside comments. \mark is undefined, which is why amsart's \markboth contributes nothing and why those papers need -lenient.

    What it takes, from the reference

    • \mark{…} as a node on the vertical list, its token list expanded at mark time, travelling with page breaking. pageStyleNode (pagenum.go:28-35) is the precedent: \thispagestyle solves the same "which page does this land on" problem by contributing a node and letting the builder find it, "which is how TeX gets the same answer".

    • \topmark, \firstmark, \botmark maintained per page — tex.web:7864-7866, with the per-break arithmetic at :19718-19729:

      value
      \topmark the previous page's \botmark
      \firstmark this page's first mark, or \topmark if it has none
      \botmark this page's last mark, or \topmark if it has none
    • They must hold the page's values before the head is drawn, since the head expands them during assembly — which is where our single live pair gets away with being wrong today.

    \splitfirstmark / \splitbotmark belong to \vsplit and are out of scope here.

    Closing #426 means doing this, so the two are one piece of work. I am keeping them as separate issues because the populations differ — #426 counts the papers losing a head, this one names the mechanism — but neither closes without it.

  2. tannevaled commented on Nov 7, 2026

    @tannevaled
    ContributorAuthor

    Measured: each page now carries its own section. Branch kernel-marks, HELD

    With #729's mark primitives under it, the kernel's own accessors can read the page's marks instead of the single live pair. Three sections forced onto three pages, \pagestyle{headings}:

    page 2 head   "2 DEUXIEME SECTION"
    page 3 head   "3 TROISIEME SECTION"
    

    That is this issue. With one live pair every page showed the document's last section, because our \ps@headings redefines \sectionmark as a \markright and the final one wins everywhere.

    What the branch does, straight from the kernel:

    \def\markboth#1#2{\protected@xdef\@themark{{#1}{#2}}\mark{\@themark}}
    \def\markright#1{\expandafter\@markright\@themark{#1}\mark{\@themark}}
    \def\leftmark{\expandafter\@leftmark\botmark\@empty\@empty}
    \def\rightmark{\expandafter\@rightmark\firstmark\@empty\@empty}

    ltpage.dtx:239-244 and :265-267 for emitting the mark beside writing the pair — the pair is for a class to read back, the mark is for the page builder — and :341-342 for the accessors.

    ⛔ Why it is held rather than proposed

    Six tests fail, all of them probing \leftmark/\rightmark at document time:

    TestClassKernelDeclarations/marks        TestOnlyNumberedSectionsMark
    TestPagestyleHeadingsDeclaresAHead       TestRunningMarks
    TestAClassThatBringsItsOwnMarkbothReachesTheHead
    TestMarkrightKeepsTheLeftHalf
    

    They are not wrong about the engine. They are wrong about TeX: those values belong to a page, so outside a page there are none, and a real LaTeX answers empty there too. latex.ltx has carried a note about this awkwardness since 1993 — "Stopgap solution to make \leftmark and \rightmark work without initializing mark until the problem is solved" (ltpage.dtx:318-323) — with an upstream \task still open on it. In this engine the page builder runs after the whole document is read, so document time can never see a page's marks at all.

    So the six have to be rewritten against the drawn head rather than against an expansion. That is the same move #728's test made — it stopped probing \gotex@rightmark, the storage, and started probing \rightmark, what a page style reads — and doing it properly needs a helper for reading a rendered page's text plus six careful conversions. Rushing that is how a suite ends up asserting the implementation instead of the behaviour, which is the defect this whole chain has been about.

    Nothing is measured on the corpus either: the machine's 1-minute load average was 20.05 against a ceiling of ~8.

    The branch is pushed so the work is not lost, and reported here rather than left as a branch with no pull request — which is precisely the invisible work branchscan exists to find. It opens as a pull request after #729 merges and the six tests are converted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions