Lay out ensuremath bodies as math
This commit is contained in:
1 parent
0340878909
commit
0c8228eced
4 files changed
+87
-3
No files matched your search
@@ -6798,6 +6798,68 @@ fn split_color_runs(
|
||||
out
|
||||
}
|
||||
|
||||
fn expand_ensuremath_plain(text: &str) -> String {
|
||||
let mut out = String::new();
|
||||
let mut rest = text;
|
||||
while let Some(index) = rest.find("\\ensuremath{") {
|
||||
out.push_str(&rest[..index]);
|
||||
let after_open = &rest[index + "\\ensuremath{".len()..];
|
||||
let mut depth = 1_usize;
|
||||
let mut end = None;
|
||||
for (offset, character) in after_open.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('$');
|
||||
out.push_str(&after_open[..end]);
|
||||
out.push('$');
|
||||
rest = &after_open[end + 1..];
|
||||
}
|
||||
out.push_str(rest);
|
||||
out
|
||||
}
|
||||
|
||||
/// Expand `\\ensuremath{...}` outside math mode into an inline math run.
|
||||
fn expand_ensuremath(text: &str) -> String {
|
||||
let mut out = String::new();
|
||||
let mut rest = text;
|
||||
let mut math_mode = false;
|
||||
while !rest.is_empty() {
|
||||
if math_mode {
|
||||
if let Some(position) = rest.find('$') {
|
||||
out.push_str(&rest[..=position]);
|
||||
rest = &rest[position + 1..];
|
||||
math_mode = false;
|
||||
} else {
|
||||
out.push_str(rest);
|
||||
break;
|
||||
}
|
||||
} else if let Some(position) = rest.find('$') {
|
||||
out.push_str(&expand_ensuremath_plain(&rest[..position]));
|
||||
out.push('$');
|
||||
rest = &rest[position + 1..];
|
||||
math_mode = true;
|
||||
} else {
|
||||
out.push_str(&expand_ensuremath_plain(rest));
|
||||
break;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn inline_math_segments(
|
||||
text: &str,
|
||||
base: ParagraphFont,
|
||||
@@ -8857,6 +8919,7 @@ fn build_document_with_labels(
|
||||
.trim_start()
|
||||
.to_string();
|
||||
}
|
||||
text = expand_ensuremath(&text);
|
||||
if let Some((color, rest)) = color_switch(&text) {
|
||||
text = format!("\\textcolor{{{color}}}{{{rest}}}");
|
||||
}
|
||||
@@ -12921,6 +12984,13 @@ A definition.
|
||||
assert_eq!(runs[0].3, "Boxed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ensuremath_makes_inline_math() {
|
||||
let preview = preview_json("A \\ensuremath{x^2} B").expect("preview");
|
||||
assert!(preview.contains("font-style=\\\"italic\\\""));
|
||||
assert!(!preview.contains("ensuremath"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tildes_make_spaces() {
|
||||
let json = preview_json("Figure~1.").expect("preview");
|
||||
|
||||
Reference in new issue
Block a user