Keep each margin note on the page of its callout#25
Draft
dbosk wants to merge 1 commit into
Draft
Conversation
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>
Author
|
I've confirmed that this works. I'll just need to use it for a while to see if it breaks anything. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A
\marginparis a float, so LaTeX can attach it to the page it is currentlyassembling 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 placementavoids 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:
(
\mfx@callid) and again in the output routine (\mfx@placeid); the twocounters stay in lock-step because every real marginpar passes through each
exactly once. Injected control marginpars are skipped.
.auxwith a deferred\write, so the page is captured at shipout (the same trick\labeluses).
\@addmarginparhold anote back in
\mfx@deferreduntil its anchor page is shipped, instead ofsetting it too early.
\MFX@combinefloats@beforereleases waiting notes atthe top of their page, ahead of that page's own notes, preserving document
order for clusters.
margin notes never move the body's page breaks → anchor pages are stable →
the process converges, usually in a single extra pass.
\@testdefcheck (so the standard"Label(s) may have changed. Rerun…" warning fires and tooling like latexmk
reruns automatically — no bespoke message).
\dumpmarginsforce-releases anything still waiting at end of document, so anote 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
.auxdata yet) the new code is inert — identicaloutput to before.
\marginpar,\marginnote-free stacking,\clearmargin, phantoms, etc. areunchanged.
Testing
test/anchorpage.texexercises the two-pass machinery: anchor pages arerecorded, the rerun warning fires once then clears (convergence), and a
cluster of three notes on one line is neither lost nor overprinted.
"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