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.

How do I set a TraditionalForm output for a particular symbol/function?

In particular I would like my user-defined symbol pvB[n,P,x,s,m0,m1] to have an output that looks like $B_{0\ldots0 1\ldots1}(s,m0,m1)$. Here, the number of $0$'s in the subscript is $n$ and the number of $1$'s in the subscript is $P-n$. The third parameter shouldn't appear in the output, and $s$, $m0$, $m1$ are just arguments. (Of course, if this appears as part of a larger expression, I need to have it be formatted appropriately).

So for example:

TraditionalForm[pvB[2,4,x,s,m0,m1]]

should give

$B_{0011}(s,m0,m1)$.

How can I achieve this?

share|improve this question
    
In addition to the answers below this technique and discussion be useful: mathematica.stackexchange.com/questions/15058/… –  Mike Honeychurch Jan 11 '13 at 21:53
    
QuantumDot, any comments on my answer? It seems like the simplest way to proceed to me. –  Mr.Wizard Jan 14 '13 at 9:52
add comment

3 Answers

up vote 10 down vote accepted

Here is the formatting command that does this:

pvB /: MakeBoxes[pvB[n1_, n2_, x_, s_, m0_, m1_], TraditionalForm] :=

  RowBox[{SubscriptBox["B", 
    RowBox[{Sequence @@ Riffle[Table["0", {n1}], "\[ThinSpace]"], 
      "\[ThinSpace]", 
      Sequence @@ Riffle[Table["1", {n2 - n1}], "\[ThinSpace]"]}]], "(",
    Sequence @@ Riffle[Map[ToBoxes, {x, s, m0, m1}], ","], ")"}]

For example:

pvB[2, 4, x, s, m0, m1] // TraditionalForm

$B_{0\,0\,1\,1}(s,m0,m1)$

The \[ThinSpace] will display as a small empty string, but is useful to insure that the indices are separated by just a tiny bit.

Edit: why use MakeBoxes?

I prefer to use MakeBoxes to define output formats, even though there is the function Format too. The reason my default choice is MakeBoxes is described in this post. Essentially, this becomes important if you want to be able to re-use your formatted output as input in later computations.

share|improve this answer
add comment

I might be oversimplifying something but I believe you can use:

MakeBoxes[pvB[n_, P_, _, x__], fmt : TraditionalForm] :=
  MakeBoxes[#, fmt] & @ Subscript[Defer @ B, Row[1 ~Table~ {n} ~PadLeft~ P]][x]

pvB[2, 4, x, s, m0, m1] // TraditionalForm

Mathematica graphics

share|improve this answer
    
PadLeft is a nice touch. –  Jens Jan 12 '13 at 17:59
    
This is certainly a very short piece of code, but I've never seen many of the symbols/functions in here. I'd have to investigate! –  QuantumDot Jan 14 '13 at 16:46
    
@QuantumDot I'd be happy to try to explain any part of it. It's really fairly simple, though that doesn't mean easy to read. Possibly my use of ~infix~ confuses you but I prefer it to stacked brackets. Basically I create the list of zeros and ones with 1 ~Table~ {n} ~PadLeft~ P then put it in a Subscript with B (held with Defer in case B has a value). This Subscript acts as a function and gets any remaining arguments, and finally it is passed back to MakeBoxes for automatic formatting. I find this much cleaner than fully crafting the Box form manually as Jens did. –  Mr.Wizard Jan 15 '13 at 1:58
add comment

Here's an alternate way to format it using Format:

Format[pvB[n_, P_, x_, s_, m0_, m1_], TraditionalForm] := DisplayForm@RowBox[{
    SubscriptBox["B", StringJoin@SparseArray[{i_ :> "1" /; i > P - n}, P, "0"]],
    RowBox[{ "(", Sequence @@ Riffle[ToBoxes /@ {s, m0, m1}, ","], ")" }]
}]

This definition will be saved in the FormatValues for pvB.

share|improve this answer
    
This answer is very economical, but the problem is that if the pvB function appears in a larger expression, an unneeded pair of parenthesis is added around it. Try TraditionalForm[3 pvB[2,4,x,s,m0,m1]], for example. How should I remove it? –  QuantumDot Jan 11 '13 at 22:44
    
@QuantumDot I'm not near mma to test it right now, but I believe the reason is because Format is mainly for display purposes only and not for use in computations, so I would recommend his approach for flexibility (see the last paragraph in Jens' answer). Mine isn't any more "economical" than Jens'... if you're referring to the compactness of generating the "0011" string, then you can very well use what I have above with MakeBoxes as in Jens' answer. –  rm -rf Jan 11 '13 at 22:53
    
Oh, I am just saying that the result of the example in my comment above is: $3\big(B_{0011}(s,m0,m1)\big)$, and the outer parenthesis is extraneous. –  QuantumDot Jan 11 '13 at 23:11
    
@QuantumDot Yeah, I understood that and I'm saying that I think it's because of the reason in Michael Pilat's answer (but I'm not sure) –  rm -rf Jan 11 '13 at 23:15
    
Yes, you are right; Jens' answer doesn't lead to extraneous parentheses. And, yeah -- I thought your answer was more economical because it took 'fewer lines.' Those are my newbie instincts showing up. –  QuantumDot Jan 11 '13 at 23:27
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.