I've been playing around a lot with making a new style sheet with corresponding syntax colorings in the past few days, but I'm finding the default options pretty lacking. While you can use the preferences menu to set comment colors, string colors, undefined symbol colors, and local variable colors, there is no way to differentiate between, say, variables and functions, which is what I'm ultimately looking for in a color scheme. At the heart of the matter, I essentially want to be able to set syntax specific coloring for a defined term (whether it be Plot or a variable) based on the Head of that term.

As a concrete example, let's say I define a = {1,2,3} and b = 2. Is there any way to have the variable a be colored red (as it is a List head) wherever it appears, the variable b be colored blue (as it is an Integer head) wherever it appears, and the function Plot be colored green (as it is a Symbol head) wherever it appears.

Edit: After more digging I've found this answer: https://stackoverflow.com/questions/6130514/syntax-coloring-in-mathematica, which gets quite close to what I'm looking for - I guess the natural follow-up is if it is possible to set an object's context programmatically by it's Head (and here my lack of deep Mathematica knowledge is probably showing - this might not even be a sensical question).

  • $\begingroup$ In Mathematica objects can be both functions and symbols. I think the next-best thing to ask for is for a different coloring for the built-ins vs user-defined objects. I remember seeing this possibility somewhere on this site... $\endgroup$
    – QuantumDot
    Sep 6, 2019 at 6:09
  • $\begingroup$ Right, but I figure if I can make Head-defined syntax coloring I'll be able to fine tune to my aesthetic needs. I'll look for that other post as well! $\endgroup$ Sep 6, 2019 at 6:29
  • 3
    $\begingroup$ Putting discussion about 'categories of symbols' you are talking about, no, there is not way to do this unless you implement your own highlighter. And since there is no way to neatly attach own highlighters (there are ways to do this but not efficient), I suggest to abandon this idea. P.s. you do not want to change symbol's context as soon as it gets 'different type' of value. red`a is just a different symbol than blue`a and it will only lead to troubles. $\endgroup$
    – Kuba
    Sep 6, 2019 at 13:04
  • $\begingroup$ You can color specific symbols pretty easily, using one of the AutoStyleOptions but it’s pretty much impossible to do classes of Symbols unless you do some expensive work with Dynamic that’ll blow up in usage complexity and be very fragile. $\endgroup$
    – b3m2a1
    Sep 7, 2019 at 20:13


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.