Search for (LaTeX) regex found NOT within another regex (math mode)

107 Views Asked by At

I'm trying to find all improperly-written commands in LaTeX that are NOT within math mode. That is, in the following, I want to match "\bad" but NOT "\good\" or "\math".

Example of \bad command. Example of \good\ command. And $x=\math + y$ is also a good command.

I figured out how to match math mode, which begins and ends with non-escaped dollar signs "$" -- I essentially want to invert this match somehow:

(?<!\\)\$.+?[^\\]\$

And I figured out how to match "\bad" but not "\good\" (note the space after the +):

\\[A-Za-z]+ 

But I can't figure out how to combine the two. I've tried (negative) lookarounds and don't seem to be getting anywhere (not every paragraph necessarily contains math mode). Any suggestions/hints? Thanks in advance!

EDIT:

  1. I'm using Perl-compatible regex (Sublime Text 3, to be exact).
  2. A "bad" command is a macro NOT within math mode, followed by a space. A "good" command is a macro followed by anything else -- punctuation or backslash -- or any macro within math mode.
1

There are 1 best solutions below

3
On BEST ANSWER

Sublime Text uses the Boost library to handle regexes, so it's not fully Perl-compatible.

The true Perl-compatible answer would be:

\$.*?\$(*SKIP)(*FAIL)|\\\w+(?=\s)

Demo

But you can't use the (*SKIP)(*FAIL) trick with Boost, unfortunately (until this feature request get implemented).

You can work around this somehow like that:

\$.*?\$\K|\\\w+(?(?=\s)|\K)

Demo

The problem with this approach is that "bad" commands and math mode end markers will still yield an empty (zero-length) match (thanks to \K). I don't know if that's good enough for you.