[TxMt] LaTeX Language Grammar and sub/superscripts

Charilaos Skiadas cskiadas at uchicago.edu
Tue Jul 18 21:23:43 UTC 2006


On Jul 18, 2006, at 3:33 PM, Mike Miller wrote:

> I am attempting to modify the LaTeX Language Grammar so that I may  
> style superscripts and subscripts distinctly.  I tried to add the  
> following:
> {   name = 'constant.superscript.latex';
>     match = '\^(\w{1}|\{.*?\}|\\\w*)';
>     captures = { 1 = { name = 'meta.supersript.latex'; }; };	
> },
> {   name = 'constant.subscript.latex';
>     match = '_(\w{1}|\{.*?\}|\\\w*)';
>     captures = { 1 = { name = 'meta.subscript.latex'; }; };
> },

> However, when I invoke the "show scope" command in the TextMate  
> bundle, neither of these scopes will appear.  (For example, if I  
> type "Suppose that $\{x_n\}$ is a sequence..." and put the caret  
> after the underscore, neither "constant.subscript.latex" nor  
> "meta.subscript.latex" appears in the show scope tooltip.)

Try to have something like that outside of dollar signs. Will it  
match there?

Let me use this opportunity to explain what happens in general. The  
way TextMate matches things is that at each location it goes through  
all the top rules in the LaTeX grammar and tries to match the rule  
starting at each location. If it can't then it moves on. Now let's  
look at the LaTeX grammar for a second. The first relevant rules we  
find are these:

		{	name = 'string.other.math.latex';
			begin = '\\\(';
			end = '\\\)';
			patterns = ( { include = 'source.math.tex'; } );
		},
		{	name = 'string.other.math.latex';
			begin = '\\\[';
			end = '\\\]';
			patterns = ( { include = 'source.math.tex'; } );
		},

These are the ones dealing with other kinds of math environments.  
Since we'll see the same thing with the dollar sign environment in a  
moment, I'll talk about it then.
The next thing we find is:

		{	include = 'source.tex'; },

This means that if none of the previous rules has matched, then we  
try all the rules in source.tex. That's the TeX grammar. Let's go in  
there, because that's where the dollar sign rules are:
		{	name = 'string.other.math.tex';
			begin = '\$';
			end = '\$';
			swallow = '\\\$';
			patterns = (
				{	include = 'source.math.tex'; },
				{	include = '$self'; },
			);
		},

This matches a dollar sign until the next unescaped dollar sign (the  
swallow rule takes care of that, an escaped dollar signs will be  
eaten by it. ). Note now the patterns part. This shows *all* the  
things that can match inside the braces. *NO* other rules will apply  
in there. So if your two rules are not matched by what those patterns  
see, then they won't be used.

Now these patterns say that one should include everything from  
source.math.tex. This is the TeX Math grammar, yet another grammar in  
the LaTeX bundle. This is where you should put your additions to have  
them matched inside a math environment. I just tried it, and it works  
just fine. As far as naming goes, this is the standard source of  
information:
http://macromates.com/textmate/manual/ 
language_grammars#naming_conventions
This would be slightly better when you place in in the TeX Math grammar:

		{	name = 'constant.superscript.math.tex';
			match = '\^(\w{1}|\{.*?\}|\\\w*)';
			captures = { 1 = { name = 'meta.supersript.latex'; }; };
		},
		{	name = 'constant.subscript.math.tex';
			match = '_(\w{1}|\{.*?\}|\\\w*)';
			captures = { 1 = { name = 'meta.subscript.latex'; }; };
		},

You could consider the _ and ^ characters as constants, but what goes  
between the braces is certainly not. For instance imagine you have:

$x_{\sin(y)}$

You would clearly want to match the \sin part as a function. Your  
method treats everything between the braces in a uniform way. What  
you really want is to use an include command for that part. The way  
to do that is to take the _{} out of the above two rules, and create  
two new rules, that use begin-end together with including $self in  
the patterns.

I hope this is sort of clear, let me know if some part doesn't make  
sense. The LaTeX/TeX/TeXMath grammars are a very good example of how  
things should work, but not very easy for grammar beginners.

> I am new to regular expressions, but if I do a find and replace  
> with those expressions, they do seem to find all occurrences of  
> subscripts and superscripts.  I think I probably don't understand  
> the LaTeX language grammar, since I have trouble understanding even  
> the shortest of grammars!
>
> Also, are my naming conventions even close to correct?
>
> For reference, I'm using TextMate 1.5.2 (r1180) and revision 4405  
> of the LaTeX bundle from the repository.
>
> Thanks for your help,
> Mike Miller

Haris





More information about the textmate mailing list