0

I am a mac user. This is my python code to generate a pdf file:

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

# run in terminal: cd to scraper folder,
# to compile: pdflatex -pdf test1.tex

# Create a new document
doc = Document()

# Add a section and some content
with doc.create(Section('Introduction')):
    doc.append('Hello, this is a simple LaTeX document generated using Python and pylatex!')

# Generate and save the LaTeX document
doc.generate_tex('test1')
doc.generate_pdf('test1', clean_tex=False, compiler='/usr/local/bin/latexmk')

print("LaTeX file 'example_pylatex.tex' and PDF 'example_pylatex.pdf' created successfully.")

Every time i run this, it gives this:

 Rc files read:
      NONE
    Latexmk: This is Latexmk, John Collins, 7 Apr. 2024. Version 4.85.
    No specific requests made, so using default for latexmk.
    Latexmk: applying rule 'latex'...
    Rule 'latex':  Reasons for rerun
    Changed files or newly in use/created:
      /Users/c/Desktop/scraper/test1.tex
      test1.aux
    Category 'changed_user':
      /Users/c/Desktop/scraper/test1.tex
    
        ------------
        Run number 1 of rule 'latex'
        ------------
        ------------
        Running 'latex  --interaction=nonstopmode -recorder  "/Users/c/Desktop/scraper/test1.tex"'
        ------------
        sh: latex: command not found
        Latexmk: No log file was found, neither the expected one, 'test1.log', nor one in '.'.
        Latexmk: fls file doesn't appear to have been made.
        Latexmk: Couldn't read log file 'test1.log':
          No such file or directory
        ----------------------
        This message may duplicate earlier message.
        Latexmk: Failure in processing file '/Users/c/Desktop/scraper/test1.tex':
           *LaTeX didn't generate the expected log file 'test1.log'
        ----------------------
        Latexmk: Sometimes, the -f option can be used to get latexmk
          to try to force complete processing.
          But normally, you will need to correct the file(s) that caused the
          error, and then rerun latexmk.
          In some cases, it is best to clean out generated files before rerunning
          latexmk after you've corrected the files.
        Latexmk: Getting log file 'test1.log'
        Latexmk: Errors, so I did not complete making targets
        Collected error summary (may duplicate other messages):
          latex: Run of rule 'latex' gave a non-zero error code
        
        
        Traceback (most recent call last):
          File "/Users/
c/Desktop/scraper/pypdf2trying.py", line 16, in <module>
            doc.generate_pdf('test1', clean_tex=False, compiler='/usr/local/bin/latexmk')
          File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pylatex/document.py", line 276, in generate_pdf
            output = subprocess.check_output(
          File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 466, in check_output
            return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
          File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 571, in run
            raise CalledProcessError(retcode, process.args,
        subprocess.CalledProcessError: Command '['/usr/local/bin/latexmk', '--interaction=nonstopmode', '/Users/c/Desktop/scraper/test1.tex']' returned non-zero exit status 12.
    

In my "nano ~/.zshrc" i have these sourced:

export PATH=/usr/local/bin/latexmk:$PATH
export PATH=/usr/local/bin/latex:$PATH
export PATH=/usr/local/bin/pdflatex:$PATH

And in the terminal i can successfully generate a pdf with either "latexmk -pdf test1.tex" or "latex -pdf test1.tex", but it just doesn't work in python... what can i do?

UPDATE: somehow this code worked for me, i used subprocess:

import os
from pylatex import Document, Section
import subprocess

def clean_up():
    auxiliaries = ['test2.aux', 'test2.fdb_latexmk', 'test2.fls', 'test2.log']
    for file in auxiliaries:
        if os.path.exists(file):
            os.remove(file)


# Ensure the correct PATH is set
os.environ['PATH'] = '/usr/local/bin:' + os.environ['PATH']

# Create a new document
doc = Document()

# Add a section and some content
with doc.create(Section('Introduction')):
    doc.append('Hello, this is a simple LaTeX document generated using Python and pylatex!')

# Generate and save the LaTeX document
doc.generate_tex('test2')
#doc.generate_pdf('test2', clean_tex=False, compiler='/usr/local/bin/latexmk', options=["-pdf"])
subprocess.run(['/usr/local/bin/latexmk', '-pdf', 'test2.tex'], check=True)
print("LaTeX file 'test2.tex' and PDF 'test2.pdf' created successfully.")

clean_up()
print("Cleaned up auxiliary files from latexmk")
New contributor
SupersonicMan12 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
10
  • I don't understand where the content of the tex file is inserted. If I compare your code to the manual page of pylatex, see here: jeltef.github.io/PyLaTeX/current/examples/full.html , it looks quite differently?!
    – Keks Dose
    Commented Jul 11 at 10:20
  • 3
    This seems to be the same as your previous question - you get the error sh: latex: command not found, which means that pylatex and/or latexmk cannot find your latex installation, probably because it doesn't use your login shell so it doesn't see your PATH changes - can you explain how this is different?
    – Marijn
    Commented Jul 11 at 10:25
  • 2
    you need to set the PATH in the right shell and to the right value export PATH=/usr/local/bin/pdflatex:$PATH looks completely wrong, PATH takes directories not names of programs so it should be export PATH=/usr/local/bin:$PATH except that it is highly unlikely that /usr/local/bin is not already in your path. Commented Jul 11 at 10:44
  • @KeksDose, i think i only used some of the very basic functions... the main ones i see should align...? Commented Jul 11 at 11:14
  • 1
    ... not sure if you want to publish your OPENAI_API_KEY here Commented Jul 11 at 11:20

0

Browse other questions tagged .