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?

  • $\begingroup$ +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 :) $\endgroup$ Commented Mar 4, 2014 at 11:42
  • 1
    $\begingroup$ 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.) $\endgroup$
    – akater
    Commented Mar 4, 2014 at 12:12
  • $\begingroup$ That would be interesting :) $\endgroup$ Commented Mar 4, 2014 at 12:16
  • 1
    $\begingroup$ 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? $\endgroup$
    – halirutan
    Commented Mar 4, 2014 at 15:51
  • 1
    $\begingroup$ @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.. $\endgroup$
    – halirutan
    Commented Mar 5, 2014 at 0:19

1 Answer 1


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).

  • $\begingroup$ 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. $\endgroup$
    – akater
    Commented Mar 4, 2014 at 11:59
  • $\begingroup$ …and I think it's even possible to manage Blank vs BlankSeqeunce behaviour automatically, so there are no drawbacks, after all. $\endgroup$
    – akater
    Commented Mar 4, 2014 at 12:03

Your Answer

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

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