mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Render LaTeX underbraces in TUI display math (#62577)
## Why
An unsupported `\underbrace` leaves the display equation as LaTeX source,
so readers have to interpret which terms each label groups.
## What changed
Render `\underbrace{expression}_{label}` with a Unicode brace that spans
the expression's terminal columns and a label centered below it. Keep
surrounding math aligned with the expression. Inline math, unsupported
arguments, and layouts that exceed the available width still use the
existing fallback.
## Testing
Add a snapshot for labeled Boolean clauses, short expressions, and labels
wider than their expressions. Check fallback for inline, malformed,
nested, multiline, and over-width cases.
GitOrigin-RevId: 8fbe92d512d06c73cf21247343ad869743bebd0c
This commit is contained in:
@@ -247,7 +247,7 @@ impl MathParser<'_> {
|
||||
{
|
||||
self.display_operator(symbol(name)?)
|
||||
}
|
||||
"begin" | "boxed" | "underset" | "overset" | "substack" | "mathcal" => {
|
||||
"begin" | "boxed" | "underset" | "overset" | "underbrace" | "substack" | "mathcal" => {
|
||||
self.structured_command(name)
|
||||
}
|
||||
"frac" | "dfrac" | "tfrac" => {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Small, bounded layouts for aligned equations, parenthesized matrices, and optimization notation.
|
||||
//! Cell parsing shares the parent parser's depth budget and preserves nested groups.
|
||||
//! Surrounding math aligns to the first row of an aligned block and the middle of a matrix.
|
||||
//! Display annotations and operator limits are centered above or below their base.
|
||||
//! Display annotations and operator limits are centered above or below their base;
|
||||
//! underbrace markers retain the exact width of their expression.
|
||||
|
||||
use super::Layout;
|
||||
use super::MAX_COLUMNS;
|
||||
@@ -124,6 +125,54 @@ impl MathParser<'_> {
|
||||
// Parenthesize the base so an annotation cannot attach to only its last atom.
|
||||
Some(Layout::text(format!("({base}){marker}{{{annotation}}}")))
|
||||
}
|
||||
"underbrace" => {
|
||||
if self.mode != MathMode::Display || !self.stack_annotations {
|
||||
return None;
|
||||
}
|
||||
self.remaining = self.remaining.trim_start();
|
||||
if !self.remaining.starts_with('{') {
|
||||
return None;
|
||||
}
|
||||
let expression = self.compact_argument()?;
|
||||
let expression = expression.single()?.trim();
|
||||
self.remaining = self.remaining.trim_start().strip_prefix('_')?.trim_start();
|
||||
if !self.remaining.starts_with('{') {
|
||||
return None;
|
||||
}
|
||||
let label = self.compact_argument()?;
|
||||
let label = label.single()?.trim();
|
||||
let width = display_width(expression);
|
||||
let label_width = display_width(label);
|
||||
if width == 0 || label_width == 0 {
|
||||
return None;
|
||||
}
|
||||
// Use the same left-center column for the marker tip and the label.
|
||||
let tip = (width - 1) / 2;
|
||||
let label_center = (label_width - 1) / 2;
|
||||
let expression_padding = label_center.saturating_sub(tip);
|
||||
let label_padding = tip.saturating_sub(label_center);
|
||||
if (expression_padding + width).max(label_padding + label_width) > MAX_COLUMNS {
|
||||
return None;
|
||||
}
|
||||
let marker = match width {
|
||||
1 => "┬".to_owned(),
|
||||
2 => "└┘".to_owned(),
|
||||
_ => format!(
|
||||
"└{}┬{}┘",
|
||||
"─".repeat((width - 3) / 2),
|
||||
"─".repeat((width - 2) / 2)
|
||||
),
|
||||
};
|
||||
let padding = " ".repeat(expression_padding);
|
||||
Some(Layout {
|
||||
rows: vec![
|
||||
format!("{padding}{expression}"),
|
||||
format!("{padding}{marker}"),
|
||||
format!("{}{label}", " ".repeat(label_padding)),
|
||||
],
|
||||
baseline: 0,
|
||||
})
|
||||
}
|
||||
"substack" => {
|
||||
self.remaining = self.remaining.trim_start().strip_prefix('{')?;
|
||||
let mut rows = Vec::new();
|
||||
|
||||
@@ -196,6 +196,37 @@ Inline: \(\sum_{i=1}^N x_i\), \(\underset{x\ge0}{\min} x\).",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unicode_math_underbrace() {
|
||||
insta::assert_snapshot!(plain(
|
||||
r"\[
|
||||
\varphi=\underbrace{(x\lor y\lor z)}_{C_1}\land\underbrace{(\neg x\lor y)}_{C_2}
|
||||
\]
|
||||
|
||||
$$\underbrace{x}_{\text{term}}+\underbrace{xy}_{C_3}+\underbrace{x+y}_{C_4}$$
|
||||
|
||||
$$\underbrace{a+bc}_{\text{total}}$$",
|
||||
/*width*/ 80
|
||||
));
|
||||
for source in [
|
||||
r"\underbrace{x+y}",
|
||||
r"\underbrace{x+y}_C",
|
||||
r"\underbrace{}_{C}",
|
||||
r"\underbrace{x}_{}",
|
||||
r"\underbrace{\frac{x}{y}}_{C}",
|
||||
r"\underbrace{x}_{\frac{a}{b}}",
|
||||
r"\underbrace{\underbrace{x}_{a}}_{b}",
|
||||
] {
|
||||
assert_eq!(render(source, MathMode::Display), None, "{source}");
|
||||
}
|
||||
assert_eq!(render(r"\underbrace{x}_{C}", MathMode::Inline), None);
|
||||
let formula = r"$$\underbrace{abcdefghij}_{C_1}$$";
|
||||
assert_eq!(
|
||||
plain(formula, /*width*/ 12),
|
||||
plain(&format!("`{formula}`"), /*width*/ 12)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unicode_math_grouped_scripts_and_annotations() {
|
||||
insta::assert_snapshot!(plain(
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
---
|
||||
source: tui/src/markdown_render/math_display_tests.rs
|
||||
expression: "plain(r\"\\[\n\\varphi=\\underbrace{(x\\lor y\\lor z)}_{C_1}\\land\\underbrace{(\\neg x\\lor y)}_{C_2}\n\\]\n\n$$\\underbrace{x}_{\\text{term}}+\\underbrace{xy}_{C_3}+\\underbrace{x+y}_{C_4}$$\n\n$$\\underbrace{a+bc}_{\\text{total}}$$\",\n80)"
|
||||
---
|
||||
φ=(x∨ y∨ z)∧ (¬ x∨ y)
|
||||
└───┬───┘ └──┬───┘
|
||||
C₁ C₂
|
||||
|
||||
x + xy+ x+y
|
||||
┬ └┘ └┬┘
|
||||
term C₃ C₄
|
||||
|
||||
a+bc
|
||||
└┬─┘
|
||||
total
|
||||
Reference in New Issue
Block a user