# How to use PSTricks with XeLaTeX in TextMate?

I am trying to compile the follwing file, which uses the "pstricks" and "fontspec" packages, using XeLaTeX in TextMate by clicking Bundles → LaTeX → Typeset & View (PDF). In my preferences (Bundles → LaTeX → Preferences...), I have set xelatex as default typesetting engine, but it appears like TextMate uses pdfLaTeX nevertheless.

\documentclass{article}
\usepackage{fontspec}
\usepackage{pstricks}
\begin{document}
\psline(0, 0)(1, 1)
\end{document}


This results in the following error message:

Package: fontspec 2016/02/01 v2.5a Font selection for XeLaTeX and LuaLaTeX

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
/usr/local/texlive/2016basic/texmf-dist/tex/latex/fontspec/fontspec.sty:28: Fat
al fontspec error: "cannot-use-pdftex"
!
! The fontspec package requires either XeTeX or LuaTeX.
!
! You must change your typesetting engine to, e.g., "xelatex" or
! "lualatex"instead of plain "latex" or "pdflatex".
!
! See the fontspec documentation for further information.
!
! For immediate help type H <return>.
!...............................................

l.28 \msg_fatal:nn {fontspec} {cannot-use-pdftex}

|'''''''''''''''''''''''''''''''''''''''''''''''
| This is a fatal error: LaTeX will abort.
|...............................................

• Note you can compile pstricks code with pdflatex if you load auto-pst-pdf after pstricks and launch pdflatex with the -shell-escape switch. – Bernard Feb 2 '17 at 21:12

Whenever TextMate detects that you are importing the PSTricks-package, it will automatically switch back to the default pdflatex command to compile your LaTeX file -- irrespective of your "Default Engine"-setting in Preferences. This is because PSTricks is not 100% compatible with XeLaTeX; TextMate therefore thinks this is an indicator you would rather use pdfLaTeX. However pdfLaTeX does not work together with Fontspec, which is what breaks your document.

The easy way to fix this is to set your preferred LaTeX compilation command on a per-document basis. To do this, go to Bundles → LaTeX → File Preferences → Set Typesetting Engine and choose xelatex. This way, TextMate will use the XeLaTeX-command even if you import the PSTricks-package.

If you do not want to do this for every single document, the only other solution is to edit the source code of the TextMate-LaTeX-bundle.1 To do this, go to Bundles → Edit Bundles..., select the LaTeX-bundle in the left pane and navigate to Support → bin. Right-click the file texmate.py and click Show "texmate.py" in Finder. Then, in Finder, open the file "texmate.py", go to line 658 and remove the item 'pstricks' from the list, so that it reads:

latex_indicators = {'xyling', 'pst-asr', 'OTtablx'}


After saving the file, importing the PSTricks-package will no longer cause TextMate to switch to the pdfLaTeX-compiler.

You might then still get the following error:

xdvipdfmx:fatal: File ended prematurely
Output file removed.
Error 256 (driver return code) generating output;
file test.pdf may not be valid.


What caused this error for me was that the command ps2pdf was not in the PATH of TextMate. Go to TextMate → Preferences... → Variables. Make sure the variable PATH has a check-mark next to it. If it is not there, open a Terminal, type which ps2pdf, and then add a Variable named PATH in TextMate which contains the path which should be shown in your terminal, for example \$PATH:/opt/local/bin:/usr/local/bin:/usr/texbin.

• Why is PSTricks " not 100% compatible with XeLaTeX" ?? – user2478 Feb 2 '17 at 21:14
• @Herbert I read that somewhere, but I can not find the source anymore, sorry. But there must be some reason the TextMate developers included pstricks in the list of "latex_indicators" so there probably are some compatibility issues. – anroesti Feb 2 '17 at 21:20
• Nearly all PStricks code runs with xelatex – user2478 Feb 2 '17 at 21:27
• @Herbert Even more of a reason to remove PSTricks from this list then. – anroesti Feb 2 '17 at 21:36
• As far as I can see textmate is not a specialized latex editor but a more general code editor. So probably they simply don't know enough about the various engines. – Ulrike Fischer Feb 2 '17 at 22:20