2

I wanted to use pylatex and tried to generate the example file. I have installed the packages with anaconda (pylatex and latexmk with conda-forge) and having following code:

from pylatex import Document, PageStyle, Head, MiniPage, Foot, LargeText, \
    MediumText, LineBreak, simple_page_number
from pylatex.utils import bold


def generate_header():
    geometry_options = {"margin": "0.7in"}
    doc = Document(geometry_options=geometry_options)
    # Add document header
    header = PageStyle("header")
    # Create left header
    with header.create(Head("L")):
        header.append("Page date: ")
        header.append(LineBreak())
        header.append("R3")
    # Create center header
    with header.create(Head("C")):
        header.append("Company")
    # Create right header
    with header.create(Head("R")):
        header.append(simple_page_number())
    # Create left footer
    with header.create(Foot("L")):
        header.append("Left Footer")
    # Create center footer
    with header.create(Foot("C")):
        header.append("Center Footer")
    # Create right footer
    with header.create(Foot("R")):
        header.append("Right Footer")

    doc.preamble.append(header)
    doc.change_document_style("header")

    # Add Heading
    with doc.create(MiniPage(align='c')):
        doc.append(LargeText(bold("Title")))
        doc.append(LineBreak())
        doc.append(MediumText(bold("As at:")))

    doc.generate_pdf("header", clean_tex=False)

generate_header()

I am getting this error:

Traceback (most recent call last):
  File "/Users/user/git/project/test.py", line 43, in <module>
    generate_header()
  File "/Users/user/git/project/test.py", line 41, in generate_header
    doc.generate_pdf("header", clean_tex=False)
  File "/Users/user/opt/anaconda3/envs/env/lib/python3.11/site-packages/pylatex/document.py", line 250, in generate_pdf
    output = subprocess.check_output(command,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/opt/anaconda3/envs/env/lib/python3.11/subprocess.py", line 465, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/opt/anaconda3/envs/env/lib/python3.11/subprocess.py", line 569, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['latexmk', '--pdf', '--interaction=nonstopmode', 'XXX']' returned non-zero exit status 12.

Additionally before the Traceback it says: sh: pdflatex: command not found although I have installed it in the anaconda environment.

The XXX location is pointing to the right anaconda environment so I have no idea what's wrong here.

  • Check the stderr from the command that failed. How to retrieve it depends on the details of the `subprocess.run()` (or similar) call used to start the process, but it _may_ be an attribute on the exception. (If you had included the full stack trace, we'd know more). – Charles Duffy Nov 21 '22 at 01:49
  • That said, I can't reproduce this error. See https://replit.com/@CharlesDuffy2/RequiredRoundedArrays#main.py -- it successfully creates a `header.pdf` when run in the sandbox with a copy of LaTeX installed via nixpkgs (specifically, `pkgs.texlive.combined.scheme-full`; might possibly work with `scheme-basic`, I just didn't try). A [mre] needs to be complete enough other people can follow instructions in the question to see the same problem ourselves; can't test fixes for an issue we can't reproduce. – Charles Duffy Nov 21 '22 at 01:57
  • 1
    Ahh! Thank you for adding more details: that `sh: pdflatex: command not found` is your underlying problem. `pdflatex` is not a Python library -- it's something you should be installing with your OS-vendor-provided package manager, not with conda. – Charles Duffy Nov 21 '22 at 12:50
  • BTW, you could also _try_ running `latexmk --pdf` without Python to prove to yourself that it's not a Python-specific problem. – Charles Duffy Nov 21 '22 at 12:50
  • Alright I think I got what the problem is. So I need to install pdflatex on the system? What is then the pdflatex package using pip for? – samuelhertrich Nov 21 '22 at 19:29
  • The pip package is a wrapper around the command-line tool to provide an API for it from Python; but because it's just a wrapper, it doesn't work without the underlying tool being installed. – Charles Duffy Nov 21 '22 at 22:25
  • 1
    Ah okay now I get it. I've installed TexLive and now it's working thank you! – samuelhertrich Nov 22 '22 at 09:53

0 Answers0