5
$\begingroup$

I don't understand the following behavior (in v 9.0.1) of TraditionalForm regarding insertion of parenthesis.

Let us start with the a definition for myfuncF to make boxes in traditional form:

myfuncF /: MakeBoxes[myfuncF[a_, b_], TraditionalForm] := RowBox[{"f(", Sequence @@ Riffle[Map[ToBoxes, {a, b}], ","], ")"}] 

Now look at what I get as output for the following two commands:

x myfuncF[a,b] // TraditionalForm myfuncF[a,b] > 0 //TraditionalForm 

first case

Notice that in the first case, an unnecessary pair of parenthesis is inserted for multiplication.

Now I modify the MakeBoxes definition by inserting a small space: (in LHS, change RowBox[{"f(",… to RowBox[{" f(",…):

myfuncF /: MakeBoxes[myfuncF[a_, b_], TraditionalForm] := RowBox[{" f(", Sequence @@ Riffle[Map[ToBoxes, {a, b}], ","], ")"}] 

Run the two commands again, and look what we get:

second case

Now we get an unnecessary pair of parenthesis in the inequality!

So my questions are:

  1. How do I understand this odd behavior (what's going on behind the scenes)?
  2. What can I do to get rid of these superfluous parentheses?
$\endgroup$

2 Answers 2

4
$\begingroup$

Let us inspect the built-in behavior

ToBoxes@TraditionalForm[f[a, b]] (* TagBox[FormBox[RowBox[{"f", "(", RowBox[{"a", ",", "b"}], ")"}], TraditionalForm], TraditionalForm, Editable -> True] *) 

So

myfuncF /: MakeBoxes[myfuncF[a_, b_], TraditionalForm] := RowBox[{"f", "(", RowBox@Riffle[Map[ToBoxes, {a, b}], ","], ")"}] x myfuncF[a, b] // TraditionalForm myfuncF[a, b] > 0 // TraditionalForm 

enter image description here

It is because TraditionalForm don't understand you and put parentheses for sure. They are necessary in the following example

plus /: MakeBoxes[plus[a_, b_], TraditionalForm] := RowBox@Riffle[Map[ToBoxes, {a, b}], "+"] x plus[a, b] // TraditionalForm plus[a, b] > 0 // TraditionalForm 

enter image description here

$\endgroup$
2
$\begingroup$
myfuncF /: MakeBoxes[myfuncF[a_, b_], TraditionalForm] := RowBox[{"f", "(", RowBox[{ToString@a, ",", ToString@b}], ")"}] x myfuncF[a, b] // TraditionalForm myfuncF[a, b] > 0 // TraditionalForm 

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.