DEV Community

Cover image for Getting maths out of ChatGPT and into Word without it turning into backslashes
NAN
NAN

Posted on

Getting maths out of ChatGPT and into Word without it turning into backslashes

Ask ChatGPT to derive something. You get a clean equation on screen. Copy it into a document and you get one of three outcomes:

\frac{-b \pm \sqrt{b^2-4ac}}{2a}
Enter fullscreen mode Exit fullscreen mode

…or a jumble of characters that looks like the equation went through a blender…

…or, occasionally, a picture.

None of these is what you wanted, and which one you get is close to arbitrary. The reason is that "the equation on screen" is not one thing. It is several representations layered on top of each other, and copying picks one more or less by accident.

What is actually sitting there

The model emits LaTeX. A renderer — KaTeX and MathJax are the common ones — turns that string into something a browser can paint.

The important part is what the renderer leaves behind. KaTeX output looks roughly like this:

<span class="katex">
  <span class="katex-mathml">
    <math><semantics>
      <mfrac></mfrac>
      <annotation encoding="application/x-tex">\frac{a}{b}</annotation>
    </semantics></math>
  </span>
  <span class="katex-html" aria-hidden="true">
    <!-- dozens of absolutely-positioned spans -->
  </span>
</span>
Enter fullscreen mode Exit fullscreen mode

Three representations, all present at once:

  1. MathML — semantic. <mfrac> means "this is a fraction". Machines understand it.
  2. The original LaTeX, tucked inside <annotation>.
  3. Visual spans — the thing you actually see, built from positioned glyphs.

That third one is marked aria-hidden="true" because it is meaningless to a screen reader. It is a drawing.

Now the three bad outcomes make sense. Grab the visual layer and you get the blender output — those positioned glyphs in document order, which is not reading order. Grab the annotation and you get raw LaTeX. Only the MathML carries the structure, and it is invisible, so nothing grabs it by accident.

Why Word cannot just take MathML

Word does not store equations as MathML. It uses OMML — Office Math Markup Language, its own XML dialect living inside the .docx package.

So a faithful conversion is a chain:

LaTeX  →  MathML  →  OMML  →  equation object in Word
Enter fullscreen mode Exit fullscreen mode

Word can do the middle step itself when you paste MathML, though which paste path triggers it is inconsistent across versions and platforms. Doing the conversion before the document is written is more predictable.

Google Docs is a separate problem again: its equation support is thinner, so the realistic target there is a rendered image or plain text, not an editable object.

That difference is worth knowing before you pick a destination. If you need to edit the maths later, Word is the format that can hold it.

The streaming trap

There is a timing failure that catches people and looks like a tool bug.

ChatGPT streams tokens. The LaTeX source appears in the DOM first, and the renderer runs a beat later. Copy during that window and you copy the source, because the rendered form does not exist yet.

This is why "it exported as backslashes that one time" is usually not random. Let the answer finish rendering, then take it.

What each route actually gives you

Ctrl+C into Word. Unpredictable. Depends on what your selection covered and which clipboard flavour Word asked for. Sometimes fine, often not.

Copy the LaTeX deliberately. If your destination speaks LaTeX — Obsidian, Notion, a repo, Overleaf — this is the best outcome, not a failure. Many renderers expose a copy-as-LaTeX affordance for exactly this.

Screenshot. Always works, always dead. Fine for a slide, useless in anything you will edit or search.

A converter that walks the chain. Reads the MathML that is already in the page and writes OMML into the .docx directly.

Disclosure

I wrote one of those, so weigh this accordingly.

Chat Exporter for ChatGPT exports a conversation to Word, PDF, Google Docs, Notion, Markdown or JSON. The maths path is the LaTeX → MathML → OMML chain above, so equations land in Word as real equation objects you can click into, rather than images or source strings. Code blocks keep their language label, and Markdown tables come out as actual tables. Free tier, paid for unlimited.

Its limits follow from the same mechanics. Export mid-stream and you get the pre-render state, same as copying mid-stream. Google Docs still cannot hold an editable equation, because that is a Docs constraint and no extension changes it. And images generated inside ChatGPT sit on short-lived URLs — on an old conversation those may already have expired at OpenAI's end, in which case there is nothing left to fetch.

The rule worth remembering

Rendered maths is a drawing next to a description. Copy tends to take the drawing. Anything that produces an editable equation is reading the description instead — the MathML that was sitting there the whole time, hidden behind aria-hidden.

When an export gives you backslashes, it read the annotation. When it gives you gibberish, it read the drawing. Neither is broken; both grabbed the wrong layer.


If you work with maths-heavy notes, which destination have you settled on? I have not found anything that beats Word for editability, and I would be glad to be wrong.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.