# Display of nested fractions as products of fractions

How do I get Mathematica to avoid displaying nested fractions in outputs, and instead automatically display them as parenthesized, factored-out terms?

For example, I would like

(A/(B + C) + D)/F


to be displayed as $$\frac{1}{F} \left(\frac{A}{B+C}+D\right)$$ instead of $$\frac{\left(\frac{A}{B+C}+D\right)}{F}$$.

If possible, I would like to do this in a somewhat automated manner, without having to manually Inactivate multiplications, for instance.

It would be great if the solution could work for "higher-order" nested fractions too. For instance:

((A/(B + C) + D)/F + G)/H


could be displayed as $$\frac{1}{H}\left(\frac{1}{F}\left(\frac{A}{B+C}+D\right)+G\right)$$

Consider an actual example of expression that I have in my code:

L^2 + (-4 + (6 L)/J + (L^2 (7 - 2 \[Nu]))/J^2 + (L^4 (-9 + \[Nu]))/   J^4 + \[Nu])/c^2 + ((L (77/2 - 11 \[Nu]))/J^3 + (4 + \[Nu]/4)/   L^2 + (3 L^3 (-9 + \[Nu]))/J^5 + (-(39/2) + 6 \[Nu])/(J L) + (   L^4 (-6920 + 2580 \[Nu] - 179 \[Nu]^2))/(32 J^6) + (   168 + 12 \[Nu] + \[Nu]^2)/(32 J^2) + (   7 L^6 (360 - 96 \[Nu] + 7 \[Nu]^2))/(16 J^8) + (   L^2 (294 - 149 \[Nu] + 10 \[Nu]^2))/(4 J^4))/c^4


I would like to ideally have an output similar to:

$$L^2 + \frac{1}{c^2}\left(\frac{L^4 (\nu -9)}{J^4}+\frac{L^2 (7-2 \nu )}{J^2}+\frac{6 L}{J}+\nu -4\right) +\frac{1}{c^4}\left(\frac{7 L^6 \left(7 \nu ^2-96 \nu +360\right)}{16 J^8}+\frac{L^4 \left(-179 \nu ^2+2580 \nu -6920\right)}{32 J^6}+\frac{3 L^3 (\nu -9)}{J^5}+\frac{L^2 \left(10 \nu ^2-149 \nu +294\right)}{4 J^4}+\frac{L \left(77-22\nu \right)}{2 J^3}+\frac{\nu ^2+12 \nu +168}{32 J^2}+\frac{12 \nu - 39}{2 J L}+\frac{\nu + 16}{4 L^2} \right)$$

• Welcome to Mathematica StackExchange! Such manipulations of the output can be quite cumbersome in Mathematica. Can you please give some more examples of your terms, so that we know how general you want the solution to be? Otherwise, this will work for your particular example: (a/(b + c) + d)/f /. (1/x_)*y_ :> HoldForm[Defer[(1/x)] y]. Commented Feb 20 at 15:32
• If you are only interested in the display of the formula, have a look at the MaTeX package from Szabolcs. It's phenomenal. With that package you get professional typeset-quality math formatting developed by true math typeset professionals (the TeX/LaTeX folks). Commented Feb 20 at 23:32
• @Nasser provided a very nice solution. If it solves your problem, please accept it. Otherwise, specify more clearly what is your desired output for your particular example. Commented Mar 1 at 18:48
• Thanks @Domen, I have provided further infos in the question Commented Mar 4 at 14:25
• I have accepted Nasser's answer. I was able to get rid of the numerical factors via the FactorTerms method. Commented Mar 5 at 12:08

Since this is for display only, how about

ClearAll["Global*"]
makeNice[e_] :=  If[Denominator[e] =!= 1, 1/Denominator[e]·Numerator[e], e]


And now you can use it as follows

makeNice[(a/(b + c) + d)/f]


makeNice[(1 + x)/(x*Sin[x])/4]


Tested only on small examples. Feel free to test it more and see if you find any bugs and will try to fix. This can be made more robust by adding more checks on the input ofcourse.

But you can't use the output since it uses center dot for multiplications. You could always replace the center dot with the real * to use it.

This works only on one term. To make it work on sum of terms, it is easy to do that. For example, given

e = (a/(b + c) + d)/f + x + (a/(b + c) + d)/g


Then you can do

 Map[makeNice, e]


• Hi @Nasser, thanks a lot for your great solution. Ideally, I would like this to also work iteratively on nested fractions inside nested fractions, if possible. See my EDIT for an example. Do you think such a thing could be done? Finally, there is a corner case where makeNice seems to fail, for instance: makeNice[6 L/J] Commented Mar 4 at 14:25
• @Christopher I tried your example and this is what I get on V 14 !Mathematica graphics is this not what is expected? Commented Mar 4 at 14:27
• Yes, ideally it should only apply when fractions are nested. In this example there is only one fraction, so I would like makeNice to do nothing Commented Mar 4 at 14:38

Although @Nasser has provided a great straightforward answer for simple cases, more complex nested fractions might require a pattern-matching solution. The following should work for a great variety of cases.

First, we define a pattern-testing function to determine whether an expression has a non-trivial denominator in any subexpression. This can be done with FreeQ:

denominatorFreeQ[expr_] := FreeQ[expr, x_ /; !SameQ[Denominator[x], 1]];


As a test, we can apply denominatorFreeQ to a few sample cases:

denominatorFreeQ /@
{a, a/x, (a + b)/(x + y), a (b + c), a (b + c/x), a b (c + (d + e/2))}


The main function can now check an expression for subexpressions whose numerators aren't denominator-free, and inactivate the Times.

formatNestedTimes = ReplaceRepeated[
x_ /; And[!denominatorFreeQ[Numerator[x]],!SameQ[Denominator[x], 1]]
:> Inactive[Times][1/Denominator[x], Numerator[x]]
]
`

Testing on the sample provided in the question yields: