2
$\begingroup$

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[10]]

Mathematica graphics

TeXForm[%]

\frac{x^{10}+x^9+x^8+x^7+x^6+x^5+x^4+x^3+x^2+x}{\pi }
$\endgroup$
2
  • 1
    $\begingroup$ can you provide a MWE? $\endgroup$
    – Nasser
    Commented May 3, 2018 at 9:19
  • $\begingroup$ 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 $\endgroup$
    – Lukas Lang
    Commented May 3, 2018 at 9:47

2 Answers 2

2
$\begingroup$

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]]]
  )

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Great! super cool method $\endgroup$ Commented May 3, 2018 at 11:58
2
$\begingroup$

Perhaps this:

expr = 1/Pi Total[x^Range[10]];
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)$$

$\endgroup$

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.