[Tex/LaTex] Sublime Text syntax highlighting sections titles as bold in the source

sublime-textsyntax highlighting

Currently the section commands are coloured blue (using the Monokai theme). I would like to add more emphasis to sectioning commands in the source code when open in Sublime Text 3, similar to the effect illustrated in the Emacs answer.

I installed the PackageResourceViewer plugin and opened LaTeX.sublime-syntax resource. I see:

  sections:
    - match: |-
        (?x)
        (
          (\\)
          (?:
            (?:sub){0,2}section
            | (?:sub)?paragraph
            | chapter|part|addpart
            | addchap|addsec|minisec
          )
          (?:\*)?
        )
        (?:
          (\[)([^\[]*?)(\])               # optional Title
        )?
        (\{)
      captures:
        1: support.function.section.latex
        2: punctuation.definition.backslash.latex
        3: punctuation.definition.group.brace.begin.latex
        4: entity.name.section.latex
        5: punctuation.definition.group.brace.end.latex
        6: punctuation.definition.group.brace.begin.latex
      push:
        - meta_scope: meta.section.latex
        - meta_content_scope: entity.name.section.latex
        - match: '\}'
          scope: punctuation.definition.group.brace.end.latex
          pop: true
        - include: main

But I don't know how to proceed.

Best Answer

Overview

The way Sublime Text styles files is to separate the semantic meaning and the actual rendering. This is similar to LaTeX: you have a command \section{...}—indicating the intent—versus the actual rendering, perhaps \textbf{\huge ...} for a very simple example.

A .sublime-syntax file is only supposed to attach semantic meaning to parts of a document ("this is a section"), whilst the job of deciding how to render things ("this is bold") on screen belongs to colour scheme .tmThemes files. (Of course in practice things are not so clean, and you'll see a mix of the two in .sublime-syntax files. In a perfect world this wouldn't happen.)

So you have two options:

  • tweak the syntax file, analogous to replacing every instance of \section{...} with \textbf{\huge ...} to get bold sections. Not recommended, but I'll talk about how you could do it here.

  • tweak the colour scheme file, analogous to changing the definition of \section to embolden sections.

Tweak the syntax

Here we simply change the relevant parts of LaTeX.sublime-syntax to e.g. markup.bold.latex (see this link for a discussion about scope names in Sublime Text). The altered lines are the ones with comments # ... after them.

sections:
  - match: |-
      (?x)
      (
        (\\)
        (?:
          (?:sub){0,2}section
          | (?:sub)?paragraph
          | chapter|part|addpart
          | addchap|addsec|minisec
        )
        (?:\*)?
      )
      (?:
        (\[)([^\[]*?)(\])
      )?
      (\{)
    captures:
      1: support.function.section.latex
      2: punctuation.definition.backslash.latex
      3: punctuation.definition.group.brace.begin.latex
      4: entity.name.section.latex markup.bold.latex  # this is for the reference [...]
      5: punctuation.definition.group.brace.end.latex
      6: punctuation.definition.group.brace.begin.latex
    push:
      - meta_scope: meta.section.latex
      - meta_content_scope: entity.name.section.latex markup.bold.latex  # this is for the title {...}
      - match: '\}'
        scope: punctuation.definition.group.brace.end.latex
        pop: true
      - include: main

(I kept the old scope entity.name.section.latex around in-case things rely on it, but putting markup.bold.latex after it means that the bold has priority over it.)

Results may (will) vary across different colour schemes, for example some don't understand what markup.bold means. You mention Monokai, the default version doesn't handle markup.bold, but e.g. Monokai Extended understands does.

Tweak the colour scheme

The other option is to tweak the colour scheme itself. Colour schemes in Sublime Text are .tmTheme files, basically xml files. They have this sort of structure:

... (preamble stuff)
<plist version="1.0">
<dict>
  <key>name</key> <string>Colour Scheme Name</string>
  <key>settings</key>
  <array>

    <dict>
      <key>settings</key> <dict> ... (general settings) </dict>
    </dict>

    <dict>
      <key>name</key> <string>First Scope Name</string>
      <key>scope</key> <string>first.scope</string>
      <key>settings</key> <dict> ... </dict>
    </dict>

    ...

    <dict>
      <key>name</key> <string>Last Scope Name</string>
      <key>scope</key> <string>last.scope</string>
      <key>settings</key> <dict> ... </dict>
    </dict>

  </array>
</dict>
</plist>

Taking a look at the contents of the default Monokai.tmTheme, the relevant part for us is this entry in the array:

<dict>
  <key>name</key>
  <string>Entity name</string>
  <key>scope</key>
  <string>entity.name - (entity.name.filename | entity.name.section | entity.name.tag | entity.name.label)</string>
  <key>settings</key>
  <dict>
    <key>fontStyle</key>
    <string></string>
    <key>foreground</key>
    <string>#A6E22E</string>
  </dict>
</dict>

This is what controls the style our entity.name.section.latex scope gets. What we can do is put in a new rule specifically targeting LaTeX sections:

<dict>
  <key>name</key>
  <string>LaTeX section entity name</string>
  <key>scope</key>
  <string>entity.name.section.latex</string>
  <key>settings</key>
  <dict>
    <key>fontStyle</key>
    <string>bold</string>
    <key>foreground</key>
    <string>#A6E22E</string>
  </dict>
</dict>

I don't know whether order matters here, to be safe I would put this in the <array> ... </array> list before the "Entity name" entry. Also, I don't know whether it's possible to change the font size; if it can be done, then this would surely be the place to do it.

Related Question