Keep the text of mbox and makebox commands

This commit is contained in:
gloamxun committed 2026-10-26 04:17:26 +08:00
1 parent 0c8228eced
commit fb111806e2
3 files changed
+90 -4

No files matched your search

+87 -1
View File
@@ -893,7 +893,7 @@ fn latex_internal_at(rest: &str, macros: &[MacroDefinition]) -> Option<(String,
if let Some(after) = rest.strip_prefix("\\@nameuse") {
if boundary(after) {
let skipped = after.len() - after.trim_start().len();
let after = after.trim_start();
let mut after = after.trim_start();
let (inner, used) = braced_group(after)?;
return Some((
format!("\\{}", inner.trim().trim_start_matches('\\')),
@@ -6798,6 +6798,81 @@ fn split_color_runs(
out
}
fn strip_box_commands(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
loop {
let matches: Vec<(&str, usize)> = ["\\mbox{", "\\hbox{", "\\makebox["]
.iter()
.filter_map(|command| rest.find(command).map(|index| (*command, index)))
.collect();
let Some((command, index)) = matches.into_iter().min_by_key(|(_, index)| *index) else {
out.push_str(rest);
return out;
};
out.push_str(&rest[..index]);
let mut after = &rest[index + command.len()..];
if command == "\\makebox[" {
let Some(close) = after.find(']') else {
out.push_str(&rest[index..]);
return out;
};
after = &after[close + 1..];
let mut after = after.trim_start();
if let Some(stripped) = after.strip_prefix('[') {
if let Some(close) = stripped.find(']') {
after = &stripped[close + 1..];
} else {
out.push_str(&rest[index..]);
return out;
}
}
let mut after = after.trim_start();
while let Some(stripped) = after.strip_prefix('[') {
let Some(close) = stripped.find(']') else {
out.push_str(&rest[index..]);
return out;
};
after = &stripped[close + 1..];
after = after.trim_start();
}
if let Some(stripped) = after.strip_prefix('{') {
let Some(close) = stripped.find('}') else {
out.push_str(&rest[index..]);
return out;
};
out.push_str(&strip_box_commands(&stripped[..close]));
after = &stripped[close + 1..];
rest = after;
continue;
}
out.push_str(&rest[index..]);
return out;
}
let mut depth = 1_usize;
let mut end = None;
for (offset, character) in after.char_indices() {
match character {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
end = Some(offset);
break;
}
}
_ => {}
}
}
let Some(end) = end else {
out.push_str(&rest[index..]);
return out;
};
out.push_str(&strip_box_commands(&after[..end]));
rest = &after[end + 1..];
}
}
fn expand_ensuremath_plain(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
@@ -8919,6 +8994,7 @@ fn build_document_with_labels(
.trim_start()
.to_string();
}
text = strip_box_commands(&text);
text = expand_ensuremath(&text);
if let Some((color, rest)) = color_switch(&text) {
text = format!("\\textcolor{{{color}}}{{{rest}}}");
@@ -12984,6 +13060,16 @@ A definition.
assert_eq!(runs[0].3, "Boxed");
}
#[test]
fn box_commands_keep_their_text() {
let preview =
preview_json("A \\mbox{B C} \\makebox[2cm][l]{D} \\hbox{E}.").expect("preview");
assert!(preview.contains(">B</text>"));
assert!(preview.contains(">E</text>"));
assert!(!preview.contains("mbox"));
assert!(!preview.contains("makebox"));
}
#[test]
fn ensuremath_makes_inline_math() {
let preview = preview_json("A \\ensuremath{x^2} B").expect("preview");