Consume mark commands written inside running text

The line-start forms of \markboth and \markright were handled, but an inline
"Body \markright{Short title} text." leaked the command. A body pass now finds
either command anywhere, records the left/right marks for the running heads,
and removes the command from the text (malformed brackets are left untouched).

A fancy-header test puts \markright inside a paragraph and checks the header
shows "Short title" with no warnings and no leaked command.
This commit is contained in:
gloamxun committed 2026-10-27 02:53:27 +08:00
1 parent 5dda1a948a
commit 1227d82f02
4 files changed
+74 -6

No files matched your search

+67
View File
@@ -6442,6 +6442,7 @@ fn geometry_and_body_with_images(
body = expand_skip_registers(&body, &mut skip_registers);
geometry.skip_registers = skip_registers;
body = expand_links(&body);
body = strip_inline_marks(&body, &mut geometry);
let today = today_string();
body = expand_today(&body, &today);
body = expand_lipsum(&body);
@@ -9914,6 +9915,52 @@ fn expand_phantoms(text: &str) -> String {
output
}
/// Consume `\markboth`/`\markright` that appear inside running text (the
/// line-start forms are handled while the page geometry is read) and record the
/// most recent marks for the running heads.
fn strip_inline_marks(body: &str, geometry: &mut PageGeometry) -> String {
let mut output = String::new();
let mut rest = body;
loop {
let mut next: Option<(usize, &str)> = None;
for command in ["\\markboth", "\\markright"] {
if let Some(index) = rest.find(command) {
let better = next.is_none_or(|(known, _)| index < known);
if better {
next = Some((index, command));
}
}
}
let Some((index, command)) = next else {
output.push_str(rest);
break;
};
output.push_str(&rest[..index]);
let after = rest[index + command.len()..].trim_start();
let (left, tail) = if command == "\\markboth" {
let Some((left, used)) = braced_group(after) else {
output.push_str(command);
rest = &rest[index + command.len()..];
continue;
};
(Some(left.trim().to_string()), after[used..].trim_start())
} else {
(None, after)
};
let Some((right, used)) = braced_group(tail) else {
output.push_str(command);
rest = &rest[index + command.len()..];
continue;
};
if let Some(left) = left {
geometry.running_left = Some(left);
}
geometry.running_right = Some(right.trim().to_string());
rest = &tail[used..];
}
output
}
/// Keep the visible label of `\hyperref[label]{text}`, `\hyperlink{name}{text}`
/// and `\hypertarget{name}{text}`; internal PDF targets are not modelled.
fn strip_hyperref_commands(text: &str) -> String {
@@ -16843,6 +16890,26 @@ A definition.
assert!(!text.contains("??"), "{text}");
}
#[test]
fn inline_marks_set_the_running_head() {
let source = "\\documentclass{article}\n\\pagestyle{fancy}\n\\fancyhf{}\n\\fancyhead[R]{\\rightmark}\n\\begin{document}\n\nBody \\markright{Short title} text.\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"]
);
assert!(!value.to_string().contains("markright"), "{json}");
let texts = svg_texts(&json);
let header = texts
.iter()
.find(|(text, _, _)| text == "Short title")
.expect("header");
assert!(header.2 < 0.0, "header should sit above the body");
}
#[test]
fn autoref_uses_the_float_kind() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{figure}[h]\n\\rule{10pt}{10pt}\n\\caption{Cap}\\label{fig:a}\n\\end{figure}\n\nSee \\autoref{fig:a}.\n\n\\end{document}\n";