math: invisible brackets export to LaTeX as a TeX group

A script whose base isn't simple prints as `〖a^b〗^c` in linear text (port of
wordcraft#6084), and LaTeX export copied the brackets into the .tex, where the
exporter's preamble can't set them. They are now a TeX group, `{a^b}^c`; one
without its partner is dropped. Also pins that `〖a+b〗 ^2` keeps the delimiter.

Port of the review fix on wordcraft#6084.
This commit is contained in:
kiwi4boy
2026-11-16 22:21:40 -05:00
parent 8a26ca7e3f
commit 3e61d2e436
3 changed files with 41 additions and 1 deletions
+22
View File
@@ -437,3 +437,25 @@ fn a_tabular_row_keeps_the_cells_a_table_row_holds() {
assert_eq!(t.rows.first().map(Vec::len), Some(craft_wordmodel::table::MAX_COLS));
assert_eq!(t.rows.get(1).map(Vec::len), Some(2));
}
#[test]
fn a_script_on_a_script_exports_as_a_tex_group() {
use craft_math::math::{MNode, MRun, ScriptKind, to_linear};
let a = |t: &str| vec![MNode::Run(MRun::new(t))];
let sup = |base, s| MNode::Script { kind: ScriptKind::Sup, base, sub: Vec::new(), sup: s };
// sSup(sSup(a, b), c), as Word files hold it: the docx reader keeps `to_linear` of it.
let linear = to_linear(&[sup(vec![sup(a("a"), a("b"))], a("c"))]);
assert_eq!(linear, "〖a^b〗^c");
let mut d = Document::new();
let mut p = craft_wordmodel::Paragraph::new();
p.insert_object(0, craft_wordmodel::para::InlineObject::Equation { linear, display: false, math: Default::default() }, &Default::default())
.unwrap();
d.body = vec![craft_wordmodel::para_block(p)];
let tex = export(&d);
// The preamble sets up no Unicode math font, so `〖` would stop pdflatex.
assert!(tex.contains("${a^b}^c$") && !tex.contains('〖') && !tex.contains('〗'), "{tex}");
// Invisible brackets anywhere are a TeX group; one left unmatched is dropped.
for (linear, tex) in [("〖a+b〗^2", "{a+b}^2"), ("x^〖a〗", "x^{a}"), ("〖〖a〗^b〗^c", "{{a}^b}^c"), ("a〗b〖c", "abc")] {
assert_eq!(linear_to_latex(linear), tex, "{linear}");
}
}
+12 -1
View File
@@ -235,6 +235,7 @@ pub fn linear_to_latex(linear: &str) -> String {
fn linear_to_latex_at(linear: &str, depth: usize) -> String {
let chars: Vec<char> = linear.chars().collect();
let parens = closers(&chars, '(', ')');
let brackets = closers(&chars, '〖', '〗');
let mut o = String::new();
let mut i = 0;
let sub = |s: &str| if depth < MAX_NEST { linear_to_latex_at(s, depth + 1) } else { escape_math_text(s) };
@@ -309,6 +310,16 @@ fn linear_to_latex_at(linear: &str, depth: usize) -> String {
o.push('\\');
o.push(c);
}
// Invisible brackets (`〖a^b〗^c`, a script base that isn't simple) only group: a TeX
// group. One without its partner shows nothing and is dropped.
'〖' => {
if let Some((inner, end)) = balanced(&chars, &brackets, i) {
o.push_str(&format!("{{{}}}", sub(&inner)));
i = end;
continue;
}
}
'〗' => {}
'{' => o.push_str("\\{"),
'}' => o.push_str("\\}"),
'~' => o.push_str("\\sim "),
@@ -332,7 +343,7 @@ fn linear_to_latex_at(linear: &str, depth: usize) -> String {
fn escape_math_text(s: &str) -> String {
let mut o = String::new();
for c in s.chars().filter(|c| !matches!(c, '{' | '}' | '\\' | '$' | '%' | '#' | '&') && !c.is_control()) {
for c in s.chars().filter(|c| !matches!(c, '{' | '}' | '\\' | '$' | '%' | '#' | '&' | '〖' | '〗') && !c.is_control()) {
// Never `^^` (see `linear_to_latex_at`).
if c == '^' && o.ends_with('^') {
continue;
+7
View File
@@ -72,3 +72,10 @@ fn an_explicit_delimiter_as_the_base_is_kept() {
// Grouping still leaves none.
assert_eq!(parse_linear("〖a〗^2"), vec![sup(a("a"), a("2"))]);
}
#[test]
fn invisible_brackets_before_a_space_stay_a_delimiter() {
// Only `〗` right before `^`/`_` makes them a script base; after a space they are kept as typed.
let n = parse_linear("〖a+b〗 ^2");
assert!(matches!(&n[..], [MNode::Delim { beg: None, end: None, .. }, ..]), "{n:?}");
}