Honour protect in edef but expand it normally
\protect was just a no-op token, so \edef\b{\protect\a} expanded \a at
definition time. In an edef-style pass the expander now copies \protect and the
following token unchanged (so the stored body keeps the protected command); in
the ordinary document pass it drops \protect and lets the token expand as
LaTeX does when the text is typeset.
A test defines a=A, \edef b={\protect\a}, redefines a=C, and checks both \b and
a literal \protect\a print C with no warnings.
This commit is contained in:
1 parent
c3ae52452d
commit
53a299bd58
4 files changed
+45
-6
No files matched your search
@@ -1813,6 +1813,22 @@ fn expand_macros_inner(
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Some(after) = rest.strip_prefix("\\protect")
|
||||
&& !after
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|character| character.is_ascii_alphabetic())
|
||||
{
|
||||
if freeze_unexpanded && let Some((token, used)) = next_token(after) {
|
||||
output.push_str("\\protect");
|
||||
output.push_str(&token);
|
||||
index += "\\protect".len() + used;
|
||||
} else {
|
||||
index += "\\protect".len();
|
||||
}
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Some((body, used)) = command_argument(rest, "\\expanded") {
|
||||
let mut table = runtime.clone();
|
||||
for scope in &scopes {
|
||||
@@ -16375,6 +16391,27 @@ A definition.
|
||||
assert!(!text.contains("makeatletter"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn protect_survives_edef_and_expands_normally() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\a{A}\n\\edef\\b{\\protect\\a}\n\\def\\a{C}\n\n\\b and \\protect\\a\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 svg = value["pages"][0]["svg"].as_str().expect("svg");
|
||||
let text: String = svg
|
||||
.split('>')
|
||||
.filter_map(|piece| piece.split('<').next())
|
||||
.collect::<String>()
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
assert!(text.contains("CandC"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csname_resolves_parameterised_targets() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\foo#1{[#1]}\n\n\\csname foo\\endcsname{x} and \\csname foo\\endcsname{y}\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user