首页 > 解决方案 > PyLaTeX:pylatex.errors.CompilerError:未找到 LaTex 编译器

问题描述

我正在尝试从此处运行确切的代码以获取 pylatex 工作的示例。

在我正在工作的目录中,我从链接中复制并粘贴了:

from pylatex import Document, Section, Subsection, Command
from pylatex.utils import italic, NoEscape
import pdflatex


def fill_document(doc):
    """Add a section, a subsection and some text to the document.

    :param doc: the document
    :type doc: :class:`pylatex.document.Document` instance
    """
    with doc.create(Section('A section')):
        doc.append('Some regular text and some ')
        doc.append(italic('italic text. '))

        with doc.create(Subsection('A subsection')):
            doc.append('Also some crazy characters: $&#{}')


if __name__ == '__main__':
    # Basic document
    doc = Document('basic')
    fill_document(doc)

    doc.generate_pdf(clean_tex=False,compiler='pdflatex')
    doc.generate_tex()

    # Document with `\maketitle` command activated
    doc = Document()

    doc.preamble.append(Command('title', 'Awesome Title'))
    doc.preamble.append(Command('author', 'Anonymous author'))
    doc.preamble.append(Command('date', NoEscape(r'\today')))
    doc.append(NoEscape(r'\maketitle'))

    fill_document(doc)

    doc.generate_pdf('basic_maketitle', clean_tex=False)

    # Add stuff to the document
    with doc.create(Section('A second section')):
        doc.append('Some text.')

    doc.generate_pdf('basic_maketitle2', clean_tex=False)
    tex = doc.dumps()  # The document as string in LaTeX syntax

我一直收到错误:

Traceback (most recent call last):
  File "test.py", line 26, in <module>
    doc.generate_pdf(clean_tex=False,compiler='pdflatex')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.7/site-packages/pylatex/document.py", line 280, in generate_pdf
    'Either specify a LaTex compiler ' +
pylatex.errors.CompilerError: No LaTex compiler was found

您可以看到我根据其他人的建议尝试过的一些事情: 1. 如果我只是在此目录中打开一个 python 控制台,然后键入:

from pylatex import Document, Section, Subsection, Command
from pylatex.utils import italic, NoEscape
import pdflatex

没有错误,说明导入成功。

  1. 我看到另一个必须安装 perl 的建议,它是:

    localhost:test1$ perl --version : 返回有关 perl 的信息

  2. 我已经按照 StackOverflow 其他地方的建议指定了编译器:'doc.generate_pdf(clean_tex=False,compiler='pdflatex')'

我还可以做些什么?最终目标是我用 python 生成了字符串和图像,我想把它们都变成 PDF 和格式,以防任何人都可以提出更好的选择。Ps 我知道tex stack exchange,但我特别需要一种让latex与python交互的方法,这就是我在这里问的原因。

标签: pythonlatexpylatex

解决方案


显然你需要运行这两个 apt-get 依赖项

sudo apt-get install latexmk
sudo apt-get install -y texlive-latex-extra

也可以pdflatex用 pip安装

pip install pdflatex

然后使用

doc.generate_pdf(compiler='pdflatex')


推荐阅读