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!


1 Answer 1


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}$

  • $\begingroup$ 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 $\endgroup$
    – Silviu
    Mar 4, 2018 at 17:00
  • 2
    $\begingroup$ @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 $\endgroup$ Mar 4, 2018 at 21:50

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.