Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose we defined a pattern with custom pattern name provided by user, like

Quiet[myCustomPattern@terms_ := f[terms : (_[_, _])..]]

for use in further definitions:

myTransform@myCustomPattern@localNameForTerms := Reverse /@ {localNameForTerms}

However, in definitions like this one, localNameForTerms is not by default highlighted the way it would be in case of appearing in explicit Pattern expression. Is it possible to Style it as a properly localized symbol, by putting

myCustomPattern -> Composition[myCustomPattern, Style[#, Darker@Darker@Green, Italic] &]

somewhere in the low-level notebook representation rules, for example?

share|improve this question
+1 for showing us that you can ignore the warning of patterns appearing on the RHS. Did you find this out yourself? Anyway it is a bit too crazy to be practical I'd say :) – Jacob Akkerboom Mar 4 '14 at 11:42
I guess I found that out myself but it's not really counter-intuitive. There is no real reason to restrict such pattern generation. This is definitely not the first time I place a Quiet to mute a warning of this kind. Maybe I'll post an example of how it could be useful. (If the description won't turn out to be bloated.) – Akater Mar 4 '14 at 12:12
That would be interesting :) – Jacob Akkerboom Mar 4 '14 at 12:16
Please before you move on with this, take a step back and reconsider: Is it really constructive if you change your code just to circumvent issues in the highlighter and get a proper colouring? Some very helpful constructs like Function[, #^2, {Listable}] are coloured incorrectly. Does this mean I shouldn't use them to circumvent bugs in the highlighter? – halirutan Mar 4 '14 at 15:51
@JacobAkkerboom No, no such list exists, but you can think of the highlighter in Mathematica (and IDEA too) as kind of pattern matcher. If for instance the your Module code does not exactly match the form Module[{..},..] the highlighting will go wrong. Most basic example is (Module)[{x,y},x+y]. From knowing this, you can create your own crazy examples. Unfortunately, some of the crazy highlighter-breakers are not crazy at all, but are used very often. Think for instance about a Table, where the iterators don't have the exact form of {i,..} but were created like this.. – halirutan Mar 5 '14 at 0:19
up vote 1 down vote accepted

Crazyness! Ok so this behaviour relies on something like x = s_; f[x] := s behaving like you how you want it to. I must say I am guilty of making crazy definitions myself. Personally I don't like leaving any evaluation on the left hand side up to SetDelayed in cases like these. Anyway, you can do the following, but really it becomes only more crazy

ClearAll[myCustomPattern, myTransform, f];
Quiet[myCustomPattern[Verbatim[Pattern][patt_, Verbatim[_]]] := 
  f[patt : (_[_, _]) ..]];
myTransform@myCustomPattern@localNameForTerms_ := 
 Reverse /@ {localNameForTerms};
myTransform // Definition

This outputs


At first I thought Verbatim would not be able to do the job, but I guess surrounding Pattern with Verbatim is a nice trick.

Note that this works correctly even if localNameForTerms has a value. This is important, because if you have for example x=3 and you use instead of localNameforTerm, you might not notice that it already had a value due to the green syntax colouring (as opposed to black vs blue).

share|improve this answer
Oh yes, it's certainly a satisfying solution. I'll accept if no alternatives show up, thank you. Unfortunately myCustomPattern@localNameForTerms_ would then generate a pattern with localNameForTerms standing for BlankSequence while the mark-up corresponds to Blank, but that seems to be a relatively minor drawback. – Akater Mar 4 '14 at 11:59
…and I think it's even possible to manage Blank vs BlankSeqeunce behaviour automatically, so there are no drawbacks, after all. – Akater Mar 4 '14 at 12:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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