# Pull out variable and common denominator from fraction

I have this code:

K[Q_, n_Integer] :=
Module[{z, x},
SymmetricReduction[
SeriesCoefficient[
Product[ComposeSeries[Series[Q[z], {z, 0, n}],
Series[x[i] z, {z, 0, n}]], {i, 1, n}], n],
Table[x[i], {i, 1, n}], Table[Subscript[c, i], {i, 1, n}]][[1]] //
FactorTerms]

poly = K[Sqrt[#]/Tanh[Sqrt[#]] &, 8] /. c -> p;
primeFactorForm[n_] :=
If[Length@# == 1, First@#, CenterDot @@ #] &[Superscript @@@ FactorInteger[n]];

For[i = 0, i < 5, i++,
poly = K[Sqrt[#]/Tanh[Sqrt[#]] &, i] /. c -> p;
Print[Subscript[L, i], " = ",
Plus @@ List @@ Expand[poly] /.
Times[Rational[n_, d_], e__] :>
primeFactorForm[n]/ primeFactorForm[d]*e]]


Which outputs some polynomials of the form:

$L_2 = \frac{p_1^2-1^1}{ 5^1 \cdot 3^2}+\frac{7^1 p_2}{5^1\cdot 3^2 }$

How can I put the variable out of the fraction? I want to obtain something like: $L_2 = \frac{-1^1}{ 5^1 \cdot 3^2}p_1^2+\frac{7^1 }{5^1\cdot 3^2 }p_2$ Thank you!

• You can use collect command. – Gopal Verma Mar 6 '18 at 11:56
• @GopalVerma Where exactly should I add that? I see that Collect requires a variable but my polynomial has many (it is not just a polynomial in x) – Silviu Mar 6 '18 at 12:16
• Can you provide your polynomial? – Gopal Verma Mar 6 '18 at 12:22
• I gave an example of one of them in my post. They look more or less the same for higher terms, just with more parameters not just p1 and p2 and also crossed terms i.e. p1*p3 – Silviu Mar 6 '18 at 12:25
• Did you try Expand[poly]? – Gopal Verma Mar 6 '18 at 14:32