DEV Community

Cover image for Why ChatGPT's LaTeX breaks in Obsidian, and why find-and-replace makes it worse
Artyom Shimanski
Artyom Shimanski

Posted on

Why ChatGPT's LaTeX breaks in Obsidian, and why find-and-replace makes it worse

You ask ChatGPT to explain something with a formula, copy the answer into Obsidian, and get this:

The kinetic energy is \(E_k = \tfrac{1}{2}mv^2\), so at 25 m/s:

\[
E_k = \tfrac{1}{2} \cdot 1200 \cdot 25^2 = 375\,000\ \text{J}
\]
Enter fullscreen mode Exit fullscreen mode

No formulas, just backslashes and brackets. The TeX is fine. The delimiters are the problem.

The same ChatGPT answer pasted as is, and with its delimiters converted

Short answer: as of November 2026, ChatGPT writes LaTeX between \( \) and \[ \], and Obsidian only renders $…$ and $$…$$. Converting one to the other is easy to get wrong. Skip code and links, replace with a function rather than a string, and trim the space inside inline formulas. Details and tested code below.

Two dialects of the same math

ChatGPT writes inline math between \( and \), and display math between \[ and \]. That's standard LaTeX, and chatgpt.com renders it. The copy button under an answer gives you exactly that text.

Obsidian renders math only between $…$ (inline) and $$…$$ (display). So do GitHub and Typora. Paste ChatGPT's version and nothing renders.

So the fix is four replacements. Right?

The obvious fix, and five ways it breaks

const naive = (s) => s
  .replace(/\\\(/g, '$').replace(/\\\)/g, '$')
  .replace(/\\\[/g, '$$').replace(/\\\]/g, '$$');
Enter fullscreen mode Exit fullscreen mode

I ran it on lines that real answers contain. Here's what came out:

Input Naive output Problem
\[ a^2 + b^2 = c^2 \] $ a^2 + b^2 = c^2 $ Display math became inline
Run `find . \( -name "*.md" \)` Run `find . $ -name "*.md" $` Broke a shell command in code
[RFC \[1\]](https://example.com/rfc1) [RFC $1$](https://…) Turned a citation into math
See \[see above\] for that. See $see above$ for that. Turned words into math
Spaces \( x \) padded. Spaces $ x $ padded. Obsidian won't draw it

The first row is my favourite. In JavaScript, '$$' in a replacement string is a pattern that means "a literal $". So .replace(/\\\[/g, '$$') inserts one dollar sign, not two, and every display formula silently becomes inline. Pass a function instead of a string and the problem goes away, because a function's return value is used as is.

The last row is about Obsidian, not JavaScript. Like Pandoc, Obsidian only treats $…$ as math when there's no space just inside the dollars. That rule is also why It costs $5 and $10 doesn't turn into a formula. ChatGPT sometimes pads its formulas (\( x \)), so the spaces have to go.

A version that survives real answers

Three rules fix every row above:

  1. Never touch code or URLs. Split them out first, convert the rest, join it back.
  2. \[ … \] is display math only if it holds TeX. \[1\] and \[see above\] are escaped brackets, not formulas. A backslash, ^, _, = or a brace is a good sign of TeX.
  3. Trim inline formulas, so Obsidian draws them.
function fixMath(md) {
  // Odd parts are code blocks, inline code and URLs: never touched.
  const parts = md.split(/(`{3}[\s\S]*?`{3}|`[^`\n]*`|https?:\/\/\S+)/);
  return parts.map((part, i) => i % 2 ? part : part
    // \[ … \] is display math only if it holds TeX, not "\[see above\]" or "\[1\]".
    .replace(/\\\[([\s\S]+?)\\\]/g, (m, tex) => /[\\^_={}]/.test(tex) ? `$$${tex}$$` : m)
    // \( … \): Obsidian won't draw "$ x $", so trim the inside.
    .replace(/\\\(([\s\S]+?)\\\)/g, (m, tex) => `$${tex.trim()}$`)
  ).join('');
}
Enter fullscreen mode Exit fullscreen mode

On the same lines:

Input fixMath output
\[ a^2 + b^2 = c^2 \] $$ a^2 + b^2 = c^2 $$
Run `find . \( -name "*.md" \)` unchanged
[RFC \[1\]](https://example.com/rfc1) unchanged
See \[see above\] for that. unchanged
https://example.com/a\(b\) unchanged
Spaces \( x \) padded. Spaces $x$ padded.
Set \(\{1,2\}\) and \(x_1\). Set $\{1,2\}$ and $x_1$.

It's still a heuristic. A link whose text holds a real formula would trip it, and so would \( inside an HTML block. For answers you paste one at a time, it holds up.

The other way in: copying from the page

Maybe you don't use the copy button, and select part of the answer on the page with Cmd+C or Ctrl+C instead. Now the clipboard holds rich text, and the formulas in it are already drawn.

ChatGPT, Claude, Perplexity, DeepSeek and Grok draw formulas with KaTeX. KaTeX renders every formula twice: HTML for your eyes, and MathML for screen readers. The MathML carries the original TeX in an annotation:

<span class="katex">
  <math>
    <semantics>
      <mrow><mi>E</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow>
      <annotation encoding="application/x-tex">E = mc^2</annotation>
    </semantics>
  </math>
  <!-- …the HTML version… -->
</span>
Enter fullscreen mode Exit fullscreen mode

So a paste handler can read the TeX back:

editor.addEventListener('paste', (e) => {
  const html = e.clipboardData.getData('text/html');
  if (!html) return; // plain text: use fixMath() instead
  const doc = new DOMParser().parseFromString(html, 'text/html');
  for (const k of doc.querySelectorAll('.katex')) {
    const tex = k.querySelector('annotation[encoding="application/x-tex"]')?.textContent;
    if (tex) k.replaceWith(`$${tex.trim()}$`); // a .katex inside .katex-display is a $$ block
  }
  // …then turn the rest of doc.body into Markdown
});
Enter fullscreen mode Exit fullscreen mode

There's a catch. In my tests, Chrome kept the MathML when copying a selection but dropped the <annotation>. What you get is <mi>E</mi><mo>=</mo>… and no TeX at all. To support Chrome, you have to turn MathML back into TeX: <mfrac> into \frac{}{}, <msup> into ^{}, <mi mathvariant="normal"> letters into \mathrm{} or \sin, and so on. Gemini is easier: it keeps the TeX in a data-math attribute on the formula.

If you'd rather not maintain this

There are a few ways out:

  • An Obsidian plugin. Several community plugins convert \( \) and \[ \] on paste, or render them as they are. Search for "LaTeX delimiter" in Community plugins.
  • Ask ChatGPT to use dollars. It works for a while, then drifts back.
  • A paste-and-copy page. I built a free ChatGPT LaTeX fixer that does all of the above in the browser: both the copy-button text and copied rich text (including Chrome's MathML), with code, links and escaped brackets left alone. Nothing is sent anywhere.

The ChatGPT LaTeX fixer: an answer pasted on the left, Markdown with dollar signs on the right

The same rules run inside Chat to Markdown, the extension I'm building that saves whole ChatGPT and Claude chats into an Obsidian vault, where every formula has to come out right without anyone checking it.

The short version

  • ChatGPT: \( \) and \[ \]. Obsidian, GitHub, Typora: $ $ and $$ $$.
  • Don't use '$$' as a replacement string in JavaScript. Use a function.
  • Skip code, inline code and URLs. Only treat \[ … \] as math when it holds TeX. Trim inline formulas.
  • From a copied page, read KaTeX's <annotation>, and in Chrome, rebuild TeX from MathML.

Top comments (0)