Skip to content

Commit

Permalink
[LaTeX] Math scopes (#3608)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngc92 authored Nov 1, 2023
1 parent 504a1a7 commit 66dccdf
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 180 deletions.
22 changes: 15 additions & 7 deletions LaTeX/LaTeX.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -581,16 +581,22 @@ contexts:
- include: inline-math-ensuremath

inline-math-ensuremath:
- match: ((\\)ensuremath)(\{)
- match: (\\)ensuremath{{endcs}}
scope: support.function.ensuremath.latex
captures:
1: support.function.ensuremath.latex
2: punctuation.definition.backslash.latex
3: punctuation.definition.group.brace.begin.tex
push: inline-math-ensuremath-body
1: punctuation.definition.backslash.latex
push: inline-math-ensuremath-begin

inline-math-ensuremath-begin:
- meta_scope: meta.function.ensuremath.latex
- match: \{
scope: punctuation.definition.group.brace.begin.tex
set: inline-math-ensuremath-body
- include: paragraph-pop

inline-math-ensuremath-body:
- meta_scope: meta.function.ensuremath.latex
- meta_content_scope: meta.environment.math.inline.ensuremath.latex
- meta_content_scope: meta.environment.math.inline.ensuremath.latex markup.math.inline
- include: brace-group-end
- include: math-content

Expand All @@ -601,6 +607,7 @@ contexts:

inline-math-parens-body:
- meta_scope: meta.environment.math.inline.paren.latex
- meta_content_scope: markup.math.inline
- include: inline-math-parens-end
- include: math-content

Expand Down Expand Up @@ -631,7 +638,7 @@ contexts:
push: block-math-begin-end-command-body
block-math-begin-end-command-body:
- meta_content_scope: meta.environment.math.block.be.latex
- meta_content_scope: meta.environment.math.block.be.latex markup.math.block
- include: block-math-begin-end-command-end
- include: math-content

Expand All @@ -652,6 +659,7 @@ contexts:

block-math-brackets-body:
- meta_scope: meta.environment.math.block.bracket.latex
- meta_content_scope: markup.math.block
- include: block-math-brackets-end
- include: math-content

Expand Down
26 changes: 17 additions & 9 deletions LaTeX/TeX.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ contexts:

inline-math-dollar-body:
- meta_scope: meta.environment.math.inline.dollar.tex
- meta_content_scope: markup.math.inline
- include: inline-math-dollar-end
- include: math-content

Expand All @@ -558,6 +559,7 @@ contexts:

block-math-dollar-body:
- meta_scope: meta.environment.math.block.dollar.tex
- meta_content_scope: markup.math.block
- include: block-math-dollar-end
- include: math-content

Expand Down Expand Up @@ -649,14 +651,16 @@ contexts:
1: punctuation.definition.backslash.tex

math-operators:
- match: '[<>]=?'
# note: in math notation, = usually denotes that two terms are equal, instead
# of an assignment.
- match: '[<>]=?|='
scope: keyword.operator.comparison.tex
- match: '[-+*/^!]'
- match: '[-+*/!]'
scope: keyword.operator.arithmetic.tex
- match: =
scope: keyword.operator.assignment.tex
- match: _
scope: punctuation.separator.indice.tex
scope: punctuation.separator.subscript.tex
- match: \^
scope: punctuation.separator.superscript.tex

math-braces:
- match: \{
Expand All @@ -677,10 +681,14 @@ contexts:
scope: punctuation.section.brackets.begin.tex
- match: \]
scope: punctuation.section.brackets.end.tex
- match: (\\)[{}]
scope: constant.character.escape.tex
- match: (\\){
scope: punctuation.section.braces.begin.tex constant.character.escape.tex
captures:
1: punctuation.definition.escape.tex
1: punctuation.definition.backslash.tex
- match: (\\)}
scope: punctuation.section.braces.end.tex constant.character.escape.tex
captures:
1: punctuation.definition.backslash.tex

math-numbers:
- match: \d*(\.)\d+
Expand All @@ -692,7 +700,7 @@ contexts:

math-variables:
- match: '[A-Za-z]+'
scope: variable.other.math.tex
scope: variable.other.math.tex markup.italic.math.tex

###[ PROTOTYPES ]##############################################################

Expand Down
83 changes: 50 additions & 33 deletions LaTeX/tests/syntax_test_latex.tex
Original file line number Diff line number Diff line change
Expand Up @@ -762,47 +762,58 @@ \subsubsection{name}
% Check we have always a shared environment
$f(x) = x^2$
% <- meta.environment.math.inline.dollar.tex string.other.math.tex punctuation.definition.string.begin.tex
%^^^^^^^^^^ meta.environment.math.inline.dollar.tex - string
% ^ meta.environment.math.inline.dollar.tex string.other.math.tex punctuation.definition.string.end.tex
% ^ - meta.environment.math
$$f(x) = x^2$$
% <- meta.environment.math.block.dollar.tex string.other.math.tex punctuation.definition.string.begin.tex
%^ meta.environment.math.block.dollar.tex string.other.math.tex punctuation.definition.string.begin.tex
% ^^^^^^^^^^ meta.environment.math.block.dollar.tex - string
% ^^ meta.environment.math.block.dollar.tex string.other.math.tex punctuation.definition.string.end.tex
% ^ - meta.environment.math
\(f(x) = x^2\)
% <- meta.environment.math.inline.paren.latex string.other.math.latex punctuation.definition.string.begin.latex
%^ meta.environment.math.inline.paren.latex string.other.math.latex punctuation.definition.string.begin.latex
% ^^^^^^^^^^ meta.environment.math.inline.paren.latex - string
% ^^ meta.environment.math.inline.paren.latex string.other.math.latex punctuation.definition.string.end.latex
% ^ - meta.environment.math
\[
% <- meta.environment.math.block.bracket.latex string.other.math.latex punctuation.definition.string.begin.latex
%^ meta.environment.math.block.bracket.latex string.other.math.latex punctuation.definition.string.begin.latex
% ^ meta.environment.math.block.bracket.latex - string
$f(x) = x^2$
%^ string.other.math.tex punctuation.definition.string.begin.tex
%^^^^^^^^^^^^ meta.environment.math.inline.dollar.tex
% ^^^^^^^^^^ markup.math.inline - string
% ^ punctuation.definition.string.end.tex - markup.math.inline
$$f(x) = x^2$$
%^^ string.other.math.tex punctuation.definition.string.begin.tex
%^^^^^^^^^^^^^^ meta.environment.math.block.dollar.tex
% ^^^^^^^^^^ markup.math.block - string
% ^^ string.other.math.tex punctuation.definition.string.end.tex - markup.math.block
\(f(x) = x^2\)
%^^^^^^^^^^^^^^ meta.environment.math.inline.paren.latex
% ^^^^^^^^^^ markup.math.inline - string
%^^ string.other.math.latex punctuation.definition.string.begin.latex
% ^^ string.other.math.latex punctuation.definition.string.end.latex - markup.math.inline
\[
%^^ string.other.math.latex punctuation.definition.string.begin.latex
f(x) = x^2 \text{ $f$ is a function}
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.environment.math.block.bracket.latex
\]
% <- meta.environment.math.block.bracket.latex string.other.math.latex punctuation.definition.string.end.latex
%^ meta.environment.math.block.bracket.latex string.other.math.latex punctuation.definition.string.end.latex
% ^ - meta.environment.math
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.environment.math.block.bracket.latex markup.math.block
% ^^^^^^^^^^ - string
\]
%^^ meta.environment.math.block.bracket.latex punctuation.definition.string.end.latex - markup.math.inline
% ^ - meta.environment.math
\ensuremath{f(x) = x^2}
% <- meta.function.ensuremath.latex support.function.ensuremath.latex punctuation.definition.backslash.latex
%^^^^^^^^^^ meta.function.ensuremath.latex
% ^ meta.function.ensuremath.latex - meta.environment.math
% ^^^^^^^^^^ meta.function.ensuremath.latex meta.environment.math.inline.ensuremath.latex
% ^ meta.function.ensuremath.latex - meta.environment.math
%^^^^^^^^^^^^^^^^^^^^^^ meta.function.ensuremath.latex
% ^ - meta.environment.math - markup.math
% ^^^^^^^^^^ meta.environment.math.inline.ensuremath.latex markup.math.inline
% ^ - meta.environment.math - markup.math
%^^^^^^^^^^ support.function.ensuremath.latex
% ^ punctuation.definition.group.brace.begin.tex
% ^ punctuation.definition.group.brace.end.tex
\ensuremath %
%^^^^^^^^^^^^^ meta.function.ensuremath.latex
%^^^^^^^^^^ support.function.ensuremath.latex
% ^^ comment.line.percentage.tex
{x^2}{normal text}
%^^^^^^^^^ meta.function.ensuremath.latex
% ^ punctuation.definition.group.brace.begin.tex
% ^^^ meta.environment.math.inline.ensuremath.latex markup.math.inline
% ^ punctuation.definition.group.brace.end.tex
% ^^^^^^^^^^^^^ - meta.function.ensuremath.latex
\ensuremaths{5}
% ^ - meta.function.ensuremath.latex
\begin{equation}
% <- support.function.begin.latex keyword.control.flow.begin.latex punctuation.definition.backslash.latex
%^^^^^ support.function.begin.latex keyword.control.flow.begin.latex
Expand All @@ -820,6 +831,12 @@ \subsubsection{name}
% ^ punctuation.definition.group.brace.end.tex
$\verb!$! \alpha$
%^^^^^^^^^^^^^^^ meta.environment.math.inline.dollar.tex markup.math.inline
% ^ markup.raw.verb.latex
% ^^^^^^ keyword.other.math.greek.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Boxes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
Loading

0 comments on commit 66dccdf

Please sign in to comment.