3
$\begingroup$

It seems that expression for fraction differs
when the number of terms in the numerator is more than 2.

{a, a + b, a + b + c, a + b + c + d, a + b + c + d + e}/2

{a/2, (a + b)/2, 1/2 (a + b + c), 1/2 (a + b + c + d), 
 1/2 (a + b + c + d + e)}

Much better to see it as a screenshot :

enter image description here

I want to control the expression for fraction precisely.
Like alpha/beta/gamma/delta type in the screenshot.
(I want to see such expressions in output cell)
You can just provide an answer without theoretical explanation.
(After seeing some examples, probably I can do something more difficult)

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ Answer to this question depends on if you want to use these outputs as inputs to symbolic computation or just as typographical layout. $\endgroup$
    – kirma
    Commented Mar 4, 2023 at 14:14

2 Answers 2

5
$\begingroup$

They are not different. They are exactly the same, What you are mixing is external representation of expressions vs. the internal one.

Here is the TreeForm of

Mathematica graphics

And here is the TreeForm of

Mathematica graphics

You can see the internal representation is the same.

But the external representation of the first one and the second one are

Mathematica graphics

To control the external representation it means you have to fight the frontend and have to use things like Hold or HoldForm or Inactive and so on to prevent evaluation. But this buys you nothing, Since to be able to use the expression you have to release the hold. So you end up with just more complicated code to look at.

If you are interested in final expression representation only, and not during computation, then you could always do that at the very end using the Hold functions mentioned above. If this is what you want, it will be easy to do that.

expr = {a, a + b, a + b + c, a + b + c + d, a + b + c + d + e}/2

Mathematica graphics

"gamma" type

Expand[expr]

Mathematica graphics

Alpha type

DisplayForm[FractionBox[Numerator[#], Denominator[#]]] & /@ expr

Mathematica graphics

I would actually do everything using MateX, since it is just for display purposes. Here is an example

Alpha

<< MaTeX`
expr = {a, a + b, a + b + c, a + b + c + d, a + b + c + d + e}/2;
s = "\\frac{" <> ToString@TeXForm[Numerator[#]] <> "}{" <> 
     ToString@TeXForm[Denominator[#]] <> "}" & /@ expr;
MaTeX[s]

Mathematica graphics

Beta

s = "\\frac{1}{" <> toX[Denominator[#]] <> "}\\left(" <> 
    toX[Numerator[#]] <> "\\right)" & /@ expr
MaTeX[s, Magnification -> 1.3]

Mathematica graphics

Delta type could be done similarly.

$\endgroup$
4
$\begingroup$

Expanding on Alexey's answer, here are some possible ways to obtain different box structures of rational expressions. You could further complicate the β type to remove unnecessary parentheses for a/2.

expr = {a, a + b, a + b + c, a + b + c + d, a + b + c + d + e}/2;

(* α type *)
Clear[MakeBoxes];
MakeBoxes[Times[Rational[n_, d_], expr__], StandardForm] := 
  FractionBox[ToBoxes[Times[n, expr]], d];
expr

(* β type *)
Clear[MakeBoxes];
MakeBoxes[Times[Rational[n_, d_], expr__], StandardForm] := 
  RowBox[{FractionBox[n, d], " ", RowBox[{"(", ToBoxes[expr], ")"}]}];
expr

(* γ type *)
Clear[MakeBoxes];
Expand /@ expr

(* δ type *)
Clear[MakeBoxes];
MakeBoxes[Times[Rational[n_, d_], expr__], StandardForm] := 
  RowBox[{FractionBox[n, d], " ", ToBoxes[expr]}];
Expand /@ expr

Outputs

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.