Respect myheadings marks-only headers

\pagestyle{myheadings} behaved exactly like headings, so it filled the left
header with the current numbered heading. The geometry now records myheadings
separately; in that style the left header shows the explicit \markboth/\markright
text when present and stays empty otherwise, while headings keeps using the
current heading title. The page number behavior is unchanged.

A two-page test sets myheadings and \markright{Custom mark}, then checks the
header shows "CUSTOM MARK" and not "1 INTRODUCTION".
This commit is contained in:
gloamxun committed 2026-10-27 03:09:16 +08:00
1 parent 4ad15f1f0b
commit 0e386f8fdc
4 files changed
+42 -7

No files matched your search

+36 -1
View File
@@ -218,6 +218,8 @@ struct PageGeometry {
fancy_pagestyle: bool,
/// `\pagestyle{headings}` / `{myheadings}` puts the number in the head.
headings_pagestyle: bool,
/// `\pagestyle{myheadings}` uses only explicit marks, not the heading.
myheadings_pagestyle: bool,
/// `\thispagestyle{empty}` suppresses the running head on the first page.
suppress_first_page_header: bool,
header_left: Option<String>,
@@ -3577,6 +3579,9 @@ fn strip_page_geometry(
{
geometry.headings_pagestyle = true;
}
if rest.trim_start().starts_with("{myheadings}") {
geometry.myheadings_pagestyle = true;
}
} else if let Some(rest) = trimmed.strip_prefix("\\thispagestyle") {
if rest.trim_start().starts_with("{empty}") {
geometry.suppress_first_page_number = true;
@@ -13418,7 +13423,15 @@ fn stamp_running_heads(
// LaTeX's headings style uses plain on the first page.
continue;
}
if let Some(Some(title)) = headings.get(index) {
let heading = if geometry.myheadings_pagestyle {
geometry
.running_left
.clone()
.or_else(|| geometry.running_right.clone())
} else {
headings.get(index).cloned().flatten()
};
if let Some(title) = heading {
list.push(DisplayItem::Text {
x: 0,
y: baseline,
@@ -17103,6 +17116,28 @@ A definition.
assert!(text.contains("D"), "{text}");
}
#[test]
fn myheadings_use_only_explicit_marks() {
let source = "\\documentclass{article}\n\\pagestyle{myheadings}\n\\markright{Custom mark}\n\\begin{document}\n\n\\section{Introduction}\n\nFirst.\n\n\\newpage\n\nSecond.\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
assert_eq!(
value["warnings"].as_array().map(Vec::len),
Some(0),
"{}",
value["warnings"]
);
let texts = svg_texts(&json);
assert!(
texts.iter().any(|(text, _, _)| text == "CUSTOM MARK"),
"{texts:?}"
);
assert!(
!texts.iter().any(|(text, _, _)| text == "1 INTRODUCTION"),
"{texts:?}"
);
}
#[test]
fn nameref_resolves_standalone_labels() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\section{Introduction}\n\n\\label{sec:intro}\n\nSee \\nameref{sec:intro} and \\autoref{sec:intro}.\n\n\\end{document}\n";