代码之家  ›  专栏  ›  技术社区  ›  user4369032

生成TeX/LaTeX文件并在Python中编译

  •  4
  • user4369032  · 技术社区  · 10 年前

    我正在准备一个数据处理代码,这些代码的结果将被馈送到TeX/LaTeX文件,该文件将由Python编译(即Python应发送命令来编译TeX/LaTeX文件)

    目前,我计划为tex/LaTeX生成一个具有必要语法的文本文件(扩展名为.tex),然后使用os.system调用外部系统命令。有什么更简单的方法或模块可以做到这一点吗?

    3 回复  |  直到 10 年前
        1
  •  4
  •   Tom Kurushingal    10 年前

    不存在这样的Python模块,但您可以尝试使用扩展名为.tex的文本文件的所需语法生成文本文件,并通过使用Python运行系统命令来编译:

    import os
    os.system("pdflatex filename")
    
        2
  •  1
  •   JelteF    10 年前

    你可以使用PyLaTeX。这正是它应该用来做的事情之一。 https://github.com/JelteF/PyLaTeX

        3
  •  0
  •   hygull    7 年前

    确保已安装 Perl语言 MikTeX公司 (用于 latexmk pdflatex 编译器支持)。

    如果没有,您可以下载 Perl语言 从…起 https://www.perl.org/get.html

    MikTeX公司 从…起 https://miktex.org/download .

    也不要忘记检查 http://mg.readthedocs.io/latexmk.html#installation 因为它很好地指导了Latex编译器。

    我有 document.tex 具有以下内容。

    \documentclass{article}%
    \usepackage[T1]{fontenc}%
    \usepackage[utf8]{inputenc}%
    \usepackage{lmodern}%
    \usepackage{textcomp}%
    \usepackage{lastpage}%
    %
    %
    %
    \begin{document}%
    \normalsize%
    \section{A section}%
    \label{sec:A section}%
    Some regular text and some %
    \textit{italic text. }%
    \subsection{A subsection}%
    \label{subsec:A subsection}%
    Also some crazy characters: \$\&\#\{\}
    
    %
    \end{document}
    

    最后,创建任何python文件并粘贴以下任意一个代码并运行。

    第1路

    # 1st way
    import os
    
    tex_file_name = "document.tex";
    os.system("latexmk " + tex_file_name + " -pdf");
    

    第二种方式

    # 2nd way
    import os
    
    tex_file_name = "document.tex";
    os.system("pdflatex " + tex_file_name);
    

    对于编译复杂的乳胶文件,您需要查找传递给 晚xmk pdflatex公司 命令。

    谢谢