# How can I make Which format its output?

Functions like Piecewise have cool, easy-to-read formatting. In the same vein, I think it would be wonderful if the Which function automatically returned output in a column or tabulated form:

 Which[a < b,     1,
c < d,     2,
True,      3]


rather than the what currently happens, which can sometimes be difficult to read.

Which[a < b, 1, c < d, 2, True, 3]


This example happens to be easy to read, but for Which expressions with a large number of <condition, value> pairs, it would be nice to have a structured layout for the output.

Any suggestions as to how to make this happen automatically?

-

$Post is handy but it can get confusing when you want to use it for many things at once. I propose using MakeBoxes for this kind of thing as it is specifically intended for specifying formatted (Box) output. Interpretation is used to make the output work correctly as input. The right-hand-side of the definition can be either explicit *Box expressions or you can use ToBoxes to convert "normal" expressions to box form. MakeBoxes[p : Which[x__], fmt_] := Interpretation[ Row @ {Which, MatrixForm @ Partition[Defer /@ Unevaluated[{x}], 2]}, p ] ~ToBoxes~ fmt Which[a < b, 1, c < d, 2, True, 3]  Alternatively with square brackets: MakeBoxes[p : Which[x__], fmt_] := Interpretation[ Style[HoldForm[Which][Grid @ Partition[List @@ Defer /@ Hold[x], 2]], SpanMaxSize -> ∞], p ] ~ToBoxes~ fmt Which[a < b, Print@1, c < d, Print@2, True, Print@3]  (Incidentally List @@ Defer /@ Hold[x] and Defer /@ Unevaluated[{x}] are two ways to do the same thing; take your pick for which is easier to read.) - Very cool - especially the square bracket version. One wonders why this is not built-in to Mma? ... at least for 30 rows or less. – wolfies Jul 20 '13 at 19:54 @Rojo Please let me know if you see any other/new problems with my current code. – Mr.Wizard Jul 21 '13 at 2:19 When I want to modify an output then first thing that comes to mind is $Post.
$Post = If[Head[#] === Which, Row[{Which, MatrixForm@Partition[List @@ #, 2]}], #] &; Which[a < b, 1, c < d, 2, True, 3]  Moreover, there is nothing to prevent you from using the 2D input interface with Which. You only have to be aware what you are writing. So using standard Crtl+, with Crtl+Enter you can write like this: And, if you haven't reset $Post=. from what I showed above, the output will be:
Wow Kuba, \$Post is great stuff :). This is no ordinary plotting ;). –  Jacob Akkerboom Jul 19 '13 at 23:25