11

I am trying to run the exact code from here to get an example of pylatex working.

In the directory that I am working in, I have copied and pasted from the link:

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

I consistently get the error:

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

You can see some of the things I've tried based on other people's suggestions: 1. if I just open a python console in this directory, and type:

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

there are no errors, implying importing was successful.

  1. I saw another recommendation that perl must be installed, which it is:

    localhost:test1$ perl --version : returns info about perl

  2. I've specified the compiler as was suggested elsewhere on StackOverflow: 'doc.generate_pdf(clean_tex=False,compiler='pdflatex')'

What else can I do? The ultimate aim is I have generated strings and an image with python, and I want to put them both into a PDF and format, in case there is a better alternative that anyone can suggest. P.s. I'm aware of the tex stack exchange, but I specifically need a way for latex to interact with python, which is why I asked here.

Slowat_Kela
  • 1,377
  • 2
  • 22
  • 60

3 Answers3

5

Apparantly you'll need to run these two apt-get dependencies

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

also install pdflatex with pip

pip install pdflatex

then use

doc.generate_pdf(compiler='pdflatex')

f1uk3r
  • 317
  • 7
  • 15
  • I could install `pdflatex` via pip, but I cannot install `latexmk`: I cannot do `apt-get install` as I need to run this without `sudo`. However, the error says _Either specify a LaTex compiler or make sure you have latexmk **or** pdfLaTex installed_. Is `latexmk` really needed? How can I work around this without `sudo`? – alelom Sep 12 '22 at 08:48
1

For me, on CentOS 8, I had to run:

pip install pylatex
sudo dnf install texlive
sudo dnf install texlive-lastpage

Then it worked for me.

jimh
  • 1,651
  • 2
  • 15
  • 28
0

I had this same error and my problem was that I had forgotten to add pdflatex (through MiKTex) to my environment path in Windows. My code worked after reloading my terminal. I have not installed Perl by the way.

Alexander Kvist
  • 559
  • 6
  • 12