1

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.

  • I don't think this is possible in Sublime Text currently. The .sublime-syntax file just attaches "scope" names to the different parts of a LaTeX file (so that in e.g. \section[One]{...} the "One" gets the name entity.name.section.latex). It doesn't do any styling/colouring itself. As far as I know that side of things is controlled entirely by a colour scheme (a .tmTheme, for example Monokai) using the scope names. I know they can choose different colours and sometimes bold/italic, but I've never seen changing the font size. – 9tTn9B Apr 25 '17 at 9:16
  • Just bold without a change in font size would be an improvement. – Hugh Apr 25 '17 at 9:17
3

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.

  • 2
    You should change the colorscheme not the syntax definition in such cases. The colorscheme should manage the appearance and th syntax definition should provide scopes. In addition you may miss future updates if you change it. – r-stein Apr 25 '17 at 19:25
  • I agree. I did want to show changing the syntax definition anyway since, while it is less correct, it's quick and easy. – 9tTn9B Apr 25 '17 at 19:31
  • Yes, it a good and detailed answer. I just wanted to emphasize, that the 2nd part is more desirable. – r-stein Apr 25 '17 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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