diff --git a/crates/latex/src/tests.rs b/crates/latex/src/tests.rs index ccf349c80..ebffe770e 100644 --- a/crates/latex/src/tests.rs +++ b/crates/latex/src/tests.rs @@ -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}"); + } +} diff --git a/crates/math/src/latex_text.rs b/crates/math/src/latex_text.rs index 5541a36c3..24d9638e8 100644 --- a/crates/math/src/latex_text.rs +++ b/crates/math/src/latex_text.rs @@ -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 = 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; diff --git a/crates/math/tests/linear_script_base.rs b/crates/math/tests/linear_script_base.rs index 8a6505b14..9c655ee4f 100644 --- a/crates/math/tests/linear_script_base.rs +++ b/crates/math/tests/linear_script_base.rs @@ -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:?}"); +}