Skip to content

Commit 8abfda4

Browse files
Edwin ChanEdwin Chan
authored andcommitted
Fix imported LaTeX currency notation
1 parent 65d757d commit 8abfda4

6 files changed

Lines changed: 22 additions & 2 deletions

File tree

CENTRAL.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Stack: Next.js, NextAuth (Google + dev bypass), Prisma, KaTeX for math, Lucide i
3838
- **Mobile sidebar focus fix** — mobile sidebar sheet geometry now remains stable under `:hover`, `:focus`, and `:focus-within`, preventing the nav grid from jumping upward when a mobile tap focuses one of the upper links (source: `app/globals.css`).
3939
- **Answer previews and LaTeX grading** — Practice and problem-set answer boxes now render a small KaTeX preview above the input using `mathInputToTex(...)`. `lib/grading.ts` normalizes math delimiters and common LaTeX forms through `lib/math-input.ts`, so expression answers and answer keys can use `$...$`, `\sqrt{...}`, coefficients before functions like `5\sqrt{2}-7`, `\frac{...}{...}`, braced powers, and `\pi`.
4040
- **Expanded LaTeX statement support** — KaTeX is upgraded to `0.17.x`, the official `mhchem` extension enables `\ce{...}`, and `lib/latex-compat.ts` converts document wrappers plus `tabular`, `tabular*`, `tabularx`, and `longtable` into safe KaTeX-compatible arrays. Bare display environments render when mixed with prose. Rendering keeps `trust: false`, `maxSize: 50`, and `maxExpand: 1000`; full TeX document compilation and resource-loading commands remain unsupported. See `docs/latex-support.md`.
41+
- **Legacy LaTeX currency compatibility** — imported contest statements using full-LaTeX `\textdollar` or invalid currency sequences such as `\54` now render through bounded aliases in `lib/latex-compat.ts`; escaped row breaks remain unchanged.
4142
- **Anime.js animation pass** — the shared `MathCurveLoader` now drives SVG path drawing, rotation, and dot pulses with Anime.js v4 instead of CSS keyframes, `AnimeRouteEffects` adds reduced-motion-aware route reveal animations for key panels/rows/actions, and analytics surfaces use `AnalyticsMotion` plus animated `TrendChart` path/dots for growing bars and drawn charts. `TypewriterGreeting` keeps the existing typed/deleted text state machine and only animates the caret, avoiding per-character text tween flicker. Dependency: `animejs` in `package.json`.
4243
- **Per-set analytics access** — admins can open `/admin/sets/[id]/analytics` directly from the student-facing problem set header and the admin set editor; the editor delete action uses full-size topbar button sizing while the list keeps compact row actions.
4344
- **Problem-set writeups**`/problem-sets/[slug]/writeups` lets signed-in users post LaTeX/HTML solution notes with up to four images, then sort by latest/top and upvote/downvote posts. Authors can delete their own writeups with confirmation; admins can delete any writeup. `/writeups` is the sidebar directory for latest/top writeups and problem-set search. Persistence uses new `Writeup`, `WriteupImage`, and `WriteupVote` Prisma models plus `lib/writeup-images.ts`; image bytes are stored through the existing storage layer and streamed via `/api/files/[id]`.

DBSMO/Projects/dbsmo/Components.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ This note maps important [[dbsmo]] UI/components to their source files and usage
3939
- Writeup header link in `app/problem-sets/[slug]/page.tsx`: icon link next to `BookmarkButton` that opens `/problem-sets/[slug]/writeups`.
4040
- `WriteupsPage` and `WriteupsClient` in `app/problem-sets/[slug]/writeups/`: server/client pair for set writeups. The server page handles auth, set visibility, sorting, and initial data; the client component handles the composer, image selection, optimistic voting, confirm-delete controls, and feed cards rendered with `LatexStatement`.
4141
- `WriteupsDirectoryPage` in `app/writeups/page.tsx`: sidebar community writeups page with latest/top tabs and problem-set-focused search suggestions; it reuses `WriteupsClient` without the composer.
42-
- `LatexStatement` in `app/problem-sets/[slug]/latex-statement.tsx`: server-side KaTeX renderer for LaTeX/HTML-style statements and imported image assets. It detects delimited and bare display environments, loads the official `mhchem` extension, and applies `lib/latex-compat.ts` before rendering. Compatibility converts document wrappers and common table environments to arrays while preserving `trust: false` and explicit expansion/size limits (sources: named files, `tests/latex-statement.test.ts`, `docs/latex-support.md`).
42+
- `LatexStatement` in `app/problem-sets/[slug]/latex-statement.tsx`: server-side KaTeX renderer for LaTeX/HTML-style statements and imported image assets. It detects delimited and bare display environments, loads the official `mhchem` extension, and applies `lib/latex-compat.ts` before rendering. Compatibility converts document wrappers and common table environments to arrays, and repairs legacy contest currency forms such as `\textdollar` and `\54`, while preserving `trust: false` and explicit expansion/size limits (sources: named files, `tests/latex-statement.test.ts`, `docs/latex-support.md`).
4343

4444
## Practice and Games
4545

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ Session updates from the CodeGraph/Second Brain indexing pass onward:
5555
- Removed the global yellow wavy eyebrow/title underlines and moved the handwritten character into stable two-pass ink borders for cards, panels, controls, and form fields.
5656
- Restored the desktop sidebar as a compact icon rail that expands on hover/focus, preserved the stable mobile sheet, and introduced a shared simplified Sigma favicon/landing mark.
5757
- Upgraded KaTeX to v0.17, enabled the official `mhchem` extension, and added secure compatibility rendering for document wrappers and table environments such as `tabular`, `tabular*`, `tabularx`, and `longtable`.
58+
- Fixed imported contest currency notation so full-LaTeX `\textdollar` and legacy sequences such as `\54` render as dollar amounts instead of KaTeX errors.
5859

5960
© 2026 Cosmic Crusader

docs/latex-support.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ DBSMO renders mathematical LaTeX with KaTeX `0.17.x` in `app/problem-sets/[slug]
88
- Display math: `$$...$$` and `\[...\]`.
99
- Bare display environments mixed with prose, including `array`, matrix variants, `align`, `gather`, `equation`, `cases`, and `CD`.
1010
- Chemistry through KaTeX's official `mhchem` extension, for example `$\ce{2H2 + O2 -> 2H2O}$`.
11-
- Common shorthand macros: `\RR`, `\NN`, `\ZZ`, `\QQ`, `\CC`, and `\degree`.
11+
- Common shorthand macros: `\RR`, `\NN`, `\ZZ`, `\QQ`, `\CC`, `\degree`, and `\textdollar`.
12+
- Legacy contest imports that encode currency as an invalid backslash-number sequence such as `\54`; this is normalized to `\$54` before rendering.
1213
- Imported image tokens such as `[[img:diagram1]]`; images remain separate from LaTeX and are served through the application's validated asset path.
1314

1415
## Table Compatibility

lib/latex-compat.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const KATEX_COMPAT_MACROS: Record<string, string> = {
1010
"\\QQ": "\\mathbb{Q}",
1111
"\\CC": "\\mathbb{C}",
1212
"\\degree": "^{\\circ}",
13+
"\\textdollar": "\\$",
1314
};
1415

1516
type BracedGroup = {
@@ -204,6 +205,7 @@ export function normalizeLatexStatementSource(value: string): string {
204205

205206
export function normalizeLatexForKatex(value: string): string {
206207
let normalized = normalizeLatexStatementSource(value);
208+
normalized = normalized.replace(/(^|[^\\])\\(?=\d)/g, "$1\\$");
207209
normalized = convertTabularEnvironment(normalized, "tabular*");
208210
normalized = convertTabularEnvironment(normalized, "tabularx");
209211
normalized = convertTabularEnvironment(normalized, "longtable");

tests/latex-statement.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,19 @@ x & $y$ \\
8787
expect(html).toContain(String.raw`\ce{H2 + O2`);
8888
expect(html).not.toContain("katex-error");
8989
});
90+
91+
it("renders legacy currency commands from imported contest problems", () => {
92+
const html = renderToStaticMarkup(
93+
React.createElement(LatexStatement, {
94+
statement: String.raw`Sale prices are $50\%$ below the original price, with another $20\%$ discount.
95+
A coat originally costs $\textdollar180$. Choose $\text{(A)}\ \54 \qquad \text{(B)}\ \72 \qquad \text{(C)}\ \90$.`,
96+
format: "LATEX",
97+
}),
98+
);
99+
100+
expect(html).toContain("Sale prices are");
101+
expect(html).toContain("50");
102+
expect(html).toContain("180");
103+
expect(html).not.toContain("katex-error");
104+
});
90105
});

0 commit comments

Comments
 (0)