# Latex Equations: Colour all instances of symbol

I (and most humans) find equations a lot easier to read when certain variables are highlighted. For example:

$$0 = \frac{\partial}{\partial\color{blue}{z_l}}\big(\|h(z_{l-1})\cdot w_l-\color{blue}{z_l}\| + \lambda \| h(\color{blue}{z_l})\cdot w_{l+1} - z_{l+1}\| \big)$$


Gives a visually appealing:

My question: Is there some way to do this without explicitly filling the equation with these \color commands? Ideally, I would not have to change the equations at all, and could just define a block around the equation that takes care of the colouring. I'm looking for something like defining a block of code where you automatically replace all instances of z_l with \color{blue}{z_l}.

To be clear, I am aware of the "Find -> Replace" function of most editors, but this isn't what I'm looking for - I want to be decorate my equations without messing up the equation code itself.

(edited/simplified the code after OP clarified that only z_l instances need to be colored for now)

You could achieve your objective by using LuaLaTeX and taking a preprocessor-based approach, as is done in the following example.

% !TeX program = lualatex
\documentclass[letterpaper]{article}
\usepackage{xcolor,mathtools}
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}

\usepackage{luacode}
% The lua function 'color_zl' does the actual work
\begin{luacode}
function color_zl ( s )
s = string.gsub ( s , "z_l" , "\\textcolor{blue}{%0}" )
return s
end
\end{luacode}

% The lua function is assigned to 'process_input_buffer' callback:
"process_input_buffer", color_zl , "color_zl" )}}
\newcommand{\ZColorOff}{\directlua{luatexbase.remove_from_callback(
"process_input_buffer", "color_zl" )}}

\begin{document}
\ZColorOn % turn on automatic coloring of "z_l" terms
$0 = \frac{\partial}{\partial z_l}\bigl( \norm[\big]{h(z_{l-1})\cdot w_l-z_l} + \lambda \norm[\big]{h(z_l)\cdot w_{l+1} - z_{l+1}} \bigr)$

\ZColorOff % turn off automatic coloring of "z_l" terms
$0 = \frac{\partial}{\partial z_l}\bigl( \norm[\big]{h(z_{l-1})\cdot w_l-z_l} + \lambda \norm[\big]{h(z_l)\cdot w_{l+1} - z_{l+1}} \bigr)$
\end{document}


Addendum If you wanted to typeset in blue all instances of z with any subscript, not just z_l, you should modify the Lua function by enabling a couple of pattern-matching operations:

function color_zl ( s )
s = string.gsub ( s , "z_%b{}" , "\\textcolor{blue}{%0}" )
s = string.gsub ( s , "z_(%w)" , "\\textcolor{blue}{%0}" )
return s
end


The first pattern, %b{}, matches a matching pair of curly braces with any content. The second pattern, %w, matches a single alphanumeric character, e.g., l, 1, etc.

• Nice! Do you know if this can be done in some kind of environment (something like \begin{ZColor} ... \end{ZColor})? – Peter Feb 10 '17 at 11:55
• @Peter - Certainly! Just add the following code to the preamble: \newenviroment{ZColor}{\ZColorOn}{\ZColorOff} – Mico Feb 10 '17 at 12:06

Just try to make your own definition for that. As a starting point a example for your case:

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

\def\zl{{\ensuremath{\color{blue} z_l}}}

\begin{document}
$0 = \frac{\partial}{\partial\zl}\big(\|h(z_{l-1})\cdot w_l-\zl\| + \lambda \| h(\zl)\cdot w_{l+1} - z_{l+1}\| \big)$
\end{document}

• It's not bad but it still requires modifying the equation - making the equation code unportable. Also I would only like to do this in a context - I may not want to have all instances of z_l go blue in the entire document. – Peter Feb 10 '17 at 11:54
• @Peter You can do this def right before the equation and copy it to another file too (it's portable). If you later on want to have it back in black just do a \def\zl{\ensuremath z_l}. – TeXnician Feb 10 '17 at 11:55

You do not have to have markup (or Lua) to colour math tokens for example

\documentclass{article}
\usepackage{color}

\mathchardef\zmath\mathcodez
\mathcodez="8000

{\makeatletter
\def~{\@ifnextchar_{\zzm}{\zmath}}
\catcodez\active
\global\let z~
}

\def\zzl{l}
\def\zzm_#1{%
\def\tmp{#1}%
\ifx\tmp\zzl
\textcolor{blue}{\zmath_{l}}%
\else
\zmath_{#1}%
\fi}

\begin{document}
$0 = \frac{\partial}{\partial z_l}\bigl(\|h(z_{l-1})\cdot w_l-z_l\| + \lambda \| z_l)\cdot w_{l+1} - z_{l+1}\| \big)$
\end{document}

• Thanks, this seems to work, although I'm a bit baffled as to what it's doing. – Peter Feb 12 '17 at 21:12

I was quite fed up with writing X_{\mathit{y}} to get proper index y following the variable X. You seem to have simillar problem. My solution, adapted to your case, was:

\newcommand[2][blue]{\foo}{{\ensuremath{\color{#1}#2}}}


And usage is simple:

Variable \foo{b} is emphasized blue and variable \foo[red]{r} is emphasized red.

• Your opening sentence is quite confusing: X_y and X_{\mathit{y}} should produce the exact same output, no? Also, the OP wrote that "I want to ... decorate my equations without messing up the equation code itself". Wouldn't replacing all instances of z_l with \foo{x_l}` constitute an example of "messing up the equation code itself"? – Mico Feb 10 '17 at 13:19