3

I am using Sublime Text 3, in this case to edit LaTeX. To further customize the syntax highlighting I would like to define a new scope that simply extends the shipped LaTeX.tmLanguage (to which I do not have access, not even to read). N.B. I have no interest to write my own tmLanguage-file, not even to download an existing, I just want to extend it, much like overloading a class in Python.

In particular I would like to add meta.footnote.latex to have a custom syntax highlight for \footnote{...} (one definition I found in https://github.com/bradrobertson/sublime-packages/blob/master/LaTeX/LaTeX.tmLanguage)

Tom de Geus
  • 5,625
  • 2
  • 33
  • 77

1 Answers1

6

You can use the include directive to import existing rules from a different file and override or extend its rules.

.tmLanguage:

<dict>
    <key>fileTypes</key>
    <array>
        <string>mylatex</string>
    </array>
    <key>name</key>
    <string>MyLatex</string>
    <key>patterns</key>
    <array>
        <dict>
            <key>include</key>
            <string>text.tex.latex</string>
        </dict>
        <!-- your rules -->
    </array>
    <key>scopeName</key>
    <string>text.tex.latex.mylatex</string>
</dict>

.sublime-syntax:

%YAML 1.2
---
name: MyLatex
file_extensions:
  - mylatex
scope: text.tex.latex.mylatex
contexts:
  main:
    - include: scope:text.tex.latex
    # your rules
idleberg
  • 12,634
  • 7
  • 43
  • 70
  • Great! This was a good suggestion. Based on this, further searching, and help of the Sublime forum I have found that I should probably be focusing on using the `.sublime-syntax` format instead (in `YAML`), which is not compatible with the `.tmLanguage` format. I'm trying to do the same as in your answer. But so far unsuccessfully. Do you also have experience with that format? I will try some more before pursuing further help. [See documentation](https://www.sublimetext.com/docs/3/syntax.html#include-syntax) – Tom de Geus Apr 18 '17 at 14:19
  • Great, this is exactly what I needed! – Tom de Geus Apr 19 '17 at 16:32
  • Does this force the language indicator at the bottom right to MyLatex or something else? Are all such extensions included at all times? – Peter Gerdes Dec 31 '17 at 09:40
  • @PeterGerdes The first I can confirm, you will see MyLatex. The latter I cannot guarantee the extend, but I can say that I have never had any problems, in the sense that my language extension is always automatically selected. – Tom de Geus Dec 31 '17 at 10:20
  • So, as I asked in a new question this means you can't have two packages which don't know about each other and both simultaneously add a scope to a given language (as you would have to pick one of their filetypes and they don't extend each other). https://stackoverflow.com/questions/48041573/multiple-extensions-of-sublime-text-language-definition – Peter Gerdes Dec 31 '17 at 10:31