How can I switch between pdflatex and xelatex conversion in Sublime Text 2?

I often switch between compiling "classic" LaTeX and XeTeX documents.

Sublime Text 2 has a package called LaTeXTools that allows you to build LaTeX documents via a simple CmdB. Neat.

The build file LaTeX.sublime-build obviously exists in the standard package folder, and you can copy it to your User folder to customize it. Here, it allows you to change the arguments given to latexmk, namely to switch from pdflatex to xelatex, for example like this:

"cmd": ["latexmk", "-cd",
  //"-e", "\\$pdflatex = 'pdflatex %O -interaction=nonstopmode -synctex=1 %S'",
  "-e", "\\$pdflatex = 'xelatex %O -interaction=nonstopmode -synctex=1 %S'",
  //"-silent",
  "-f", "-pdf"],

Now, it's very tedious having to do that every time I open up another document – open my build preferences, comment out the one line, save, etc.

Ideally, I'd like to be able to switch the engine

  • … automatically – TextMate for example somehow does that, and I can compile both LaTeX documents and XeTeX documents with a simple CmdR, although the TextMate configuration is set to pdflatex. Maybe it's some latexmk.pl-fu, I don't know.

  • … with a simple keyboard shortcut or setting – Maybe pressing CmdL, CmdX or similar. This would then toggle between pdflatex and xelatex building.

How could I set that up?


Solution 1:

Note: LaTeXTools for Sublime Text now supports automatic engine detection if your file starts with %!TEX program = <program>, where <program> is any of pdflatex, xelatex or lualatex. The below instructions are only necessary if you need to manually switch engines for whatever reasons.


Take the LaTeX.sublime-build file in the LaTeXTools folder under ~/Library/Application Support/Sublime Text 2/Packages and copy it to ~/Library/Application Support/Sublime Text 2/Packages/User/, but do it twice and give those files the following name:

  • LaTeX.sublime-build.latex
  • LaTeX.sublime-build.xetex

Change the files to use the pdflatex for LaTeX and xelatex engines for XeTeX, respectively (as seen in the question).

Then, create a new plugin through Tools » New Plugin… (saving it as switch.py to the location above):

import sublime, sublime_plugin, os, shutil, filecmp

class SwitchCommand(sublime_plugin.ApplicationCommand):
  def run(self):
    folder = os.path.expanduser( \
        '~/Library/Application Support/Sublime Text 2/Packages/User/')
    latex_src = folder + 'LaTeX.sublime-build.latex'
    xetex_src = folder + 'LaTeX.sublime-build.xetex'
    dest = folder + 'LaTeX.sublime-build'

    if filecmp.cmp(dest, latex_src):
      sublime.status_message("Switching to XeTeX")
      shutil.copy(xetex_src, dest)
    else:
      sublime.status_message("Switching to LaTeX")
      shutil.copy(latex_src, dest)

And create a keyboard shortcut for it in Preferences » Key Bindings – User:

[ {
  "keys": ["super+shift+x"], "command": "switch"
} ]

You can also add this to your menu by adding the Main.sublime-menu file to your User package:

[ {
    "id":"tools", "children":
    [ {
        "command": "switch",
        "caption": "Switch between LaTeX/XeTeX"
      }
    ]
} ]

This will now compare the currently active build file with the build settings needed for XeTeX and LaTeX and switch them if necessary.

This could sure use some improvement, but it works for the moment.