Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
Works like a charm, except that LaTeXTools doesn't seem to be giving precedence to LaTeX.sublime-build in my user packages folder. Quite odd... –  fgb Apr 13 '13 at 6:58
    
Do you make a full copy of the default LaTeX.sublime-build, which you then modify, or just include the relevant osx portion? –  fgb Apr 13 '13 at 7:25
1  
First of all thanks for the edits. I did a full copy of the default file and modified them accordingly. What I found out though was that XeLaTeX compiles all LaTeX sources fine, at least on all of the files I tested. Maybe there's a fallback somewhere in the background? –  slhck Apr 13 '13 at 7:55
    
Copying and modifying the full default file solved my issue. Interesting that using just XeLaTeX is sufficient for both. I'll check this out in the future as it'll definitely be better than having to switch compilers. Thanks for the prompt response! –  fgb Apr 13 '13 at 19:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.