5

I've just created a build system named XeLaTeX by creating a file named XeLaTeX.sublime-build in the User directory of Sublime Text, whose content is:

{
    "cmd": ["xelatex.exe","-synctex=1","-interaction=nonstopmode","$file_base_name"]
}

What should I do, if I want to bind my F1 key to this specific build system?

Note: Ctrl + B, the default build system should not be influenced. That is to say, I could use Ctrl + B to use the default one, and the key F1, the new system, is also available at the same time.


Maybe there is another way to achieve this. Add the following text to Default(Windows).sublime-keymap will execute the command:

{"keys": ["f1"], "command": "exec", "args": {"cmd": ["xelatex.exe","-synctex=1","-interaction=nonstopmode","$file_base_name"]}},

However, $file_base_name is not defined here. Is there any method to pass current file (base_)name to exec?

Ch'en Meng
  • 363
  • 3
  • 14

3 Answers3

5

I nailed it by myself.

AFAIK, there is no such a way to pass current file name through key binding, and it's not possible to use key binding to specify a certain build system. Thus, writing a Python script is of necessity.

There are only three steps.

1. Save the following content to /Data/Package/User/compile_with_xelatex.py:

import sublime, sublime_plugin

class CompileWithXelatexCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      self.view.window().run_command('exec', {'cmd': ["xelatex.exe","-synctex=1","-interaction=nonstopmode", self.view.file_name()[:-4]]})

2. Add a line to /Data/Packages/User/Default(<your-plat>).sublime-keymap

{"keys": ["f1"], "command": "compile_with_xelatex"},

3. Open your LaTeX source file with Sublime Text, and then press F1 to compile it with XeLaTeX.

Indeed, it's a little tricky, but it works like a charm for me.

Ch'en Meng
  • 363
  • 3
  • 14
  • I used a modification of this script to get Sublime Text to run with Black (the formatter). Thank you. – xax Sep 11 '19 at 10:09
3

Build systems work by either selecting them specifically in the Tools -> Build System menu, or by using a selector to match a specific syntax. If you want to use a selector, add the following line to your XeLaTeX.sublime-build file (make sure to add a comma , after the first line, the file needs to be valid JSON):

"selector": "text.tex.latex"

The build command is already bound to CtrlB and F7, but if you also want it bound to F1, open Preferences -> Key Bindings-User and add the following line if you already have custom key bindings:

{ "keys": ["f1"], "command": "build" }

If the file is empty, just add opening and closing square brackets [ ] at the beginning and end of the file, respectively, as it also needs to be valid JSON.

Now, whenever you open a LaTeX file, you should be able to hit F1 to build it. If for some reason it doesn't work (if, for example, you have other build systems for LaTeX installed by plugins like LaTeXTools), then just select Tools -> Build Systems -> XeLaTeX, and everything should work properly.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 1
    Thank you for the answer, but I'm afraid this is not I want. I have two build systems that are designed for LaTeX, say `LaTeXmk` and `XeLaTeX`. Since `LaTeXmk` would compile the soure file three, or even more, times, wasting a lot of time, a method that could specify build system manually is needed. – Ch'en Meng Mar 14 '14 at 14:34
  • Same here. I have two build for "source.java", one to compile the current file, one to compile all files in the current directory. Because there's two, "Tools > Build" is unclickable. My workaround is to use "Alt > T > U > J > Enter" to compile the current file, and "... J > J ..." to compile all. – aliteralmind Oct 29 '14 at 14:12
1

Your example will work, if you give parameter "$file_basename" not "$file_base_name".

Nikolay Gogol
  • 819
  • 7
  • 6