#!/usr/bin/env python3 import json import re def fix_latex_escapes_in_file(file_path): """批量修复JSON文件中的LaTeX反斜杠转义问题""" try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() # 如果已经可以解析,就不需要修复 try: json.loads(content) print(f"✓ {file_path} - JSON格式已经正确") return True except json.JSONDecodeError: pass # 定义所有需要转义的LaTeX命令 latex_commands = [ 'frac', 'sqrt', 'sin', 'cos', 'tan', 'cot', 'sec', 'csc', 'arcsin', 'arccos', 'arctan', 'log', 'ln', 'lg', 'alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta', 'iota', 'kappa', 'lambda', 'mu', 'nu', 'xi', 'omicron', 'pi', 'rho', 'sigma', 'tau', 'upsilon', 'phi', 'chi', 'psi', 'omega', 'Gamma', 'Delta', 'Theta', 'Lambda', 'Xi', 'Pi', 'Sigma', 'Phi', 'Psi', 'Omega', 'mathbf', 'mathcal', 'mathbb', 'mathfrak', 'mathsf', 'mathrm', 'mathit', 'mathrm', 'mathsf', 'mathtt', 'vec', 'overrightarrow', 'overleftarrow', 'overline', 'underline', 'widehat', 'widetilde', 'bar', 'dot', 'ddot', 'acute', 'grave', 'breve', 'check', 'hat', 'tilde', 'left', 'right', 'begin', 'end', 'quad', 'qquad', 'hspace', 'vspace', 'noindent', 'centering', 'item', 'cdot', 'times', 'div', 'pm', 'mp', 'le', 'ge', 'ne', 'neq', 'approx', 'equiv', 'sim', 'simeq', 'cong', 'propto', 'parallel', 'perp', 'in', 'notin', 'subset', 'supset', 'subseteq', 'supseteq', 'cup', 'cap', 'setminus', 'emptyset', 'varnothing', 'infty', 'partial', 'nabla', 'exists', 'forall', 'neg', 'wedge', 'vee', 'land', 'lor', 'implies', 'iff', 'Rightarrow', 'Leftarrow', 'Leftrightarrow', 'mapsto', 'int', 'oint', 'sum', 'prod', 'coprod', 'bigcup', 'bigcap', 'bigvee', 'bigwedge', 'lim', 'limsup', 'liminf', 'max', 'min', 'sup', 'inf', 'arg', 'det', 'dim', 'ker', 'hom', 'Pr', 'exp', 'ln', 'log', 'lg', 'sinh', 'cosh', 'tanh', 'coth', 'arcsin', 'arccos', 'arctan', 'arcsinh', 'arccosh', 'arctanh', 'overbrace', 'underbrace', 'overline', 'underline', 'overrightarrow', 'overleftarrow', 'dots', 'cdots', 'vdots', 'ddots', 'ldots', 'text', 'mbox', 'mathrm', 'mathsf', 'mathtt', 'mathit', 'mathbf', 'mathcal', 'mathfrak', 'mathbb', 'mathscr', 'mathrsfs', 'mathsf', 'boldsymbol', 'boldmath', 'unboldmath', 'displaystyle', 'textstyle', 'scriptstyle', 'scriptscriptstyle', 'limits', 'nolimits', 'Big', 'big', 'Bigg', 'bigg', 'left', 'right', 'Bigl', 'bigl', 'Biggl', 'biggl', 'Bigr', 'bigr', 'Biggr', 'biggr', 'langle', 'rangle', 'lfloor', 'rfloor', 'lceil', 'rceil', 'vert', 'Vert', 'mid', 'parallel', 'backslash', 'slash', 'setminus', 'smallsetminus', 'complement', 'prime', 'circ', 'star', 'ast', 'dagger', 'ddagger', 'aleph', 'beth', 'gimel', 'daleth', 'ell', 'wp', 'Re', 'Im', 'angle', 'measuredangle', 'sphericalangle', 'top', 'bot', 'flat', 'natural', 'sharp', 'clubsuit', 'diamondsuit', 'heartsuit', 'spadesuit', 'checkmark', 'maltese', 'degree', 'celsius', 'percent', 'promille', 'pertenthousand', 'minus', 'plus', 'pm', 'mp', 'times', 'div', 'cdot', 'centerdot', 'ast', 'star', 'circ', 'bullet', 'cap', 'cup', 'uplus', 'sqcap', 'sqcup', 'vee', 'wedge', 'land', 'lor', 'setminus', 'smallsetminus', 'triangleleft', 'triangleright', 'diamond', 'bigtriangleup', 'bigtriangledown', 'oplus', 'ominus', 'otimes', 'oslash', 'odot', 'circledcirc', 'circleddash', 'box', 'boxbox', 'triangle', 'bigtriangle', 'triangleleft', 'triangleright', 'lhd', 'rhd', 'unlhd', 'unrhd', 'oplus', 'ominus', 'otimes', 'oslash', 'odot', 'circledcirc', 'circleddash', 'intercal', 'curlywedge', 'curlyvee', 'veebar', 'doublebarwedge', 'pitchfork', 'lessdot', 'gtrdot', 'cdot', 'ldots', 'cdots', 'vdots', 'ddots', 'bar', 'barwedge', 'veebar', 'doublebarwedge', 'bar', 'bar', 'overline', 'underline', 'widehat', 'widetilde', 'overleftarrow', 'overrightarrow', 'overbrace', 'underbrace', 'overline', 'underline', 'acute', 'grave', 'check', 'breve', 'acutemath', 'gravemath', 'checkmath', 'breveemath', 'hat', 'tilde', 'bar', 'dot', 'ddot', 'mathring', 'mathring', 'mathring', 'mathring', 'mathring', 'textstyle', 'displaystyle', 'scriptstyle', 'scriptscriptstyle', 'limits', 'nolimits', 'int', 'oint', 'iint', 'iiint', 'iiiint', 'idotsint', 'oint', 'oint', 'oint', 'oint', 'oint', 'sum', 'prod', 'coprod', 'bigcap', 'bigcup', 'bigsqcup', 'bigvee', 'bigwedge', 'bigoplus', 'bigotimes', 'bigodot', 'bigsqcap', 'bigsqcup', 'biguplus', 'bigvee', 'bigwedge', 'sum', 'prod', 'coprod' ] # 修复所有LaTeX命令 fixed_content = content for cmd in latex_commands: # 修复模式:\command -> \\command pattern = r'(?