Skip to content

Keep each margin note on the page of its callout#25

Draft
dbosk wants to merge 1 commit into
shicks:masterfrom
dbosk:keep-notes-on-anchor-page
Draft

Keep each margin note on the page of its callout#25
dbosk wants to merge 1 commit into
shicks:masterfrom
dbosk:keep-notes-on-anchor-page

Conversation

@dbosk

@dbosk dbosk commented Jun 23, 2026

Copy link
Copy Markdown

Problem

A \marginpar is a float, so LaTeX can attach it to the page it is currently
assembling even when the page then breaks before the line that called it. The
visible result is a margin note whose mark sits at the top of a page but
whose text is set in the margin of the previous page.

marginfix arranges notes within whatever page LaTeX hands them, and pushes
overflow forwards, but it had no notion of which page a note belongs to, so
it could not repair this backwards drift. marginnote-style in-place placement
avoids it but loses marginfix's stacking (adjacent notes overprint), so it isn't
an option for documents with clustered margin citations.

Fix

Keep each note on the page of its callout, across two passes, reusing the
kernel's own cross-reference machinery:

  • Each real marginpar is numbered in document order — at callout time
    (\mfx@callid) and again in the output routine (\mfx@placeid); the two
    counters stay in lock-step because every real marginpar passes through each
    exactly once. Injected control marginpars are skipped.
  • At callout we record the callout's page in the .aux with a deferred
    \write, so the page is captured at shipout (the same trick \label
    uses).
  • On the next run, that recorded anchor page lets \@addmarginpar hold a
    note back in \mfx@deferred until its anchor page is shipped, instead of
    setting it too early. \MFX@combinefloats@before releases waiting notes at
    the top of their page, ahead of that page's own notes, preserving document
    order for clusters.
  • Because margin material is attached only after the body column is built,
    margin notes never move the body's page breaks → anchor pages are stable →
    the process converges, usually in a single extra pass.
  • A rerun is requested through the kernel's \@testdef check (so the standard
    "Label(s) may have changed. Rerun…" warning fires and tooling like latexmk
    reruns automatically — no bespoke message).
  • \dumpmargins force-releases anything still waiting at end of document, so a
    note is never silently dropped if a recorded page is stale.

The behaviour is automatic and needs no markup; like cross-references it just
takes an extra pass to settle.

Compatibility / risk

  • On a single pass (no .aux data yet) the new code is inert — identical
    output to before.
  • Notes that drift forward (the legitimate overflow case) are untouched.
  • \marginpar, \marginnote-free stacking, \clearmargin, phantoms, etc. are
    unchanged.

Testing

  • All eight existing tests build with no errors and no lost notes.
  • New test/anchorpage.tex exercises the two-pass machinery: anchor pages are
    recorded, the rerun warning fires once then clears (convergence), and a
    cluster of three notes on one line is neither lost nor overprinted.
  • Real-world reproduction: the margin citations in the KTH DD2520
    "appliedcrypto" course notes — footnotes whose marks were at the top of a
    page were being set at the foot of the previous page; with this change they
    move back beside their marks, still correctly stacked.

Marked draft for review.

🤖 Generated with Claude Code

A \marginpar is a float, so LaTeX can attach it to the page it is
assembling even when the page then breaks before the line that called
it.  The result is a note whose mark sits at the top of a page but whose
text is set in the margin of the previous page.  marginfix arranges notes
within whatever page LaTeX hands them and can push overflow forwards, but
it had no notion of which page a note belongs to, so it could not repair
this backwards drift.

Fix it across two passes, reusing the kernel's cross-reference machinery:

  * Each real marginpar is numbered in document order (at callout time
    and again in the output routine; the two counters stay in lock-step).
  * At callout we record the callout's page in the .aux with a deferred
    \write, so the page is captured at shipout (the \label trick).
  * On the next run that recorded anchor page lets \@addmarginpar hold a
    note back in \mfx@deferred until its anchor page is shipped, instead
    of setting it too early; \MFX@combinefloats@before releases waiting
    notes at the top of their page, before that page's own notes.
  * Because margin material is attached only after the body column is
    built, margin notes never move the body's page breaks, so anchor
    pages are stable and the process converges, usually in one extra
    pass.  A rerun is requested through the kernel's own \@testdef check
    (so the standard "Label(s) may have changed" warning fires).
  * \dumpmargins force-releases anything still waiting at end of document,
    so a note is never silently dropped if a recorded page is stale.

The behaviour is automatic and needs no markup.  Adds test/anchorpage.tex
and bumps to v1.3.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@dbosk

dbosk commented Jun 23, 2026

Copy link
Copy Markdown
Author

I've confirmed that this works. I'll just need to use it for a while to see if it breaks anything.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant