# TexForm with fractional overall factor

I am having some trouble with TeXForm output. Basically, I have many expressions that have an overall fractional factor, like 1/(16 Pi^2), in front. However, whenever I put the expression into TeXForm, it puts the entire expression in the form \frac{numerator}{16 Pi^2}. I was wondering if there is any way to force the overall factor to stay in front, without making one giant fraction.

A small example:

1/Pi Total[x^Range] TeXForm[%]

\frac{x^{10}+x^9+x^8+x^7+x^6+x^5+x^4+x^3+x^2+x}{\pi }

• can you provide a MWE? May 3, 2018 at 9:19
• I'm not sure this has much to do with TexForm - the default output shows the same behavior of putting everything onto a single fraction May 3, 2018 at 9:47

One idea that might work for you (without any example, I can only guess...):

expr = 1/(16 Pi^2) (a + b + c);

TeXForm[
Times @@ (
HoldForm /@ Times @@@
GatherBy[List @@ expr, MatchQ[_Rational | Power[_, _?Negative]]]
)
]
(* \frac{1}{16 \pi ^2} (a+b+c) *)


To see where this is coming from, let's take a look at the FullForm of the expression:

FullForm@expr
(* Times[Rational[1,16],Plus[a,b,c],Power[Pi,-2]] *)


The idea is now to take the expression and group into two groups - one for anything that is either a rational number or a negative power and the rest:

GatherBy[List @@ expr, MatchQ[_Rational | Power[_, _?Negative]]]
(* {{1/16, 1/π^2}, {a + b + c}} *)


Multiplying the terms together and wrapping them in HoldForm looks like this:

Times @@ (
HoldForm /@ Times @@@
GatherBy[List @@ expr, MatchQ[_Rational | Power[_, _?Negative]]]
) • Great! super cool method May 3, 2018 at 11:58

Perhaps this:

expr = 1/Pi Total[x^Range];
Through[{Numerator, Denominator}[expr]] /. {n_, d_} :> TeXForm@HoldForm[HoldForm[1/d] n]


$$\frac{1}{\pi } \left(x+x^2+x^3+x^4+x^5+x^6+x^7+x^8+x^9+x^{10}\right)$$