# How to change the font color of a variable in whole input cell

I want to track the occurrence of a particular variable in a code. So the idea is to change its color. Usually I do it in a crude way - convert the code into text and then use StringReplace. For example say this is my code,

c = k;
p = a x + b y + c z;
q = b x + c y + a z;
r = c x + a y + b z;

and I want to track the variable b and c. So this is what I do

var1 = "c"; col1 = Red;
var2 = "b"; col2 = Blue;
StringReplace[
"
c=k;
p=a x +b y +c z;
q=b x+c y +a z;
r=c x+ a y+b z;
",
{var1 -> Style[var1, Bold, col1],
var2 -> Style[var2, Bold, col2]}
]

Not very neat, but does the job. And I can't get rid of the ~ when I use Style.

Is there a better way to do this?

You can use this to create a functionality which will fit your need the best.

Here's how you can preview your input cell with c highlighted Red.

CellPrint[
"c" -> InterpretationBox[
StyleBox["c", FontColor -> RGBColor[1, 0, 0]],
c
]
]

You can even evaluate such cell.

### General solution

works quite well, based on pallete but you can put it as a shortcut ofc:

(*first evaluate panel definition, it is at the bottom for post readability*)

CreatePalette[
Button["CellHighlight",
Module[{cell = Quiet[First@SelectedCells[InputNotebook[]]]},
If[MatchQ[cell, _CellObject],
CreateDialog[panel[cell]],
Print["select the cell first"]]
],
Method -> "Queued",
ImageSize -> (3 CurrentValue["DefaultButtonSize"][[;; , 1]])
]
] /. DownValues[panel]

### usage

select cell and click the button on palette:

For cases like yours it you will get fully functional code. For more complex cases, with some scoping structures, I don't know. But looks nice ;)

### ToDo

• better pattern for extracting valid symbol names

### panel definition

panel[cell_] :=
DynamicModule[{color, rule, highlight, data, content, symbols},

content = data[[1]];

symbols =
Union@Cases[content,
s_String /;
StringMatchQ[
s, _?LowerCaseQ ~~ LetterCharacter ...], \[Infinity]];
color[_] = RGBColor[0, 0, 0];
Panel@Grid[{{
Grid@Table[
With[{sym = sym},
{Style[sym, FontColor -> Dynamic[color[sym]], 15],
ColorSlider[Dynamic[color[sym]]]}
]
,
{sym, symbols}
]
,
Button["PreviewCell",
CreateDocument@List@ReplacePart[data, 1 -> highlight[content]],
Method -> "Queued",
ImageSize -> CurrentValue["DefaultButtonSize"]]
}}, Alignment -> Top]
,
Initialization :> (
rule[symbol_String] := With[{
name = symbol,
col = color[symbol],
val = RawBoxes[symbol]},
(name ->
ToExpression@
MakeBoxes@
InterpretationBox[StyleBox[name, FontColor -> col], val])];

highlight[data_] := data /. (rule /@ symbols);
)
]
• Thanks @Kuba. This works perfectly with my present code. As I mentioned in the question, I was looking for a way to spot a particular variable in entire code (helps a lot when troubleshooting). So I want to assign a particular style to it and then I can find its every occurrence just by looking. Can there be a more general approach for it? – Sumit Jul 30 '15 at 10:35
• I am wanting it very much . Can you give an example for that! I can modify my question accordingly if needed. I guess all the variables can be found with a Variable or with a proper Head at some Level (or I can put a list of them in beginning - good old fortran style). I leave that completely on you :D . – Sumit Jul 30 '15 at 11:12
• magnifico @Kuba , (=U) [this is supposed to be a jawdrop with wide eyes] – Sumit Jul 30 '15 at 14:15