Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top