4

I have a custom command which I use frequently to comment out chunks of a source file, defined as follows:

\newcommand{\comment}[1]{}

In Sublime Text (with LaTeXTools), this command throws the syntax highlighting off: at the start of the first \comment{ block in a file, everything begins to be uniformly greyed out, as it should, but this doesn't ever stop; it continues past the closing }, through the entire rest of the file. Everything compiles fine; it's just that the highlighting doesn't resume properly.

The behaviour I'd like is for the \comment{ } block to be entirely greyed out, as it currently is, but for normal highlighting to resume once it closes. Is there a way I can get Sublime Text and LaTeXTools to do this?

3
  • How did you get Sublime Text to turn off the syntax highlighting when using that command?
    – Vincent
    Commented Dec 13, 2019 at 3:12
  • I didn't do anything, as far as I can recall, and I don't see anything in my user settings that it could be. I was surprised to see it respond in any way, since it doesn't to my other commands. It seems to be sensitive to the word comment; if I define eg \newcommand{\cmt}[1]{} instead, then everything is highlighted as usual both inside and outside the commented block (which still isn't the behaviour I'm trying to get). Commented Dec 13, 2019 at 3:55
  • Since the LaTeX syntax itself depends on the LaTeX source code, both LaTeXTools and the LaTeX syntax file special support for the some popular packages. This is the reason, that the comment package (ctan.org/pkg/comment) is supported in the syntax highlighting, i.e. \comment ... \endcomment and \begin{comment}...\end{comment} will be highlighted as comment. You may have a look at that package and just use it (it may also be interesting for your usecase) or change the syntax highlighting as suggested by Vincent.
    – r-stein
    Commented Dec 15, 2019 at 14:19

1 Answer 1

1

First of all, I think I should say that this is the first time that I code something in a .sublime-syntax file, so it's rather probable that I made some beginner mistake or that my solution is not optimal. However, I found your problem interesting, and after some research I found a solution that seems to work for me.

The syntax highlighting is controlled by the LaTeX.sublime-syntax file, so to modify how the highlighting behaves, the first thing to do is to access this file. The easiest way is to use PackageResourceViewer, which can be installed with Package Control. Then you can use the Open Resource tool to find the file, which should be in the LaTeX folder.

Once you have opened the LaTeX.sublime-syntax file, there are two changes to make. First, you have to remove the section of the code which throws all of the syntax highlighting off as soon as a \comment{ command is detected. This part of the code should be right after a pkgcomment: command, and should be the following

- match: '^(\\)comment\b'
  captures:
    0: punctuation.definition.comment.start.latex
    1: punctuation.definition.backslash.latex
  push:
    - meta_scope: meta.environment.comment.latex comment.block.command.comment.latex
    - match: '^(\\)endcomment\b'
      captures:
        0: punctuation.definition.comment.end.latex
        1: punctuation.definition.backslash.latex
      pop: true

As I am pretty far from being an expert in sublime-syntax files, I would strongly recommend to simply comment out that part of the code, in case anything bad happens. Each line can be commented out by placing a # at the beginning of each line.

Once the "bad" code is removed, the remaining task is to add commands to comment out what is placed between a \comment{ command and the next }. To do so, you can add a new paragraph at the beginning of the file, after the - include: comment line, in the prototype: section. If I understood well, placing it in this section gives the command a higher priority, so that it will work also in math mode, for example.

The following code corresponds to the beginning of the file, with the code I added to define the highlighting of the \comment command. The new code is indicated in comments.

name: LaTeX
file_extensions:
  - tex
  - ltx
scope: text.tex.latex
contexts:
  prototype:
    - include: comments
    # The following paragraph is a replacement to the commented code block in the pkgcomment section.
    - match: '(\\)comment(\{)'
      scope: support.function.general.latex
      captures:
        0: punctuation.definition.comment.start.latex
        1: punctuation.definition.backslash.latex
      push:
        - meta_content_scope: meta.environment.comment.latex comment.block.command.comment.latex
        - match: '(\{)'
          push: comment_braces
        - match: '(\})'
          captures:
            0: punctuation.definition.comment.end.latex
            1: punctuation.definition.backslash.latex
          pop: true

  # this is used to assert that the commented section ends with the corresponding }.
  comment_braces:
    - match: '(\{)'
      push: comment_braces
    - match: '(\})'
      pop: true

This code should be followed by the main: section.

With these changes, when I write \comment{some random text} in a LaTeX file, the \comment{ part looks the same as any other regular command, the some random text is highlighted as a comment until the matching } is reached. It also works in math mode, and if other braces are placed in the argument of \comment{}.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .