Skip to content

fix(desktop): correct pipe handling for array column specs and kets#4320

Open
lightfront wants to merge 1 commit into
esengine:main-v2from
lightfront:fix/pipe-column-spec-and-kets
Open

fix(desktop): correct pipe handling for array column specs and kets#4320
lightfront wants to merge 1 commit into
esengine:main-v2from
lightfront:fix/pipe-column-spec-and-kets

Conversation

@lightfront

Copy link
Copy Markdown
Contributor

What this fixes

Two distinct bugs in latexNormalize.ts pipe (|) handling that surface as broken math rendering in the chat. Both are edge cases of the existing |\vert rule that PR #3666 established.

1. Array column specs corrupted — KaTeX parse error

\begin{array}{c|c} was rewritten to \begin{array}{c\vert c}, causing:

KaTeX parse error: Unknown column alignment: \vert

In LaTeX, the | inside an array/tabular column specification means "draw a vertical rule" between columns. It must not be rewritten to \vert.

Fix: COLUMN_SPEC_ENVS lists environments whose first {...} argument is a column spec (array, tabular, tabularx, longtable, subarray). When \begin{<env>} is encountered, the column-spec brace group is copied verbatim — no | or % rewriting. Pipes outside the spec still convert to \vert normally.

2. Ket delimiters in GFM tables render as double bars

In Markdown tables, | is the column delimiter. To write a ket |ψ⟩ inside a table cell, the | must be escaped as \| — otherwise the table breaks. But \| in LaTeX/KaTeX is the parallel-to symbol (‖, U+2225), the heavy double bar used for norms, not the light single bar (∣, U+2223) that kets use. The result: |uud⟩ rendered as ‖uud⟩.

Fix: fixKetPipes() converts \|\vert when it is:

  • A ket opener: \|...\rangle (unpaired, ends in \rangle)
  • A bra closer: \langle...\| (unpaired, starts with \langle)

While preserving norms: matched \|...\| pairs keep the double bar. Disambiguation is by forward scan to the next \| or \rangle, with a backward scan for unmatched \langle to catch bra closers at the end of an expression.

Pattern Meaning Result
|uud\rangle ket \vert (single bar ∣)
\langle\psi| bra \vert (single bar ∣)
\langle x | y \rangle inner product \vert (single bar ∣)
|x| norm kept as | (double bar ‖)

Testing

  • 121 passed, 0 failed (was 109; +12 new golden tests)
  • TypeScript compiles cleanly
  • New tests cover column-spec preservation, ket/bra conversion, norm preservation, and end-to-end KaTeX render of all three cases through the full normalizeMathremark-mathrehype-katex pipeline

Relationship to other PRs

Related to #3666 (inline math rendering hardening) — that PR established the math pre-pass infrastructure and the |\vert rule. This PR fixes two edge cases of that rule. Independent of #3750 (\slashed) and #4216 (Young diagrams).

Two distinct bugs in latexNormalize.ts | handling that surfaced as
broken math rendering in the chat:

1. Array column specs corrupted

   \begin{array}{c|c} was rewritten to \begin{array}{c\vert c},
   causing KaTeX "Unknown column alignment: \vert" parse errors. In
   LaTeX, the | inside an array/tabular preamble means "draw a
   vertical rule" between columns and must not become \vert.

   Fix: COLUMN_SPEC_ENVS lists environments whose first {...} arg is
   a column spec. When \begin{<env>} is found, the spec brace group
   is copied verbatim (no | or % rewriting). Pipes outside the spec
   still convert to \vert normally.

2. Ket delimiters in GFM tables render as double bars

   In Markdown tables, | is the column delimiter, so an LLM writes
   kets as \|uud\rangle to avoid breaking the table. But \| in KaTeX
   is the "parallel-to" symbol (U+2225) the heavy double bar used for
   norms, not the single bar (U+2223) kets use. The result: kets
   rendered with double bars instead of single bars.

   Fix: fixKetPipes() converts \| to \vert when it is a ket opener
   (\|...\rangle) or bra closer (\langle...\|), while preserving
   matched \|...\| norm pairs. Disambiguation is by forward scan to
   the next \| or \rangle, with a backward scan for unmatched
   \langle to catch bra closers.

Tests: 121 passed, 0 failed (was 109; +12 new golden tests covering
column-spec preservation, ket/bra conversion, norm preservation, and
end-to-end KaTeX render of all three cases through the full pipeline).
@github-actions github-actions Bot added desktop Wails desktop app (desktop/**) v2 Go rewrite (1.x) — main-v2 branch, active development labels Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

desktop Wails desktop app (desktop/**) v2 Go rewrite (1.x) — main-v2 branch, active development

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants