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

Does anybody have a good way to automate the setting of SyntaxInformation for any custom function? I have a ton of functions for which I want to set SyntaxInformation, and doing it by hand seems a little pointless. Here's what I've come up with so far:

SetSyntaxInformation[symbol_Symbol] := 
  Module[{argcounts, transposecounts, argpatt},
   argcounts = With[{sym = symbol},
      HoldPattern[Verbatim[HoldPattern][sym[args___]] :> _] :> CountArguments[args]
   transposecounts = Transpose@PadRight[argcounts];
   argpatt = ArgumentCountToPattern@Union[#] & /@ transposecounts;
   SyntaxInformation[symbol] = {"ArgumentsPattern" -> argpatt}


CountArguments[args___] := CountArgument /@ {args};
CountArgument[Verbatim[PatternTest][pat_, _]] := CountArgument[pat];
CountArgument[Verbatim[Pattern][_,(Verbatim[Blank] | _)[___]]] = 1;
CountArgument[Verbatim[Pattern][_,Verbatim[BlankSequence][___]]] = Sequence[1,Infinity];

This attempts to count the number of arguments for each downvalue of a given symbol, and turns this into a "ArgumentsPattern" type list which is subsequently set. Here's an example:

F[x_] := x^2
F[x_, y_] := x^2 + y^2

(* {"ArgumentsPattern" -> {_, _.}} *)

Although this seems to get the job done, the code seems a little bit unwieldy and might not be that robust, as it's certainly missing a few cases (like e.g. options).

So my question is: is there any better way to tackle this problem? (And does anybody have something similar for setting argx messages?)

share|improve this question
Related question. – Leonid Shifrin Jul 5 '13 at 15:29
@LeonidShifrin Thanks, I was unaware of that question. Having a good way to count the number of function arguments is half the work. – Teake Nutma Jul 5 '13 at 17:59

Your Answer


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

Browse other questions tagged or ask your own question.