13

How could user-defined function symbol such as f in

  • f[x_] = 2 x

or variable symbols such as lotto in

  • lotto = Table[2, {10}];

be colored automatically ?


In Syntax coloring on M8 no option is offered for this.

Only Local Variables or Global symbols that have no values assigned.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
500
  • 6,509
  • 8
  • 46
  • 80
  • It's called syntax coloring, and according to [this page](http://www.mathematica-users.org/webMathematica/wiki/wiki.jsp?pageName=Syntax_Coloring), it's not available in Mathematica 5.2 or earlier. What version are you using? – BlueRaja - Danny Pflughoeft May 25 '11 at 20:36
  • 500, I am not certain of the behavior you desire. Would you please consider extending your question to include exactly what you expect? – Mr.Wizard May 26 '11 at 12:38
  • Do you mean only variables with an assigned value, or is colorizing of any symbol name you create acceptable? (sorry, I don't use either R or Matlab) – Mr.Wizard May 26 '11 at 13:18
  • Just a quick comment: I don't know if you're aware of this, but all built-in Mathematica functions start with a capital letter. So, the convention is to start all user defined functions with a lowercase letter, so as to be able to distinguish easily. – abcd May 29 '11 at 05:31
  • 2
    The correct term is Symbol and these can be variables, function names, option names, and more. Now, do you want to color **only** Symbols that are function names? That will be difficult. Or, do you simply want to color one or more Symbols differently from the rest? That was answered, perhaps unintelligibly, in the prior question. (Please put the answer to this comment in your question itself; I did not vote for the question because I find it unclear.) – Mr.Wizard May 29 '11 at 07:28
  • 2
    I think that this would be difficult to do automatically. You'd have to redefine `Set` and `SetDelayed` to check whether they are creating a `DownValue` (function) or `OwnValue` (constant/variable) and then move the `Symbol` to the right context. It would probably be best to do this manually... – Simon May 29 '11 at 10:17
  • 500, are you still having the shadowing problem illustrated above? This is what I see: http://i.stack.imgur.com/Nqj1i.gif – Mr.Wizard May 30 '11 at 14:41
  • 500, I am removing my comments above that do not relate to this question. I recommend you do the same. – Mr.Wizard May 31 '11 at 10:17

3 Answers3

16

This is not exactly what you asked for, but it may be useful to you.

You can highlight symbols by context, using this method:

SetOptions[$FrontEndSession, 
  AutoStyleOptions -> {"SymbolContextStyles" -> {"highlight`" -> Green}}
]

AppendTo[$ContextPath, "highlight`"];

Now, when you create a symbol in the context highlight` it will automatically be colored green:

highlight`lotto ;

enter image description here

and since highlight` was appended to $ContextPath, after that the symbol can be used plainly:

lotto

enter image description here


Highlighting all symbols (variables)

If you want all the symbols you create to automatically be highlighted, then set:

$Context = "highlight`"

After that, all new symbols you create will belong to the context highlight` and will receive the color.

New means ones that have not been previously used in the session, or have been Removed.

It occurs to me that a better way to accomplish this, that avoids a possible "shadowing" problem, may be to set highlighting for the Global` context itself. I have not considered the ramifications of this, but it can be done with this alone:

SetOptions[$FrontEndSession, 
  AutoStyleOptions -> {"SymbolContextStyles" -> {"Global`" -> Green}}
]

Tips on usage

  • The context highlight` is completely arbitrary, and you can have multiple contexts highlighted with different colors.

  • You can color the contexts of packages such as Units` to distinguish which symbols belong to that package.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • Very useful, especially for non-standard evaluation functions like [mine](http://stackoverflow.com/questions/4983301/executing-code-in-v-5-2-kernel-from-within-v-7-01-session-through-mathlink)! +1 – Alexey Popkov May 26 '11 at 10:40
  • @500 This does what you want? Okay. :-) – Mr.Wizard May 26 '11 at 13:01
  • @Mr. Well, it is acting a bit weird. I created a new notebook, the only one opened pasted you code and after highlight`lotto; which is green, the lotto I type bellow becomes red along with the first one on top. – 500 May 26 '11 at 13:18
  • 1
    @500 that is a sign of "context shadowing" and it indicates that you used (just evaluated somewhere, not even assigned a value to) the symbol `lotto` before you ran that code. Basically, you have two symbols, ``Global`lotto`` and ``highlight`lotto`` and since both ``Global` `` and ``highlight` `` are in `$ContextPath`, Mathematica does not know which to use. If this is confusing to you (which is not surprising) please post a new question so it can be directly addressed. The method that I added setting ``$Context = "highlight`"`` will be less prone to this problem. – Mr.Wizard May 26 '11 at 13:33
  • @500 [This PDF document](http://www.mathematica-journal.com/issue/v6i2/columns/wagner/wagner62.pdf) appears to be a lengthy discussion of the problem. I just found it, so I have not read it, but I expect it is useful, if not too esoteric. Don't struggle with it too long before posting a quesiton if you need to. – Mr.Wizard May 26 '11 at 13:48
  • @500 please see the part in bold near the bottom of my answer. (if you tried that in the last five minutes, there was a mistake; try again. I am quite tired.) – Mr.Wizard May 26 '11 at 14:07
  • +1 Nice answer Mr.W! It would be interesting to combine it with what was discussed in [this question](http://stackoverflow.com/questions/4314225/assumption-for-multiple-variables/4320778#4320778) so that variable names of a certain type (starting with a certain letter etc...) are automatically moved to the right context and highlighted. – Simon May 29 '11 at 10:13
  • @Simon, thank you. That could be a good use for it. I like to highlight the symbols for the Units package because I cannot remember them all. – Mr.Wizard May 29 '11 at 10:31
  • @All, using this wonderful code : SetOptions[$FrontEndSession, AutoStyleOptions -> {"SymbolContextStyles" -> {"Global`" -> RGBColor[.4, .4, 1]}}]; Is there a way such that Names of Functions I have defined (myFunction[blah_]:=2*blah) appear in a different colors compared to Value I have assigned (daList = {1,2,3,4}) ?bAs of now myFunction & daList take the same color. – 500 Aug 24 '11 at 14:55
5

Such variables are automatically colored after you assign a value to them. Under the default Mathematica settings, lotto changes color from blue to black as soon as you assign the value. Strictly speaking, it is the unassigned variables that get colored according to the setting you will find under Preferences / Appearance / Syntax Coloring / Other / Global symbols that have no value assigned.

If what you are looking for is a way to assign a distinct color to global symbols whose only definitions are own-values, then I believe that you are out of luck. As far as I know, the syntax coloring machinery does not distinguish between own-values ("variable assignments"), down-values ("function definitions") and up-values ("expression part definitions").

WReach
  • 18,098
  • 3
  • 49
  • 93
  • If one is determined to implement such a coloring scheme, it would probably be possible to program the front-end to perform arbitrary coloring by altering the cell expressions and/or rendering of input cells. – WReach May 25 '11 at 23:14
3

As I said in my comment above, I think that this would be hard to completely automate. You'd have to wrap Set and SetDelayed to automatically move variables into the right context depending on whether they're creating an OwnValue or a DownValue.

To do this manually just requires a simple edit of Mr.Wizard's previous work...

SetOptions[$FrontEndSession, 
 AutoStyleOptions -> {"SymbolContextStyles" -> 
                       {"functions`" -> Green, "variables`" -> Pink}}]
$ContextPath = Join[$ContextPath, {"functions`", "variables`"}]//DeleteDuplicates;

variables`x;
functions`f;

enter image description here

Is this what you want?

Community
  • 1
  • 1
Simon
  • 14,631
  • 4
  • 41
  • 101
  • @Simon, it is what I want, thank you.I thought I was doing sthing wrong but there seems to be a context shadowing problem. All the names turn into red after the first use :(. I will post the image in my question. – 500 May 30 '11 at 13:01
  • 1
    @500: Try running ``Remove["*`f", "*`x"]`` and then the ``variables`x; functions`f;`` line again. Also try with a new kernel (run `Exit[]`). – Simon May 30 '11 at 13:08
  • @Simon ! `Remove["*`f","*`x"]` works, thank you ! I am not sure how to use a new Kernel yet but will look into it ! – 500 May 30 '11 at 13:22
  • Would you think about a way to use solutions from there [List all Variables & Functions user-defined] (http://stackoverflow.com/questions/6166027/list-all-user-defined-variables-functions-in-a-notebook-in-mathematica) to make this automatic ? – 500 May 30 '11 at 13:28
  • @Simon, I am sorry, I can`t get my comment format right yet :( – 500 May 30 '11 at 13:30
  • @500: Mathematica is built from a frontend (the notebook interface) and a kernel (the thing that does most of the real work). The frontend sends your inputs to the kernel using MathLink. By restarting the kernel (using `Exit[]` or the menus) it's like you just opened up the notebook but haven't run any of the input cells yet. – Simon May 30 '11 at 22:30
  • @500: In [List all User-defined Variables & Functions in a Notebook in Mathematica](http://stackoverflow.com/questions/6166027/list-all-user-defined-variables-functions-in-a-notebook-in-mathematica), what do you mean by "variable"? Is it a symbol with no associated definitions? – Simon May 30 '11 at 22:34
  • @Simon: Not that it matters much. It would be impossible to tell an undefined function and its argument apart if you just typed in `f[x]` - say in differential equation that you wanted to solve `f'[x]+f[x]==0`... – Simon May 30 '11 at 22:46