Why LLM-Generated Math Formulas Turn into Garbled Text: Causes and a Universal Fix
Math formulas generated by large language models often appear as garbled text when pasted into documents. This article explains why: mismatched formula delimiters, escaped characters lost in transit, invisible characters, clipboard formatting, and missing fonts. It provides a systematic troubleshooting workflow, a standard-library Python script for detecting and cleaning suspicious characters, and a universal processing pipeline for handling LLM-generated LaTeX reliably.
The Problem and Its Background
When writing technical documentation, course materials, or paper drafts, many people ask a large language model to generate content containing mathematical formulas. The model’s output is usually LaTeX source code—for example, inline formulas are typically wrapped in paired single dollar signs. When this content is pasted directly into office documents, three outcomes are common: the formula displays verbatim as a string of backslashes and curly braces; some symbols turn into question marks or empty boxes; or the formula renders correctly but cannot be edited further. These symptoms are collectively known as garbled formulas.
Garbled output is not a defect of any particular piece of software. It is the result of a mismatch among three things: the source format, the transmission process, and the target environment. Understanding this makes it possible to solve the problem with a general method, rather than trying different applications one by one.
Main Causes of Garbled Formulas
1. Mismatch Between Delimiters and the Rendering Environment
LLMs use several types of formula delimiters. Inline formulas commonly use paired single dollar signs, but parenthesized escape-style notation is also common. Display (standalone) formulas typically use double dollar signs or bracket notation. Not every rendering environment recognizes all of these styles. If the target environment only supports one of them, the others will be rendered as plain text.
2. Escape Characters Swallowed in Transit
LaTeX commands begin with a backslash—fractions, summations, Greek letters, and many other commands all depend on it. Markdown also uses the backslash as an escape character. When content passes through a Markdown rendering step before being copied, backslashes may be consumed. The result is that a command name loses its leading backslash and the formula breaks.
3. Character Encoding and Invisible Characters
Copying between web pages, chat interfaces, and documents often introduces invisible characters such as zero-width spaces and non-breaking spaces. These characters are invisible to the eye but break formula parsing. In addition, full-width parentheses and full-width dollar signs look similar to their half-width counterparts yet fail syntax recognition.
4. Formatting Information Carried by the Clipboard
When copying from a web page or rich-text editor, the clipboard usually holds multiple formats at once, including plain text and HTML. If the target application preferentially reads the HTML, it inherits the source page’s fonts and styles, and the formula’s superscript and subscript structure may be lost.
5. Missing Fonts and Symbols
Mathematical symbols depend on specific fonts. If the target machine lacks the required math fonts, symbols will be displayed using fallback fonts—or as empty boxes. This problem is especially common when documents move across platforms.
A Systematic Troubleshooting Workflow
When formulas appear garbled, troubleshoot in the following order instead of repeatedly regenerating the content:
- Confirm whether the content is still source code. Open the raw output in a plain-text editor and check whether the formulas remain in LaTeX form.
- Check for invisible characters and full-width symbols.
- Confirm that the delimiter style matches the target environment.
- Paste as plain text to rule out formatting interference.
- Check whether the target environment has the required fonts installed.
The following Python script helps with steps 2 and 3. It relies only on the standard library:
import reimport unicodedatadef find_suspicious_chars(text: str):"""Return the positions and names of format characters (Unicode category Cf)and non-breaking spaces in the text."""results = []for i, ch in enumerate(text):if unicodedata.category(ch) == "Cf" or ch == "\u00a0":results.append((i, f"U+{ord(ch):04X}", unicodedata.name(ch, "UNKNOWN")))return resultsdef normalize_formula_text(text: str) -> str:"""Perform conservative cleanup: remove zero-width characters, normalizespaces and full-width dollar signs, and rewrite parenthesized inlineformulas into dollar-sign form. Semantic characters inside formulasare not modified."""text = re.sub(r"[\u200b\u200c\u200d\ufeff]", "", text)text = text.replace("\u00a0", " ").replace("$", "$")text = unicodedata.normalize("NFC", text)text = re.sub(r"\\\((.+?)\\\)", r"$\1$", text, flags=re.S)return textif __name__ == "__main__":sample = "The energy formula is \\(E = mc^2\\), \u200bplease check."print(find_suspicious_chars(sample))print(normalize_formula_text(sample))
The script uses NFC rather than NFKC normalization to prevent compatibility-character mappings from changing the meaning of mathematical symbols.
A Universal Processing Pipeline
Step 1: Preserve the Source Code
When copying, preserve the LaTeX source rather than copying the rendered page view. When prompting the model, you can explicitly request a consistent delimiter format and ask it to avoid mixing multiple delimiter styles in a single response. This reduces the number of rules needed for subsequent processing.
Step 2: Normalize the Text
Before moving on, use the script above (or a similar tool) to clean up invisible characters and delimiters. The cleanup only changes formatting-level content; the structure of the formulas themselves should remain unchanged, so a manual review is still necessary afterward.
