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;
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]}

enter image description here

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.

 NotebookRead @ PreviousCell[] /. 
  "c" -> InterpretationBox[
           StyleBox["c", FontColor -> RGBColor[1, 0, 0]], 

enter image description here

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*)

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


select cell and click the button on palette:

enter image description here

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 ;)

enter image description here


  • better pattern for extracting valid symbol names
  • additional directives

panel definition

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

  data = NotebookRead@cell;

  content = data[[1]];

  symbols = 
     s_String /; 
       s, _?LowerCaseQ ~~ LetterCharacter ...], \[Infinity]];
  color[_] = RGBColor[0, 0, 0];
        With[{sym = sym},
         {Style[sym, FontColor -> Dynamic[color[sym]], 15], 
        {sym, symbols}
       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 -> 
          InterpretationBox[StyleBox[name, FontColor -> col], val])];

    highlight[data_] := data /. (rule /@ symbols);
  • $\begingroup$ 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? $\endgroup$
    – Sumit
    Jul 30 '15 at 10:35
  • $\begingroup$ 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 . $\endgroup$
    – Sumit
    Jul 30 '15 at 11:12
  • $\begingroup$ magnifico @Kuba , (=U) [this is supposed to be a jawdrop with wide eyes] $\endgroup$
    – Sumit
    Jul 30 '15 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.