fix: rendering in typst
Pipeline / check (pull_request) Successful in 3m8s
Pipeline / docs (pull_request) Skipped
Pipeline / nightly (pull_request) Skipped
Pipeline / release (pull_request) Skipped

This commit is contained in:
alexm committed 2026-09-22 00:19:40 -04:00
1 parent 003340dc6f
commit cfe8a3216c
8 files changed
+414 -81

No files matched your search

+20 -68
View File
@@ -1066,61 +1066,27 @@ fn numeric_map(map: &BTreeMap<String, f64>) -> Value {
)
}
/// Rewrites inline LaTeX math (`$...$`) into a call to mitex's `mi`, so Typst's
/// own math grammar never has to parse it. Typst's `\times`, `\Delta`, `\ln` and
/// friends are not valid Typst math — a bare backslash escapes the next
/// character instead of naming a symbol — which is why equations compile but
/// print wrong instead of failing outright. `\$` is left alone, matching LaTeX's
/// own convention for a literal dollar sign, and a `$` with no matching close is
/// left alone too, rather than swallowing the rest of the field.
fn rewrite_latex_math(source: &str) -> String {
let chars: Vec<(usize, char)> = source.char_indices().collect();
let mut out = String::with_capacity(source.len());
let mut i = 0;
while i < chars.len() {
let (_, c) = chars[i];
if c == '\\' && i + 1 < chars.len() {
out.push('\\');
out.push(chars[i + 1].1);
i += 2;
continue;
}
if c != '$' {
out.push(c);
i += 1;
continue;
}
let mut j = i + 1;
let close = loop {
if j >= chars.len() {
break None;
}
match chars[j].1 {
'\\' => j += 2,
'$' => break Some(j),
_ => j += 1,
}
};
match close {
Some(close_idx) => {
let start = chars[i + 1].0;
let end = chars[close_idx].0;
out.push_str("#mi(");
out.push_str(&Value::str(&source[start..end]).to_typst(0));
out.push(')');
i = close_idx + 1;
}
None => {
out.push('$');
i += 1;
}
}
}
out
}
/// Emits one markup-bearing field for a diagnostic report.
///
/// The conversion itself lives in [`markup::to_typst`], which is the single
/// entry point for turning authoring markup into Typst: it rewrites inline
/// LaTeX math, routes mhchem through whalogen, and escapes the characters Typst
/// treats specially in content mode. This path used to carry its own copy of
/// the math rewriting, which drifted — the reports escaped nothing outside math
/// and silently disagreed with the exam papers about an unmatched `$`.
///
/// # Arguments
///
/// * `source` - the authoring source.
/// * `content` - whether to emit a content block rather than a quoted string.
/// A string is evaluated by the template with `eval(.., mode: "markup")`, so
/// both modes need the same escaping.
///
/// # Returns
///
/// The value to place in the payload.
pub(crate) fn markup_value(source: &str, content: bool) -> Value {
let source = rewrite_latex_math(source);
let source = markup::to_typst(source);
if content {
Value::content(source)
} else {
@@ -1270,17 +1236,3 @@ mod tests {
assert!(!balanced("closing ] first"));
}
}
#[test]
fn latex_math_becomes_a_mitex_call() {
assert_eq!(
rewrite_latex_math("angle $\\phi$ (phi)"),
"angle #mi(\"\\\\phi\") (phi)"
);
}
#[test]
fn escaped_and_unmatched_dollar_signs_are_left_alone() {
assert_eq!(rewrite_latex_math("costs \\$5 total"), "costs \\$5 total");
assert_eq!(rewrite_latex_math("just $5"), "just $5");
}