13
$\begingroup$

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?

$\endgroup$
2

3 Answers 3

14
$\begingroup$

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.

$\endgroup$
5
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ PadLeft is a nice touch. $\endgroup$
    – Jens
    Commented Jan 12, 2013 at 17:59
  • $\begingroup$ 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! $\endgroup$
    – QuantumDot
    Commented Jan 14, 2013 at 16:46
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Commented Jan 15, 2013 at 1:58
3
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ 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? $\endgroup$
    – QuantumDot
    Commented Jan 11, 2013 at 22:44
  • $\begingroup$ @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. $\endgroup$
    – rm -rf
    Commented Jan 11, 2013 at 22:53
  • $\begingroup$ 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. $\endgroup$
    – QuantumDot
    Commented Jan 11, 2013 at 23:11
  • $\begingroup$ @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) $\endgroup$
    – rm -rf
    Commented Jan 11, 2013 at 23:15
  • $\begingroup$ 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. $\endgroup$
    – QuantumDot
    Commented Jan 11, 2013 at 23:27

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.