# Do we need to manually invoke pythontex? TeXShop Engine: PythonTeX with XeLaTeX

I have one problem with pythontex using TeXShop: after compiling it, I need to manually go to the terminal and call pythontex filename.pdf. Is it possible to avoid this and let TeXShop call pythontex?

I almost succeeded in running PythonTeX with this MWE:

\documentclass{beamer}
\usepackage{pythontex}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\usepackage[T1]{fontenc}
\begin{document}
\begin{frame}[fragile]{Partial Derivatives}
\begin{pycode}
from sympy import *
var("x")
functions = [sin(x), cos(x), tan(x)]
print(r"\begin{align*}")
for f in functions:
d = Derivative(f, x)
print(latex(d) + "&=" + latex(d.doit()) + r"\\")
print(r"\end{align*}")
\end{pycode}
\end{frame}
\end{document}


As I need XeLaTeX, my first adaptation of pythontex engine file in TeXShop was:

PATH=/Applications/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin
filename=$1 xelatex -8bit --file-line-error --synctex=1$1
pythontex $1  My second adaptation to the engine template was: PATH=/Applications/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin filename=$1
pdfname = "${filename:r}.pdf" xelatex -8bit --file-line-error --synctex=1$1
pythontex $1 xelatex -8bit --file-line-error --synctex=1$1
pythontex pdfname
xelatex -8bit --file-line-error --synctex=1 $1  I really do not know much about the language used in the engine files, I'm just trying my best educated guess based on the templates I saw. Edit: I followed Allan Munn's answer but I have the same problem. The last part of log file looks like: (./pythontex-files-test/test.pytxmcr) (./test.nav) (./pythontex-files-test/test.pytxpyg) (./test.vrb (./pythontex-files-test/py_default_default_0.stdout (/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/umsb.fd))) [1] (./test.aux) ) Output written on test.pdf (1 page). SyncTeX written on test.synctex.gz. Transcript written on test.log. Traceback (most recent call last): File "/usr/texbin/pythontex", line 50, in <module> import pythontex2 as pythontex File "/usr/local/texlive/2015/texmf-dist/scripts/pythontex/pythontex2.py", line 61, in <module> from pygments.styles import get_all_styles ImportError: No module named pygments.styles  ## 1 Answer To adapt an Engine file in TeXShop, you need to make sure that the execute bit on the new file is set, and you usually need to restart TeXShop to make it work. So make a copy of the TeXShop pythontex.engine and put it in ~/Library/TeXShop/Engines The file should just have the following: #!/bin/bash # For anaconda distribution of Python, use PATH=~/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/Library/TeX/texbin # For system distribution of Python, use # PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/Library/TeX/texbin filename=$1

xelatex --file-line-error --synctex=1 $1 pythontex$1

xelatex --file-line-error --synctex=1 \$1


Name this file xepython.engine to distinguish it from the regular pythontex engine.

To set the executable bit on the file, open a Terminal window and type the following:

chmod +x ~/Library/TeXShop/Engines/xepython.engine


Now restart TeXShop and everything should work. You can also access the engine easily in the file itself by using:

% !TEX TS-program = xepython


See this question for more on this:

Since you are using XeLaTeX you should not load the fontenc package in your document.

• Thanks, I removed fontenc and followed all the instructions but I still have the same problem. I did only two deviations from the instructions in the answer: 1) I used the -8bit option for xelatex to avoid an indentation error and 2) since I installed Anaconda on the Applications folder, I used a different path. – Sergio Parreiras Dec 27 '15 at 18:38
• Problem solved! Besides what you pointed, my Anaconda installation had problems. I removed it and re-installed using sudo. – Sergio Parreiras Dec 27 '15 at 20:25
• @SergioParreiras I'm glad you got it fixed (and made this comment, as I was just starting to try to figure out what was going on.) – Alan Munn Dec 27 '15 at 20:26