# Operate on all strings in any math environment

I want to create an environment that will search through all of the math in it, to make some changes. So, I want something like a way to apply \regex_extract_all to search through everything which is in math mode in my environment.

I'm leaning towards using latex3, however I'm also considering luatex if that would be considerably easier. In that case, something like Automatically colourize all occurrences of certain strings (different colouring for different sets of words) is similar to what I want, however I want to apply it only within math mode, rather than to the whole document.

I've looked into applying commands to math mode, and it's been looking like I could perhaps use a combination of \regex_extract_all and \everymath however I'm worried about how it would work with other math environments like align equation or even just display mode. These questions:

\everymath, AMS equations, beamer, and tikz not working together

How to color certain commands/environments?

seem to indicate that it's non-trivial to operate on all math-mode text, and they both seem to use workarounds that might not work universally? I'm not sure. Would using \everymath, \everydisplay work for this case?

As a minimal example, if I were trying to make every occurrence of i as a variable red I'd want this code

\DeclarePairedDelimiter\norm{\lVert}{\rVert}
\begin{document}

$i+2=3$

\begin{myEnv}
This is a test about i

$\sin(\theta)+\sum_{i=0}^\infty ix^i$
\begin{equation*}
\norm{i} = \begin{cases}
i & \text{if } i \geq 0 \\
-i & \text{if } i < 0
\end{cases}
\end{equation*}

$$(x_i) = 1/i$$
\end{myEnv}
\end{document}


to generate a document where the "i"s in "i+2=3" on top and in "This is a test about i" are untouched, the "i"s in the words "if" are untouched, and the commands \sin and \infty still work, but the other "i"s used as a variable are made red.

• This seems more an easy LuaTeX task. – Manuel Dec 9 '16 at 18:37
• @Manuel how would it be done in LuaTeX? – drowdemon Dec 9 '16 at 18:40
• LuaTeX is probably a better route. That said, one approach is to use l3regex to search your string and replace it by a (protected) command \myi that tests using \ifmmode (or \mode_if_math:TF) whether it is in math mode and acts accordingly. Note that l3regex is quite slow. – Bruno Le Floch Dec 10 '16 at 1:55
• Just realized that i also appears in and . So my idea above is not good; perhaps it can be salvaged by \def\myi{\ifincsname i\else\ifmmath{\color{red}i}\else i\fi\fi}. – Bruno Le Floch Dec 10 '16 at 12:58

Solving the problem in full generality is something I wouldn't try.

Here's the solution for making i variables red.

\documentclass{article}
\usepackage{amsmath,mathtools,xcolor}

\DeclarePairedDelimiter\norm{\lVert}{\rVert}

\newenvironment{myEnv}
{\edef\redi{{\noexpand\textcolor{red}{\mathchar\the\mathcodei}}}%
\begingroup\lccode~=i \lowercase{\endgroup\let~}\redi
\renewcommand{\sin}{\mathop{\textnormal{\textup{sin}}}}%
\mathcodei="8000 }
{}

\begin{document}

$i+2=3$

\begin{myEnv}
This is a test about $i$

$\sin(\theta)+\sum_{i=0}^\infty ix^i$
\begin{equation*}
\norm{i} =
\begin{cases}
i & \text{if } i \geq 0 \\
-i & \text{if } i < 0
\end{cases}
\end{equation*}
Some text containing some letters,
$(x_{i}) = 1/i$
\end{myEnv}

$i+2=3$

\end{document}


• I really want a solution that lets me manipulate the output of a search. My example was really over-specific to the more general problem I want to solve of "search for certain characters in math mode, pass them to a latex3 (or lua?) function, and eventually potentially replace them with something." In this example, I said replace the variable i with a red i, however I do want an example that would work for any letter, and would let me operate on it in arbitrary ways. I think this also special cases sin? Would all math operators have to be special-cased? That would also be problematic. – drowdemon Dec 9 '16 at 18:29
• @drowdemon My code is just an example of what you crash onto: \sin is defined using a math mode “i” (albeit upright). I can't see how to do this in full generality. – egreg Dec 9 '16 at 18:32
• @egreg You could redefine \operatorname and that all those \sin translate to \operatorname{sin}, etc. – Manuel Dec 9 '16 at 18:37
• @Manuel That's a possibility (it should be \qopname, though). – egreg Dec 9 '16 at 18:38
• Alternatively you could make the i test whether the current font is italic or roman. – Bruno Le Floch Dec 10 '16 at 1:09