10
$\begingroup$

Sometimes, Mathematica's internal representation of expressions can give somewhat surprising results. Specifically, I noticed this when I tried to preserve products of fractions for display.

E.g. the following example as in "HoldForm does not Hold Form for fractions sometimes":

enter image description here

i.e. it isn't easily possible to preserve the output fractions as they are written. This is actually surprising though, because Mathematica internally preserves the "product of two fractions" form:

In[1]:=  HoldForm[1/x 1/y]  // InputForm
Out[1]=  HoldForm[(1/x)*(1/y)]

In[2]:=  HoldForm[1/x 1/y]  // FullForm
Out[2]=  HoldForm[Times[Times[1,Power[x,-1]],Times[1,Power[y,-1]]]]

Is there some way to get Mathematica to display such forms as actual product of fractions in any of the formatted output forms, without manually adding holdform constructs?

$\endgroup$
2
  • 1
    $\begingroup$ As to the final question, why not just convert input cell to the output once you are done with the input that should not be modified anyway? $\endgroup$
    – Kuba
    Commented Nov 15, 2018 at 11:33
  • $\begingroup$ Strongly related: (1), (2). $\endgroup$ Commented Nov 15, 2018 at 11:35

3 Answers 3

4
$\begingroup$

How about:

MakeBoxes[Times[a_, b_], StandardForm] := RowBox[{MakeBoxes@a, MakeBoxes@b}]
1/x 1/y 1/z

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ Btw, one could argue that 1/x should be kept in that exact form too, that is no fraction box in input, no fraction box in output. $\endgroup$
    – Kuba
    Commented Nov 15, 2018 at 11:17
  • $\begingroup$ @kuba Oh I forgot about that, now I've turned to MakeBoxes in all places, thanks for pointing out. As to "no fraction box in input, no fraction box in output" part, I think OP's goal is just obtaining an output consistent with the FullForm of the expression, so I'd like to stop here. (I admit that's a harder and interesting question though. Perhaps a new question like "Can I make the display of expression changeless at all?" should be asked? ) $\endgroup$
    – xzczd
    Commented Nov 15, 2018 at 11:28
  • $\begingroup$ Useful for my usecase, as it controls exactly what is displayed how. I suppose nothing more catch-all than this is doable. @xzczd I'd also prefer if it were possible to keep "1/x" vs the 2-dimensional form separate, but I don't think that this distinction is even kept in the internal representation. $\endgroup$
    – kdb
    Commented Nov 15, 2018 at 15:06
  • $\begingroup$ @kdb Yeah, if $1/x$ and $\frac{1}{x}$ needs to be distincted, I'm afraid something as shown in Kuba's answer is necessary. I hope I'm wrong. $\endgroup$
    – xzczd
    Commented Nov 15, 2018 at 15:37
  • 1
    $\begingroup$ (+1) An extended version of this approach can be found in this answer. $\endgroup$ Commented May 8, 2022 at 13:57
3
$\begingroup$

IMO, the only way to be sure is to interfere as soon as possible:

$PreRead = Function[boxes
,  boxes /. 
     RowBox[{"RawInput", "[", hf_, "]"}] :> 
     RowBox[{"RawBoxes", "[", ToBoxes[hf], "]"}]
];

enter image description here

$\endgroup$
3
$\begingroup$

Another possibility is to use InString:

1/x 1/y
ToExpression[InString[-1], StandardForm, RawBoxes]

screenshot

$\endgroup$

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.