More Tips For LaTeX in TextMate August 6
A few days ago I wrote up a quick tip about how to fix the double dollar sign problem with textmate syntax highlighting for LaTeX. This fixed one problem but whenever I typed something like $\blah$ I’d end up with $\$ on the screen at some point and the slash would escape the second dollar sign causing textmate to believe math mode extended to wherever the next dollar sign occurred. Read on if you want tips on solving this issue as well.
The first fix is to prevent inline math mode from extending past a line break. This is actually the appropriate behavior since latex throws an error if the text inside single dollar signs contains a carriage return/line feed. I submitted this bug to the latex syntax maintainer but in the meantime find the line in the TeX language definition under the LaTeX bundle (in textmate of course we don’t want to edit the original file) that looks like this:
name = 'string.other.math.tex';
begin = '\$';
end = '\$';
Change the regular expression for ending inline math mode so it always ends at a line break. This section should now read
name = 'string.other.math.tex';
begin = '\$';
end = '\$|[\n]';
This limits the problem and if you want to retain correct syntax highlighting behavior is as far as you probably want to go. However, I virtually never escape dollar signs so I further modified the language definition by changing the following section
name = 'constant.character.escape.tex';
match = '(\)[^a-zA-Z@]';
to read
name = 'constant.character.escape.tex';
match = '(\)[^a-zA-Z@$]|(?<=[{])\$';
And I deleted the earlier section (between the brackets) that explicitly gives ‘\\$’ as an escaped character (sorry I don’t feel like undoing my changes to recover what these lines originally looked like). This does two things. It prevents \$ from being recognized as an escaped $ (still interprets it as the end of math mode) but allows me to define a macro this macro \newcommand{\dolsign}{\$} and have the \$ recognized as an escaped $ there. So I give up being able to type \$ and have it recognized correctly but I can still use \dolsign to achieve the same effect in every case I care about.
There is still one difficulty here with escaped brackets. textmate correctly recognizes } as being an escaped ‘}’ symbol and brackets can swallow more than one line. For my personal use I also added ‘}’ to the match sequence above leaving it looking like this
name = 'constant.character.escape.tex';
match = '(\)[^a-zA-Z@$}{]|(?<=[{])\$';
However, this breaks the common { and } means of bracketing so I don’t recommend it for others. I almost always use \left{ and \right} but others don’t.
Once again these things feel like dirty hacks so better fixes are welcome.
LaTeX in TextMate:
- Fixing Latex Math in Textmate
- More Tips For LaTeX in TextMate
No Comments
Reply ››