Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I need to evaluate

Needs["GraphUtilities`"]

before I can use functions such as GraphPath[]. When I do so, some functions get colored red, but still work.

Why does this happen and can I change this?

share|improve this question
1  
I think its worth to mention that Mathematica sometimes, in specific cases, mistakenly colors variables red, though they are not shadowed. It just means that the syntax-checker is not perfect. –  István Zachar Mar 21 '12 at 9:19
2  
@IstvánZachar That's not the same red as shadowing — that usually happens if the scope of a variable is ambiguous or there's a possible collision. Try f[x_] := Module[{x}, x]. You can actually change these colours in Preferences > Appearance > Syntax colouring > Errors and warnings. By default they're all the same damn shade of red (I have different ones for each). CHM, to answer the last line of your question, the above is where you can change this colour. –  rm -rf Mar 21 '12 at 14:03
    
Thanks for the clarification! Though never really payed attention to these, now I have the means to suppress them. –  István Zachar Mar 21 '12 at 14:39
    
@CHM: which functions are colored red? I seem to recall that sometimes this coloring is just an oversight in declaring some names for packages (at least that's what I remember somebody from WRI responding elsewhere to a query of the same sort). –  murray Mar 21 '12 at 19:22

1 Answer 1

up vote 24 down vote accepted

The red colouring indicates shadowing — i.e., when a symbol originally in a particular context, is exposed to the current context path, thereby clashing with another symbol of the same name in a different context, also on the context path.

Example of shadowing:

Here is a short example that demonstrates this. Try it out in a fresh kernel (call Quit[] before you try it).

(* Define f in the test` context *)
Begin["test`"]
    f[x_] := x^2
End[]

(* Define f in the Global` context *)
f[y_] := y^3

So far, all's well and you see the symbols coloured the way you're used to. Now expose test`f by adding it to the context path with:

$ContextPath = Prepend[$ContextPath, "test`"]

You'll now notice that both the f turned red to indicate that it is being shadowed by a different definition.

For more information on this, you can read about shadowing in the documentation and also this article by David Wagner in the Mathematica journal.

Which definition is used in case of a conflict?

The definition that is used first, in the case of shadowing, is the one belonging to the context that appears first in $ContextPath. This is the same behaviour as in your unix shells, where directories in the $PATH variable are searched in the order they appear.

In the above example, I used Prepend, which would've placed test` first in the list. So the definition of f will be test`f and you can easily test this:

f[x]
Out[1]= x^2

Now quit your kernel and do the same, except now you place the new context at the end. You can see that the definition used is that of Global`f.

$ContextPath = Append[$ContextPath, "test`"]
f[x]
Out[2]= x^3

Incidentally, it is also possible to trigger this shadowing warning (red colour) even if there isn't a real conflict. This can occur if the same context appears at different positions in $ContextPath. To test this, in a clean kernel, define test`f as before, and Append and Prepend it to $ContextPath.

Changing the colour of the warning

To change the colour from red to your favourite, go to Preferences > Appearance > Syntax Coloring > Errors and Warnings and change the colour in the very last option:

enter image description here

To do it programmatically, evaluate:

SetOptions[$FrontEndSession, AutoStyleOptions -> {"SymbolShadowingStyle" -> 
    {FontColor -> Orange}}]

Replace Orange with the color of your choice and $FrontEndSession with $FrontEnd if you want the change to persist across future sessions (it essentially writes it to the FE's init.m). See this answer for more on the various style tokens and what they control.

share|improve this answer
1  
Nice touch to link to the Mathematica Journal. –  Yves Klett Mar 21 '12 at 7:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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