[Tex/LaTex] Python cannot compile LaTeX file

pythonxetex

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.

Best Answer

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

?

Related Question