Add expanded, unexpanded and detokenize

Three expansion primitives class files use were unsupported. \expanded{...}
runs the argument through the current macro table and scopes immediately;
\unexpanded{...} passes the text through unchanged (normal typesetting
semantics; the edef freeze is not modelled); \detokenize{...} prints the text
literally through the same private-use escaping as \string.

A preview test expands \a, passes \a through unexpanded and detokenizes it,
checking "Hi and Hi and \\a." with no warnings.
This commit is contained in:
gloamxun committed 2026-10-27 01:41:52 +08:00
1 parent 28aed153fe
commit 8bdeffa646
3 files changed
+64 -4

No files matched your search

+58
View File
@@ -794,6 +794,21 @@ fn brace_depth_delta(line: &str) -> i32 {
depth
}
fn command_argument(text: &str, command: &str) -> Option<(String, usize)> {
let after = text.strip_prefix(command)?;
if after
.chars()
.next()
.is_some_and(|character| character.is_ascii_alphabetic())
{
return None;
}
let trimmed = after.trim_start();
let skipped = after.len() - trimmed.len();
let (body, used) = braced_group(trimmed)?;
Some((body.to_string(), command.len() + skipped + used))
}
/// `\string<token>` prints the token's characters without expanding it.
fn string_token(text: &str) -> Option<(String, usize)> {
let after = text.strip_prefix("\\string")?;
@@ -1732,6 +1747,28 @@ fn expand_macros_mut(
changed = true;
continue;
}
if let Some((body, used)) = command_argument(rest, "\\expanded") {
let mut table = runtime.clone();
for scope in &scopes {
table.extend(scope.iter().cloned());
}
output.push_str(&expand_macros(&body, &table));
index += used;
changed = true;
continue;
}
if let Some((body, used)) = command_argument(rest, "\\unexpanded") {
output.push_str(&body);
index += used;
changed = true;
continue;
}
if let Some((body, used)) = command_argument(rest, "\\detokenize") {
output.push_str(&escape_literal(&body));
index += used;
changed = true;
continue;
}
if let Some((text, used)) = string_token(rest) {
output.push_str(&text);
index += used;
@@ -16268,6 +16305,27 @@ A definition.
assert!(!text.contains("makeatletter"), "{text}");
}
#[test]
fn expanded_unexpanded_and_detokenize_work() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\a{Hi}\\expanded{\\a} and \\unexpanded{\\a} and \\detokenize{\\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("HiandHiand\\a."), "{text}");
}
#[test]
fn parbox_orcid_and_hrule_are_handled() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\parbox{60mm}{Parbox words} and \\orcidlink{0000-0002-1825-0097}.\n\n\\hrule\n\nAfter.\n\n\\end{document}\n";