11
$\begingroup$

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!

$\endgroup$
1
  • $\begingroup$ I tried to change that setting and it crashed Mathematica. :-O $\endgroup$
    – Mr.Wizard
    Mar 6, 2012 at 9:48

1 Answer 1

10
$\begingroup$

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.
$\endgroup$
5
  • $\begingroup$ Will this code be executed every time I load that notebook? $\endgroup$
    – rcollyer
    Mar 6, 2012 at 19:44
  • $\begingroup$ Based on John's description the answer would be yes providing you are using this newly created stylesheet. $\endgroup$ Mar 6, 2012 at 22:20
  • $\begingroup$ 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... $\endgroup$
    – Rojo
    Mar 6, 2012 at 22:40
  • $\begingroup$ @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). $\endgroup$
    – John Fultz
    Mar 9, 2012 at 7:59
  • $\begingroup$ 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. $\endgroup$
    – John Fultz
    Mar 9, 2012 at 8:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.