Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is what I get using Mathematica 9.0.1.

enter image description here

enter image description here

Instead I want this:

enter image description here

I know how to use MakeBoxes, Format and I thought this code would work:

MakeBoxes[(1/2)(expr_), StandardForm]:=
FractionBox[ MakeBoxes[expr, StandardForm], MakeBoxes[2, StandardForm]]

But the code above and all variations that I tried have no effect. This has to be automated to get the look I want. My actual application is much more complicated.

share|improve this question
up vote 6 down vote accepted

There are multiple internal forms of x / 2. I ran into the same problem here (with 1/4):
Using Hold correctly with Simplify and ComplexityFunction

This appears to work in all cases:

MakeBoxes[expr_ / 2 | Rational[1, 2] expr_, fmt_] := 
  FractionBox[MakeBoxes[expr, fmt], "2"]

(3 + Sin[t])/2

Mathematica graphics

You can better see what is going on with FullForm:

HoldForm @ FullForm[expr_/2]
HoldForm @ FullForm[Rational[1, 2]*expr_]


You need to cover both the Power[2,-1] and Rational[1, 2] cases with your pattern.

share|improve this answer

If you'd accept a replacement rule, this could be done as follows:

oneHalf /: MakeBoxes[oneHalf[expr_], StandardForm] := 
 FractionBox[MakeBoxes[expr, StandardForm], MakeBoxes[2, StandardForm]]

(3 + Sin[x])/2 /. (expr_)/2 :> oneHalf[expr]


share|improve this answer

Perhaps, something like:

 frctnBx /: MakeBoxes[frctnBx[expr_], StandardForm] :=
 With[{num = Numerator[expr], denom = Denominator[expr]},
  Switch[denom, 1, MakeBoxes[num, StandardForm],
  _, FractionBox[MakeBoxes[num, StandardForm], 
  MakeBoxes[denom, StandardForm]]]]


 explist = {(f[x] + h[y])/g[y], (f[x] + h[y])/2, (3 + Sin[t])/(5), 
 (1/y) Sin[x], f[22/7, (1/4) Sin[x]]/5}

 Grid[Prepend[ Transpose@{#, frctnBx /@ #} &@explist, {"expr", "frctnBx@expr"}],
  Dividers -> All, BaseStyle -> {FontSize -> 24}]

enter image description here

share|improve this answer

Your Answer


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.