Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 12 down vote accepted

$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]

enter image description here

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]

enter image description here

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

share|improve this answer
    
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
add comment

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]

enter image description here

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:

enter image description here

And, if you haven't reset $Post=. from what I showed above, the output will be:

enter image description here

share|improve this answer
    
Wow Kuba, $Post is great stuff :). This is no ordinary plotting ;). –  Jacob Akkerboom Jul 19 '13 at 23:25
    
@JacobAkkerboom Quite interesting, but I suspect it is going to make some troubles when overdosed :) First faced here –  Kuba Jul 19 '13 at 23:33
add comment

Your Answer

 
discard

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.