{"id":935038,"date":"2024-12-26T18:44:50","date_gmt":"2024-12-26T10:44:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/935038.html"},"modified":"2024-12-26T18:44:51","modified_gmt":"2024-12-26T10:44:51","slug":"python%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85-latex","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/935038.html","title":{"rendered":"python\u5982\u4f55\u5b89\u88c5 latex"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071815\/b43699b6-8fc9-442d-8cc0-a91ab12482ba.webp\" alt=\"python\u5982\u4f55\u5b89\u88c5 latex\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b89\u88c5LaTeX\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u5de5\u5177\u548c\u5e93\uff0c\u5982Texlive\u3001MikTeX\u7b49\u3001\u4ee5\u53caPython\u5e93\u5982 pylatex\u3001latexmk\u7b49\u3002\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528MikTeX\uff0c\u800cLinux\u548cMacOS\u7528\u6237\u901a\u5e38\u4f7f\u7528Texlive\u3002\u5b89\u88c5\u8fc7\u7a0b\u5305\u62ec\u4e0b\u8f7d\u548c\u914d\u7f6eLaTeX\u53d1\u884c\u7248\u3001\u5b89\u88c5\u76f8\u5173Python\u5e93\u3001\u4ee5\u53ca\u786e\u4fdd\u73af\u5883\u53d8\u91cf\u6b63\u786e\u8bbe\u7f6e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u8bf4\u660e\u5176\u4e2d\u7684\u4e00\u4e2a\u6b65\u9aa4\uff0c\u6211\u4eec\u4ee5Windows\u7cfb\u7edf\u4e0b\u5b89\u88c5MikTeX\u4e3a\u4f8b\uff1a\u9996\u5148\uff0c\u8bbf\u95eeMikTeX\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u6700\u65b0\u7684\u5b89\u88c5\u7a0b\u5e8f\uff1b\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u9009\u62e9\u201cInstall missing packages on-the-fly\u201d\u9009\u9879\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u65f6\u81ea\u52a8\u4e0b\u8f7d\u7f3a\u5931\u7684\u5305\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u8fd0\u884c<code>latex<\/code>\u547d\u4ee4\uff0c\u786e\u4fddMikTeX\u5df2\u6b63\u786e\u5b89\u88c5\u5e76\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b89\u88c5LaTeX\u7684\u65b9\u6cd5\uff0c\u5e76\u8bf4\u660e\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528LaTeX\u751f\u6210\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001WINDOWS\u7cfb\u7edf\u4e0b\u5b89\u88c5LATEX<\/h2>\n<\/p>\n<p><h3>1. MikTeX\u7684\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>MikTeX\u662f\u4e00\u6b3e\u6d41\u884c\u7684LaTeX\u53d1\u884c\u7248\uff0c\u9002\u7528\u4e8eWindows\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u8bbf\u95eeMikTeX\u7684\u5b98\u65b9\u7f51\u7ad9\uff0c\u4e0b\u8f7d\u9002\u7528\u4e8eWindows\u7684\u5b89\u88c5\u7a0b\u5e8f\u3002<\/li>\n<li>\u8fd0\u884c\u5b89\u88c5\u7a0b\u5e8f\uff0c\u9009\u62e9\u201cNext\u201d\u6309\u94ae\uff0c\u76f4\u81f3\u5230\u8fbe\u201cInstall missing packages on-the-fly\u201d\u9009\u9879\uff0c\u786e\u4fdd\u52fe\u9009\u6b64\u9009\u9879\u3002<\/li>\n<li>\u7ee7\u7eed\u5b89\u88c5\uff0c\u76f4\u5230\u5b8c\u6210\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>latex<\/code>\uff0c\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/ul>\n<p><h3>2. \u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u786e\u4fddMikTeX\u7684\u5b89\u88c5\u76ee\u5f55\u5df2\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u201c\u63a7\u5236\u9762\u677f\u201d\uff0c\u9009\u62e9\u201c\u7cfb\u7edf\u548c\u5b89\u5168\u201d\u3002<\/li>\n<li>\u9009\u62e9\u201c\u7cfb\u7edf\u201d\uff0c\u7136\u540e\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u4e2d\uff0c\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u4e0b\uff0c\u627e\u5230\u5e76\u7f16\u8f91\u201cPath\u201d\u53d8\u91cf\uff0c\u6dfb\u52a0MikTeX\u7684\u5b89\u88c5\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001LINUX\u7cfb\u7edf\u4e0b\u5b89\u88c5LATEX<\/h2>\n<\/p>\n<p><h3>1. Texlive\u7684\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eLinux\u7528\u6237\uff0cTexlive\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u7ec8\u7aef\uff0c\u66f4\u65b0\u5305\u7ba1\u7406\u5668\u5217\u8868\uff1a<code>sudo apt update<\/code>\u3002<\/li>\n<li>\u5b89\u88c5Texlive\uff1a<code>sudo apt install texlive-full<\/code>\u3002<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8f93\u5165<code>latex<\/code>\u547d\u4ee4\uff0c\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/ul>\n<p><h3>2. \u914d\u7f6e\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cTexlive\u4f1a\u81ea\u52a8\u914d\u7f6e\u8def\u5f84\u3002\u5982\u679c\u9700\u8981\u624b\u52a8\u914d\u7f6e\uff0c\u53ef\u4ee5\u7f16\u8f91<code>.bashrc<\/code>\u6216<code>.bash_profile<\/code>\u6587\u4ef6\uff0c\u6dfb\u52a0Texlive\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001MACOS\u7cfb\u7edf\u4e0b\u5b89\u88c5LATEX<\/h2>\n<\/p>\n<p><h3>1. MacTeX\u7684\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>Mac\u7528\u6237\u53ef\u4ee5\u4f7f\u7528MacTeX\uff0c\u8fd9\u662f\u4e13\u4e3aMacOS\u8bbe\u8ba1\u7684LaTeX\u53d1\u884c\u7248\u3002<\/p>\n<\/p>\n<ul>\n<li>\u8bbf\u95eeMacTeX\u5b98\u65b9\u7f51\u7ad9\uff0c\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684\u5b89\u88c5\u7a0b\u5e8f\u3002<\/li>\n<li>\u8fd0\u884c\u5b89\u88c5\u7a0b\u5e8f\uff0c\u6839\u636e\u63d0\u793a\u5b8c\u6210\u5b89\u88c5\u3002<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165<code>latex<\/code>\u547d\u4ee4\uff0c\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/ul>\n<p><h3>2. \u914d\u7f6e\u73af\u5883<\/h3>\n<\/p>\n<p><p>MacTeX\u901a\u5e38\u4f1a\u81ea\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u5982\u679c\u9700\u8981\u624b\u52a8\u914d\u7f6e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91<code>.bash_profile<\/code>\u6587\u4ef6\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5b89\u88c5Python\u5e93\u4ee5\u652f\u6301LATEX<\/h2>\n<\/p>\n<p><h3>1. \u4f7f\u7528pylatex\u751f\u6210\u6587\u6863<\/h3>\n<\/p>\n<p><p>pylatex\u662f\u4e00\u4e2aPython\u5e93\uff0c\u5141\u8bb8\u7528\u6237\u5728Python\u4e2d\u751f\u6210LaTeX\u6587\u6863\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5pylatex\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>pip install pylatex<\/code>\u3002<\/li>\n<li>\u4f7f\u7528pylatex\u751f\u6210\u7b80\u5355\u7684LaTeX\u6587\u6863\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">from pylatex import Document, Section, Subsection, Command<\/p>\n<p>from pylatex.utils import italic, NoEscape<\/p>\n<p>doc = Document()<\/p>\n<p>doc.preamble.append(Command(&#39;title&#39;, &#39;Simple LaTeX Document&#39;))<\/p>\n<p>doc.preamble.append(Command(&#39;author&#39;, &#39;John Doe&#39;))<\/p>\n<p>doc.preamble.append(Command(&#39;date&#39;, NoEscape(r&#39;\\today&#39;)))<\/p>\n<p>doc.append(NoEscape(r&#39;\\maketitle&#39;))<\/p>\n<p>with doc.create(Section(&#39;Introduction&#39;)):<\/p>\n<p>    doc.append(&#39;This is a simple introduction.&#39;)<\/p>\n<p>doc.generate_pdf(&#39;simple_doc&#39;, clean_tex=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528latexmk\u81ea\u52a8\u7f16\u8bd1<\/h3>\n<\/p>\n<p><p>latexmk\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u68c0\u6d4bLaTeX\u6587\u4ef6\u7684\u4f9d\u8d56\u9879\u5e76\u7f16\u8bd1\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5latexmk\uff1a<code>sudo apt install latexmk<\/code>\uff08Linux\uff09\u6216\u901a\u8fc7MacTeX\u5b89\u88c5\uff08Mac\uff09\u3002<\/li>\n<li>\u4f7f\u7528latexmk\u7f16\u8bd1LaTeX\u6587\u4ef6\uff1a<code>latexmk -pdf document.tex<\/code>\u3002<\/li>\n<\/ul>\n<p><h2>\u4e94\u3001\u5904\u7406\u5e38\u89c1\u95ee\u9898<\/h2>\n<\/p>\n<p><h3>1. \u7f3a\u5c11\u5305\u7684\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u8bd1LaTeX\u6587\u6863\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f3a\u5c11\u5305\u7684\u9519\u8bef\u3002\u5bf9\u4e8eMikTeX\uff0c\u786e\u4fdd\u5df2\u542f\u7528\u201cInstall missing packages on-the-fly\u201d\u9009\u9879\u3002\u5bf9\u4e8e\u5176\u4ed6\u53d1\u884c\u7248\uff0c\u624b\u52a8\u5b89\u88c5\u7f3a\u5c11\u7684\u5305\u3002<\/p>\n<\/p>\n<p><h3>2. \u73af\u5883\u53d8\u91cf\u672a\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5728\u547d\u4ee4\u884c\u4e2d\u65e0\u6cd5\u8bc6\u522b<code>latex<\/code>\u547d\u4ee4\uff0c\u68c0\u67e5\u73af\u5883\u53d8\u91cf\u662f\u5426\u6b63\u786e\u914d\u7f6e\u3002\u786e\u4fddLaTeX\u7684\u5b89\u88c5\u76ee\u5f55\u5df2\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>3. \u6587\u6863\u751f\u6210\u5931\u8d25<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f7f\u7528pylatex\u751f\u6210\u6587\u6863\u5931\u8d25\uff0c\u68c0\u67e5LaTeX\u6a21\u677f\u662f\u5426\u6b63\u786e\uff0c\u5e76\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u9879\u5df2\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b89\u88c5LaTeX\uff0c\u5e76\u901a\u8fc7Python\u5e93\u751f\u6210LaTeX\u6587\u6863\u3002\u65e0\u8bba\u60a8\u662f\u9700\u8981\u7f16\u5199\u590d\u6742\u7684\u5b66\u672f\u8bba\u6587\uff0c\u8fd8\u662f\u7b80\u5355\u7684\u6587\u6863\uff0c\u8fd9\u4e9b\u5de5\u5177\u90fd\u80fd\u5e2e\u52a9\u60a8\u9ad8\u6548\u5730\u5b9e\u73b0\u76ee\u6807\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u4f7f\u7528LaTeX\u9700\u8981\u54ea\u4e9b\u6b65\u9aa4\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528LaTeX\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5LaTeX\u53d1\u884c\u7248\uff0c\u5982TeX Live\u6216MiKTeX\u3002\u8fd9\u4e9b\u53d1\u884c\u7248\u63d0\u4f9b\u4e86\u5fc5\u8981\u7684\u5de5\u5177\u548c\u5b8f\u5305\u6765\u5904\u7406LaTeX\u6587\u6863\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5e93\u5982<code>matplotlib<\/code>\u6216<code>Pylatex<\/code>\u6765\u751f\u6210\u548c\u6e32\u67d3LaTeX\u4ee3\u7801\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u63a8\u8350\u7684LaTeX\u7f16\u8f91\u5668\u4e0ePython\u7ed3\u5408\u4f7f\u7528\uff1f<\/strong><br \/>\u8bb8\u591a\u7528\u6237\u559c\u6b22\u4f7f\u7528VS Code\u6216PyCharm\u4f5c\u4e3aLaTeX\u7f16\u8f91\u5668\uff0c\u8fd9\u4e24\u4e2a\u73af\u5883\u90fd\u652f\u6301Python\u5f00\u53d1\u3002\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u76f8\u5e94\u7684\u63d2\u4ef6\u6765\u589e\u5f3aLaTeX\u548cPython\u7684\u96c6\u6210\u4f53\u9a8c\uff0c\u65b9\u4fbf\u8fdb\u884c\u6587\u6863\u7684\u7f16\u8f91\u548c\u4ee3\u7801\u7684\u8c03\u8bd5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406LaTeX\u6587\u6863\u7684\u7f16\u8bd1\u95ee\u9898\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5904\u7406LaTeX\u6587\u6863\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f16\u8bd1\u5931\u8d25\u7684\u60c5\u51b5\u3002\u786e\u4fdd\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u4e86LaTeX\u53d1\u884c\u7248\uff0c\u5e76\u4e14\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528\u6b63\u786e\u7684\u547d\u4ee4\u6765\u8c03\u7528LaTeX\u7f16\u8bd1\u5668\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u9519\u8bef\u65e5\u5fd7\u6765\u8bca\u65ad\u95ee\u9898\uff0c\u786e\u4fdd\u6240\u6709\u9700\u8981\u7684\u5b8f\u5305\u90fd\u5df2\u5b89\u88c5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b89\u88c5LaTeX\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u5de5\u5177\u548c\u5e93\uff0c\u5982Texlive\u3001MikTeX\u7b49\u3001\u4ee5\u53caPython\u5e93\u5982 [&hellip;]","protected":false},"author":3,"featured_media":935041,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935038"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=935038"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935038\/revisions"}],"predecessor-version":[{"id":935042,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935038\/revisions\/935042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/935041"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=935038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=935038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=935038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}