Python cannot compile LaTeX file

2,259

tex relative paths are relative to the working directory from where the program was started, not the directory of the main file. can you get python to do

cd foo; xeletex file

instead of

xelatex foo/file

?

Share:
2,259

Related videos on Youtube

Valerio
Author by

Valerio

Enthusiast LaTeX and Stata user.

Updated on August 01, 2022

Comments

  • Valerio
    Valerio over 1 year

    I am using a Conda Python installation and Jupyter to work with a script producing a LaTeX file I want to compile. These are the relevant code lines:

    import subprocess
    xelatex= "/usr/local/texlive/2018/bin/x86_64-darwin/xelatex"
    subprocess.check_call([xelatex, '-shell-escape', '-file-line-error', '-interaction=nonstopmode', '-synctex=1', my_folder+'/output_file.tex'])
    

    Unfortunately, the LaTeX source file does not get compiled and Python returns a non-zero exit status along the following lines:

    ---------------------------------------------------------------------------
    CalledProcessError                        Traceback (most recent call last)
    <ipython-input-26-fc4edbf90d41> in <module>
          1 import subprocess
          2 xelatex= "/usr/local/texlive/2018/bin/x86_64-darwin/xelatex"
    ----> 3 subprocess.check_call([xelatex, '-shell-escape', '-file-line-error', '-interaction=nonstopmode', '-synctex=1', dir_AED+'/registro_lezioni.tex'])
    
    ~/anaconda3/lib/python3.6/subprocess.py in check_call(*popenargs, **kwargs)
        289         if cmd is None:
        290             cmd = popenargs[0]
    --> 291         raise CalledProcessError(retcode, cmd)
        292     return 0
        293 
    
    CalledProcessError: Command '['/usr/local/texlive/2018/bin/x86_64-darwin/xelatex', '-shell-escape', '-file-line-error', '-interaction=nonstopmode', '-synctex=1', 'my_folder+'/output_file.tex']' returned non-zero exit status 1.
    

    I have a Tex Live 2018 installation on my Mac that works smoothly with many other apps:

    Last login: Sat Nov  3 07:39:44 on ttys000
    Restored session: Sat Nov  3 07:33:30 CET 2018MacBook-Pro:~ m*****$ xelatex
    This is XeTeX, Version 3.14159265-2.6-0.99999 (TeX Live 2018) (preloaded format=xelatex) restricted \write18 enabled.
    **
    

    Using any other LaTeX-based app, like Texpad, the source file gets compiled with no errors. I went to the Conda terminal window and the problem seems to be that the LaTeX compiler cannot find a ancillary file:

    ABD: EveryShipout initializing macros
    (/usr/local/texlive/2018/texmf-dist/tex/latex/translator/translator-basic-dicti
    onary-English.dict)
    (/usr/local/texlive/2018/texmf-dist/tex/latex/siunitx/siunitx-abbreviations.cfg
    ) (/usr/local/texlive/2018/texmf-dist/tex/latex/wasysym/uwasy.fd)
    
    ! LaTeX Error: File `fragment.tex' not found.
    

    Nevertheless, the file fragment.tex does live in the same folder, but the LaTeX compiler cannot find it.

    In sum, I need to instruct the LaTeX compiler to look for files in the same folder where the main source file is located.

    • Valerio
      Valerio about 5 years
      @Grimler, I can hardly understand why your raised these points since: (1) I wrote that "the file fragment.tex does live in the same folder," (2) I also added that using Texpad "the source file gets compiled with no errors." (3) To state it again: the main source file and the fragment are in the same directory. (4) The problem is totally related to how the Python compiler is being called by Python.
    • Admin
      Admin about 5 years
      Ah, right, you wrote that you got the LaTeX Error: File fragment.tex' not found.`-error in the terminal window, not from texpad, my bad
    • David Carlisle
      David Carlisle about 5 years
      tex relative paths are relative to the working directory from where the program was started, not the directory of the main file. can you get python to do cd foo; xeletex file instead of xelatex foo/file ?
    • Valerio
      Valerio about 5 years
      @DavidCarlisle Thanks so much! I have just changed the working directory and the LaTeX file now gets compiled. If I remember correctly, one can also pass the required folder as an additional argument to the subprocess.check_call command, right?
    • David Carlisle
      David Carlisle about 5 years
      @Valerio I can tell you about the tex, not the python:-)