Syntax Coloring In Mathematica

2019-03-11 05:39发布

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.

3条回答
神经病院院长
2楼-- · 2019-03-11 06:27

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?

查看更多
Evening l夕情丶
3楼-- · 2019-03-11 06:32

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").

查看更多
劫难
4楼-- · 2019-03-11 06:35

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.

查看更多
登录 后发表回答