# Tag Info

65

What you want is SyntaxInformation. With this, you can use every highlighting which already exists for things like Table, Solve, etc. for your own functions. You can specify the pattern of the arguments. With this you get the typical red commas if you use too many parameters. Or you can highlight locally used variables inside the function-arguments: ...

33

As a part of a larger sets of development tools which I am working on currently, I have developed a general syntax highlighter generator which does just that (not yet with styles though, this is coming). I wanted to put in on GitHub and do a bit more polishing / development, but since you asked the question, here goes. Features From a simple lexical ...

32

The colors used by the syntax highlighter can be set by changing the styles for StandardForm. The following is a how-to that explains how I styled the input cell in the screenshot in the question. This should be a starting point to get your own custom highlighting scheme up and running. Note that you can also do the same by choosing the colours in ...

31

The default style sheets set ShowSyntaxStyles -> False for the "Printout" environment. You could change the notebook to use a style sheet that doesn't set this. Probably the easiest way is to copy the definition from Default.nb, and modify it: Cell[StyleData[All, "Printout"], ShowSyntaxStyles->True]

26

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[] ...

16

This is because Mathematica exports to PDF in the Printout screen environment. If you change this to the working environment, it'll keep the syntax highlighting, but will also make everything a bit bigger: SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"] You can also change this setting from the GUI using Format -> Option Inspector... ... 16 A bit of historic background Before Mathematica version 6 graphics were produced as a side-effect much as Print works now. In fact you can load this old system using: << Version5Graphics Now you get this behavior: Note that the output is - Graphics - and the plot itself is handled like Print. Since there was often little value in having - ... 13 You can get the syntax highlighting that you desire by modifying your UnicodeCharacters.tr file, though I don't know how advisable this practice is. For example, adding: 0x20B0 \[PennyOp] ($penny$) Infix 155 None 5 5 I can use EscpennyEsc to enter: I am not aware of documentation of the format of this file but as ... 12 Available messages The message ::argx is one of the general messages intended for use with any function. These have the special property of being called for any symbol used (placed left of ::): Message[foo::"argx", "foo", 2, 3] foo::argx: foo called with 2 arguments; 1 argument is expected. >> Use Messages[General] to see a list of these messages. ... 12 When you're writing a blog, then I assume you take some content management system like Joomla because otherwise a blog is maybe to hard to keep up-to-date in pure html. I have personal experience with Joomla and know, that code-highlighting as described below works great. As mentioned by J.M. we have worked and tested hard to get a highlighter running for ... 12 It's not a syntax mistake. Consider it a suggestion, FrontEnd tells you: "maybe you want to plot it as it has no much sense now". Of course it may have sense, it's just a suggestion. If you take a closer look you will find that storing such Plot makes more sense for FE: 11 As @R.M. explained, the styling rules are stored in the AutoStyleOptions setting. There is already a built-in editor for the code highlighting style, that can be accessed through Edit -> Preferences... -> Apperance -> Syntax Coloring. This changes the styles globally though. My suggestion is to first edit the global styling (because the built-in ... 10 I don't know what is causing your problem, but presumably you can still specially color the symbols in those contexts as I do. For example: SetOptions[$FrontEnd, AutoStyleOptions -> {"SymbolContextStyles" -> {"Units" -> Brown, "PhysicalConstants" -> Orange} } ] Since the above suggestion doesn't have effect on your machine even ...

9

As acl points out, this post shows you how to setup error highlighting for invalid number of arguments. Coming to the actual error messages used, there are three built-in messages attached to General, that can be used for your own functions as well. These are argx, argrx and argt: General::argx (* "1 called with 2 arguments; 1 argument is expected." *) ...

9

This is because you are using = (the assignment operator) in the condition (not the body) of While. It is a typical beginner mistake to use = where == is meant, so Mathematica warns about this. Since you also use several ; in the condition, it gets a little confused and only highlights one of the = signs, not all of them.

9

You can do this using the following code: CurrentValue[EvaluationNotebook[], {AutoStyleOptions, "UndefinedSymbolStyle"}] = {FontColor -> RGBColor[0.5, 0.173, 0.765]} More style options (other than "UndefinedSymbolStyle") that can be set are: {"CommentStyle", "EmphasizedSyntaxErrorStyle", "ExcessArgumentStyle", "FormattingErrorStyle", ...

9

To prevent your second invocation of SetOptions from resetting the value of sub-option "SymbolContextStyles", you need to set both "System" and "Global" sub-sub-option values at once: SetOptions[EvaluationNotebook[], "AutoStyleOptions" -> {"SymbolContextStyles" -> {"System" -> RGBColor[211./255, 54./255, ...

8

I don't know whether I interpret your question correctly but have you checked Preferences->Appearance? There you see what the colors of the syntax highlighter mean:

7

Instead of using the Notation package, you can achieve the translation by doing the following: MakeExpression[RowBox[{x_, "⟗", y_}], StandardForm] := MakeExpression[ RowBox[{"FlatJoin", "[", x, ",", y, "]"}], StandardForm ] This takes care of the input translation. Now it's possible to enter expressions like 1 ⟗ (3 + 4 ⟗ 2) and have ...

7

The difference in color depends on whether or not it is a recognized symbol: black indicates it is recognized, blue indicates that it is not. For a symbol to be recognized, it has had to have been input to the kernel, i.e. it has to have been included in an executed cell. I have had problems in the past when quitting and restarting the kernel where the ...

7

The FE only looks at the structure of your code for colouring. It doesn't evaluate anything. This means two things: (1) it can only guess that there might be a problem, because by looking at the structure, it doesn't know whether your code really evaluates to something you might not want. (2) You can easily trick the FE by changing the structure into ...

6

I remember being confused by this when I first started out. As rcollyer notes, black means that it is either a command or a variable that contains information, blue means that it is not recognized at all. Try this: testVariable="5" When you execute it, testVariable will turn from blue to black. Now, if you're coding and need to call on testVariable, it ...

6

I think it's just a warning that you have a symbol inside a localization scope that contains a symbol of the same (base) name. Compare With[{max = 2}, {With[{max = 3}, max], max}] Module[{max}, max = 2; {Module[{max}, max = 3], max}] Block[{max}, max = 2; {Module[{max}, max = 3], max}] and so on. The outputs above are each {3,2} and no messages are ...

6

I hope you don't mind if I answer this in a larger context and you have to read a bit. What you observe here is one disadvantage of the otherwise very nice Mathematica language. Since Mathematica follows the paradigm everything is an expression and code-is-data it is a mess when it comes to features that help programmers to write correct code or make ...

6

I think the better solution that I find so far is shown as below: SetOptions[SelectedNotebook[], PrintingStyleEnvironment -> "Printout", ShowSyntaxStyles -> True]

6

This syntax coloring bug [264325] was fixed in Mathematica 10.0.2:

5

Personally I'm always disappointed by the quality of the conversion done when one simply saves a notebook as a TeX file--it never looks like what I want or expected. But perhaps this isn't too surprising, as the two file types are very different, and therefore many choices need to be made about the conversion. I think a good general approach for solving ...

5

Until (when, WRI??) there will be an update of WWB (which certainly will also fix other shortcomings and bugs) just use Block to get the coloring right:

5

I don't know if this is possible for the Input - style cells, but it is certainly possible for the Program-style cells with the syntax highlighter generator I exposed in a recent answer. The work in that direction is underway, but I don't have a complete package at the moment. In fact, apart from code highlighting for languages like C, Java, etc, a strong ...

5

An editor can only do this if it has enough understanding of Mathematica code to be able to tell which symbols are localized to what scopes. This is far from trivial, so there aren't many tools that can do this. To my knowledge the Mathematica Front End does not do this type of highlighting (even though it does know what symbols are local and indicates ...

Only top voted, non community-wiki answers of a minimum length are eligible