How does AutoStyleWords work?

11

6

The title says it all. I assume it provides some way of automatically applying a certain style, or a list of options, to certain words written, just like other options of the sort like AutoItalicWords. But I haven't been able to make it work...

Thanks!

Rojo

Posted 2012-03-06T07:14:10.447

Reputation: 40 993

I tried to change that setting and it crashed Mathematica. :-O – Mr.Wizard – 2012-03-06T09:48:32.793

Answers

10

A very similar question came up internally at WRI, so I have a nearly ready-made answer. In that case, the fellow wanted to highlight certain loop constructs, like Do and For in his code automatically. Here's how I responded.

In a fresh notebook, Format->Edit Stylesheet..., then paste and interpret the cells below at the bottom of the stylesheet. And voila, you'll got purple Dos and Fors in the notebook the stylesheet modified.

{
Cell[StyleData["Input"],
 AutoStyleWords->{"Do"->"MyStyle", "For"->"MyStyle"}],

Cell[StyleData["MyStyle"],
 FontColor->RGBColor[0.5, 0, 0.5]]
}

Some caveats about using this:

  • The thing on the rhs of the rule must be a named style (a slightly archaic and embarrassing limitation in a modern FE, but that's the way it is in v8).
  • There's a bug (fixed for future versions) in the validation of this option which can cause a crash if you feed it values formatted in any way other than this.
  • This will only work in typeset cells
  • This will only work to style things which are lexically word-type tokens. You cannot, for example, auto-style two words in sequence, a subexpression with an operator, or a substring of a word token.

John Fultz

Posted 2012-03-06T07:14:10.447

Reputation: 12 166

Will this code be executed every time I load that notebook? – rcollyer – 2012-03-06T19:44:06.340

Based on John's description the answer would be yes providing you are using this newly created stylesheet. – Mike Honeychurch – 2012-03-06T22:20:06.987

Thanks John!, @MikeHoneychurch, I don't "think" there's probably anything special about a newly created stylesheet. He probably said it so no other "normal" issues can prevent this from working, since I said that I couldn't make it work. The key was that it only works with typeset cells. Furthermore, the Options Inspector pops up an editor for the values that is TOTALLY misleading, asking a key-password pair and returning the result as a list of pairs. Add this to the bug... – Rojo – 2012-03-06T22:40:53.077

@Rojo, yes, the fact that key-password pair should have worked but crashed instead was part of the bug that I fixed (and there were various other issues....this turned out to be a pretty rarely used portion of the code, so it had been busted for a few years w/o anybody noticing). – John Fultz – 2012-03-09T07:59:17.527

Rojo is correct that this doesn't have to be in a newly created stylesheet...you could set it in the OI at the notebook or global level, or add it to a shared stylesheet. What I'm doing here is providing a specific formula that any newbie can follow and make work. This, like any other option, works in various and predictable manners which I'm happy to elucidate on in other questions where the issue would be on topic. And, to clarify...it's not "executing code"...it's merely an option setting like Background or FontColor or any other. – John Fultz – 2012-03-09T08:02:15.650