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{}
.
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).\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.