{"id":204058,"date":"2025-05-29T11:34:07","date_gmt":"2025-05-29T03:34:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204058\/"},"modified":"2025-05-29T11:34:07","modified_gmt":"2025-05-29T03:34:07","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:\/\/server.hk\/cnblog\/204058\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 Python \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 LaTeX \u591a\u5c42\u62ec\u53f7\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5229\u7528 Python \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 LaTeX \u591a\u5c42\u62ec\u53f7\uff1f<\/h1>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729916141671c6ced9d583.jpg\" class=\"aligncenter\"><\/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{\\frac{{d}x}{\\sqrt{x}}}\\n\\int 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><strong>\u89e3\u6790\u7ed3\u679c\u793a\u4f8b\uff1a<\/strong><\/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>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528 Python \u6b63\u5219\u8868\u8fbe&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204058","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204058"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204058\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}