# Pull the variable out of the fraction

I have a code which outputs a polynomial, the last line is of the form: K[f[#]/g[#] &, 6], with f and g being some functions. The coefficients are fractions, but the output has terms which look like 1234x/46436. When I try to export this as latex, it looks ugly as the variable x isn't outside the fraction. Is there a way to put x outside the fraction (make it looks like (1234/46436)x) before or during the conversion to latex? Thank you!

You don't have an example polynomial, so I will use the following:

poly = (x+2)^5/10 //Expand


16/5 + 8 x + 8 x^2 + 4 x^3 + x^4 + x^5/10

To force rationals to precede the polynomial variables, you can wrap them in HoldForm before converting to latex. For example:

poly /. r_Rational :> HoldForm[r] //TeXForm


$\frac{1}{10} x^5+x^4+4 x^3+8 x^2+8 x+\frac{16}{5}$

• Thank you! It looks like what I want. However, I also write the numerator and denominator of each fraction in its prime factorization, and if I use the line you suggested on that it doesn't seem to work. Do you know why might be the case? Something like this: 1/primeFactorForm[gcd]*Plus @@ List @@ Expand[gcd*poly] /. Times[Rational[n_, d_], e__ ] :> primeFactorForm[n]/ primeFactorForm[d]*e /. r_Rational :> HoldForm[r] //TeXForm – Silviu Mar 4 '18 at 17:00
• @Silviu it is not good etiquette to ask a question, then when someone spends time to provide an answer, expand the question. Everything you want to do should be in your original post – Mike Honeychurch Mar 4 '18 at 21:50