Search code examples
latexsubstitutionbbedit

Replacing part of LaTeX command using BBedit grep


How can I use the BBedit grep option to replace LaTeX commands like

\textcolor{blue}{Some text}

by the contents of the second set of braces, so

Some text

?


Solution

  • The BBEdit Grep Tutorial gives a lot of information and good examples on using the grep option in BBEdit. What you are trying to achieve is actually a variation of one of the examples. The solution is to enter the following:

    Find: \\textcolor\{blue\}\{([^\}]*)\}

    Replace: \1

    The relevant part is the "Find" section. The first part: \\textcolor\{blue\}\{ basically searches for the content \textcolor{blue}{. You need the \s to escape special characters.

    Next, we have the cryptic sequence ([^\}]*): The (...) saves everything inside the parentheses into the variable \1, which you can use in the "Replace" section to insert the content. The [^\}]* consists of ^\} which means match all characters which are not ^ a closing brace \}. With [...]* we say, match any number of "not brace" characters. Overall, this expression makes the grep match all characters which are not closing braces, and saves them into \1.

    Finally, the expression ends with a \}, i.e. a closing brace, which is the end of what we want to find.

    The "Replace" only contains \1, which is everything inside the parentheses (...) in the "Find" field.