Skip to content

Commit 6f3b04a

Browse files
committed
webui: LaTeX in table-cells
1 parent c476160 commit 6f3b04a

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

tools/server/webui/src/lib/utils/latex-protection.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,23 @@ $$`);
191191
});
192192

193193
test('converts \\[ ... \\] even when preceded by text without space', () => {
194-
const input = 'Algebra: \\[x = \\frac{-b \\pm \\sqrt{\\,b^{2}-4ac\\,}}{2a}\\]';
194+
const input = 'Some line ...\nAlgebra: \\[x = \\frac{-b \\pm \\sqrt{\\,b^{2}-4ac\\,}}{2a}\\]';
195195
const output = preprocessLaTeX(input);
196196

197-
expect(output).toBe('Algebra: $$x = \\frac{-b \\pm \\sqrt{\\,b^{2}-4ac\\,}}{2a}$$');
197+
expect(output).toBe(
198+
'Some line ...\nAlgebra: \n$$x = \\frac{-b \\pm \\sqrt{\\,b^{2}-4ac\\,}}{2a}$$\n'
199+
);
200+
});
201+
202+
test('converts \\[ ... \\] in table-cells', () => {
203+
const input = `| ID | Expression |\n| #1 | \\[
204+
x = \\frac{-b \\pm \\sqrt{\\,b^{2}-4ac\\,}}{2a}
205+
\\] |`;
206+
const output = preprocessLaTeX(input);
207+
208+
expect(output).toBe(
209+
'| ID | Expression |\n| #1 | $x = \\frac{-b \\pm \\sqrt{\\,b^{2}-4ac\\,}}{2a}$ |'
210+
);
198211
});
199212

200213
test('escapes isolated $ before digits ($5 → \\$5), but not valid math', () => {

tools/server/webui/src/lib/utils/latex-protection.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ export function preprocessLaTeX(content: string): string {
138138
// Step 2: Protect existing LaTeX expressions
139139
const latexExpressions: string[] = [];
140140

141+
// Match \S...\[...\] and protect them and insert a line-break.
142+
content = content.replace(/([\S].*?)\\\[([\s\S]*?)\\\](.*)/g, (match, group1, group2, group3) => {
143+
// Check if there are characters following the formula (display-formula in a table-cell?)
144+
const hasSuffix = /\S/.test(group3);
145+
let optBreak;
146+
if (hasSuffix) {
147+
latexExpressions.push(`\\(${group2.trim()}\\)`); // Convert into inline.
148+
optBreak = '';
149+
} else {
150+
latexExpressions.push(`\\[${group2}\\]`);
151+
optBreak = '\n';
152+
}
153+
return `${group1}${optBreak}<<LATEX_${latexExpressions.length - 1}>>${optBreak}${group3}`;
154+
});
155+
141156
// Match \(...\), \[...\], $$...$$ and protect them
142157
content = content.replace(/(\$\$[\s\S]*?\$\$|\\\[[\s\S]*?\\\]|\\\(.*?\\\))/g, (match) => {
143158
latexExpressions.push(match);

tools/server/webui/src/stories/fixtures/math-formulas.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-irregular-whitespace */
12
// Math Formulas Content
23
export const MATH_FORMULAS_MD = String.raw`
34
# Mathematical Formulas and Expressions
@@ -157,10 +158,35 @@ $$\lim_{n \to \infty} \left(1 + \frac{x}{n}\right)^n = e^x$$
157158
\[
158159
\left\{ \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}, \begin{pmatrix} -1 & 0 \\ 0 & -1 \end{pmatrix} \right\} = \{\pm I\}
159160
\]
161+
- Algebra:
162+
\[
163+
x = \frac{-b \pm \sqrt{\,b^{2}-4ac\,}}{2a}
164+
\]
160165
- $100 and $12.99 are amounts, not LaTeX.
161166
- I have $10, $3.99 and $x + y$ and $100x$. The amount is $2,000.
162167
- Emma buys 2 cupcakes for $3 each and 1 cookie for $1.50. How much money does she spend in total?
163168
- Maria has $20. She buys a notebook for $4.75 and a pack of pencils for $3.25. How much change does she receive?
169+
- 1 kg の質量は
170+
\[
171+
E = (1\ \text{kg}) \times (3.0 \times 10^8\ \text{m/s})^2 \approx 9.0 \times 10^{16}\ \text{J}
172+
\]
173+
というエネルギーに相当します。これは約 21 百万トンの TNT が爆発したときのエネルギーに匹敵します。
174+
- Algebra: \[
175+
x = \frac{-b \pm \sqrt{\,b^{2}-4ac\,}}{2a}
176+
\]
177+
178+
## Formulas in a Table
179+
180+
| Area | Expression | Comment |
181+
|------|------------|---------|
182+
| **Algebra** | \[
183+
x = \frac{-b \pm \sqrt{\,b^{2}-4ac\,}}{2a}
184+
\] | Quadratic formula |
185+
| | \[
186+
(a+b)^{n} = \sum_{k=0}^{n}\binom{n}{k}\,a^{\,n-k}\,b^{\,k}
187+
\] | Binomial theorem |
188+
| | \(\displaystyle \prod_{k=1}^{n}k = n! \) | Factorial definition |
189+
| **Geometry** | \( \mathbf{a}\cdot \mathbf{b} = \|\mathbf{a}\|\,\|\mathbf{b}\|\,\cos\theta \) | Dot product & angle |
164190
165191
---
166192

0 commit comments

Comments
 (0)