{"record":{"id":"a5a4855a85c87ef1","repo":"3b1b/manim","slug":"missing-inserted","errorCode":null,"errorMessage":"Missing '{' inserted","messagePattern":"Missing '\\{' inserted","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"manimlib/mobject/svg/tex_mobject.py","lineNumber":108,"sourceCode":"\n    @staticmethod\n    def get_command_matches(string: str) -> list[re.Match]:\n        # Lump together adjacent brace pairs\n        pattern = re.compile(r\"\"\"\n            (?P<command>\\\\(?:[a-zA-Z]+|.))\n            |(?P<open>{+)\n            |(?P<close>}+)\n        \"\"\", flags=re.X | re.S)\n        result = []\n        open_stack = []\n        for match_obj in pattern.finditer(string):\n            if match_obj.group(\"open\"):\n                open_stack.append((match_obj.span(), len(result)))\n            elif match_obj.group(\"close\"):\n                close_start, close_end = match_obj.span()\n                while True:\n                    if not open_stack:\n                        raise ValueError(\"Missing '{' inserted\")\n                    (open_start, open_end), index = open_stack.pop()\n                    n = min(open_end - open_start, close_end - close_start)\n                    result.insert(index, pattern.fullmatch(\n                        string, pos=open_end - n, endpos=open_end\n                    ))\n                    result.append(pattern.fullmatch(\n                        string, pos=close_start, endpos=close_start + n\n                    ))\n                    close_start += n\n                    if close_start < close_end:\n                        continue\n                    open_end -= n\n                    if open_start < open_end:\n                        open_stack.append(((open_start, open_end), index))\n                    break\n            else:\n                result.append(match_obj)\n        if open_stack:","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/3b1b/manim/blob/dee01804d47b9f94402d71472674710dcac125b8/manimlib/mobject/svg/tex_mobject.py#L90-L126","documentation":"Raised by MathTex/Tex string processing (tex_mobject.py:108, break_up_by_substrings brace matching) when the LaTeX string contains more closing braces than unmatched opening braces — i.e. a '}' appears with nothing on the open-brace stack to pair with. The tokenizer pairs brace groups so substrings like \\frac{...}{...} can be isolated, and unbalanced input is rejected.","triggerScenarios":"MathTex(r'{a}') is fine; MathTex(r'}') or MathTex(r'\\frac{a}}') raises 'Missing {'. Also occurs when isolating substrings with braces whose escaping (\\\\{) is lost through f-strings or .format so a literal } slips in unbalanced.","commonSituations":"Building Tex strings dynamically from templates where a variable can inject a bare '}'; using double-brace escaping incorrectly in f-strings; upgrading from older manim versions that silently tolerated unbalanced braces.","solutions":["Balance the braces in the tex string; escape literal braces as \\\\{ and \\\\} in math mode when they should not group","When using f-strings/format, remember braces for LaTeX must be doubled ({{ }}) or the string built by concatenation instead","Add a quick sanity check that string.count('{') == string.count('}') after template rendering (ignoring escaped ones)"],"exampleFix":"# before\nMathTex(r'\\frac{a}{b}}')  # extra '}' -> raises\n\n# after\nMathTex(r'\\frac{a}{b}')","handlingStrategy":"validation","validationCode":"def braces_balanced(s: str) -> bool:\n    depth = 0\n    for ch in s:\n        if ch == '{': depth += 1\n        elif ch == '}': depth -= 1\n        if depth < 0: return False\n    return depth == 0\n\nassert braces_balanced(tex_str)","typeGuard":null,"tryCatchPattern":"from manimlib.mobject.svg.tex_mobject import Tex\ntry:\n    tex = Tex(rendered_str)\nexcept ValueError as e:\n    if \"Missing '{'\" in str(e):\n        tex = Tex(rendered_str.replace('}', r'\\}'))","preventionTips":["Escape literal braces (\\\\{ \\\\}) in math strings","Double braces ({{ }}) inside f-strings that produce LaTeX","Assert brace balance after template rendering, before constructing MathTex"],"tags":["manim","latex","mathtex","parsing","braces"],"backgroundTag":null,"analyzedSha":"dee01804d47b9f94402d71472674710dcac125b8","analyzedAt":"2026-08-14T19:53:44.241Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}