{"id":818,"date":"2024-11-07T14:36:57","date_gmt":"2024-11-07T06:36:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/818\/"},"modified":"2024-11-07T14:36:57","modified_gmt":"2024-11-07T06:36:57","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-python-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e8%a7%a3%e6%9e%90-latex-%e5%a4%9a%e5%b1%82%e6%8b%ac%e5%8f%b7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/818\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 Python \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 LaTeX \u591a\u5c42\u62ec\u53f7\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1086\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172986418816623.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172986418816623.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172986418816623-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172986418816623-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172986418816623-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"\u5982\u4f55\u5229\u7528 Python \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 LaTeX \u591a\u5c42\u62ec\u53f7\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528 Python \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 LaTeX \u591a\u5c42\u62ec\u53f7\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528 python \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 latex \u591a\u5c42\u62ec\u53f7<\/strong><\/p>\n<p>\u5728\u5904\u7406 latex \u516c\u5f0f\u65f6\uff0c\u9700\u8981\u7ecf\u5e38\u89e3\u6790\u5d4c\u5957\u62ec\u53f7\u4ee5\u83b7\u53d6\u4e0d\u540c\u7684\u7ec4\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import re\n\n# \u76ee\u6807\u6587\u672c\nlatex_text = r\"int{rac{{d}x}{sqrt{x}}}\nint x^{2}{sqrt[3]{x}}{d}x\"\n\n# \u89e3\u6790\u591a\u5c42\u62ec\u53f7\n\u62ec\u53f7_\u6b63\u5219 = r\"{((\\{[^}]+\\})|.)+((\\{[^}]+\\})|.)+}\"\nmatches = re.findall(\u62ec\u53f7_\u6b63\u5219, latex_text)\n\n# \u6784\u5efa\u591a\u7ef4\u5b57\u5178\uff0c\u6df1\u5ea6\u4e3a\u62ec\u53f7\u6570\u91cf\n\u5b57\u5178 = {}\nfor match in matches:\n    depth = 0\n    \u5b50\u5b57\u5178 = \u5b57\u5178\n    for \u5b50\u7ec4 in match.split('{'):\n        if \u5b50\u7ec4.startswith('\\{'):\n            # \u5904\u7406\u8f6c\u4e49\u5de6\u82b1\u62ec\u53f7\n            depth += 1\n            if depth not in \u5b50\u5b57\u5178:\n                \u5b57\u5178[depth] = {}\n            \u5b50\u5b57\u5178 = \u5b57\u5178[depth]\n        else:\n            # \u5904\u7406\u975e\u8f6c\u4e49\u90e8\u5206\n            \u5b50\u5b57\u5178[\u6df1\u5ea6] = \u5b50\u7ec4.rstrip('}')\n            \u6df1\u5ea6 -= 1\n\n# \u8f93\u51fa\u7ed3\u679c\nprint(\u5b57\u5178)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u89e3\u6790\u7ed3\u679c\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<pre>{\n    1: {\n        1: \"d\",\n        2: \"x\",\n        3: \"sqrt{x}\"\n    },\n    2: {\n        1: \"x^2\",\n        2: \"sqrt[3]{x}\",\n        3: \"d\"\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528 Python \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 LaTeX \u591a\u5c42\u62ec\u53f7\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528 python \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 latex \u591a\u5c42\u62ec\u53f7 \u5728\u5904\u7406 latex \u516c\u5f0f\u65f6\uff0c\u9700\u8981\u7ecf\u5e38\u89e3\u6790\u5d4c\u5957\u62ec\u53f7\u4ee5\u83b7\u53d6\u4e0d\u540c\u7684\u7ec4\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u6b64\u95ee\u9898\u3002 \u5b9e\u73b0\u4ee3\u7801\uff1a import re # \u76ee\u6807\u6587\u672c latex_text = r&#8221;int{rac{{d}x}{sqrt{x}}} int x^{2}{sqrt[3]{x}}{d}x&#8221; # \u89e3\u6790\u591a\u5c42\u62ec\u53f7 \u62ec\u53f7_\u6b63\u5219 = r&#8221;{((\\{[^}]+\\})|.)+((\\{[^}]+\\})|.)+}&#8221; matches = re.findall(\u62ec\u53f7_\u6b63\u5219, latex_text) # \u6784\u5efa\u591a\u7ef4\u5b57\u5178\uff0c\u6df1\u5ea6\u4e3a\u62ec\u53f7\u6570\u91cf \u5b57\u5178 = {} for match in matches: depth = 0 \u5b50\u5b57\u5178 = \u5b57\u5178 for \u5b50\u7ec4 in match.split(&#8216;{&#8216;): if \u5b50\u7ec4.startswith(&#8216;\\{&#8216;): # \u5904\u7406\u8f6c\u4e49\u5de6\u82b1\u62ec\u53f7 depth += 1 if depth [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-818","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=818"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/818\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}