Why your output looks like that
You are using ToString
on subexpressions. As one can read in ToString
documentation, by default, it converts expressions to strings in OutputForm
. And in OutputForm
numerator, fraction bar and denominator are printed in separate lines:
In[1]:= ToString[17/2]
% // FullForm
Out[1]= 17
--
2
Out[2]//FullForm=
"17\n--\n2"
How to get what you want
As @YvesKlett showed in a comment, instead of wrapping subexpressions in ToString
, you could use HoldForm
to prevent evaluation.
In[3]:= TeXForm@HoldForm[47^Divide[17, 2]]
Out[3]//TeXForm=
47^{\frac{17}{2}}
But HoldForm
prevents also evaluation of symbols inside it, so even if they have some values set, those values will not be used:
In[4]:= a = 47; p = 17 ; q = 2;
TeXForm@HoldForm[a^Divide[p, q]]
Out[5]//TeXForm=
a^{\frac{p}{q}}
To use values of symbols inside Hold...
expressions you can assign those values to "local constants" using
With
.
Those constants will be used:
In[6]:= With[
{a = a, p = p, q = q},
TeXForm@HoldForm[a^Divide[p, q]]
]
Out[6]//TeXForm=
47^{\frac{17}{2}}
You could also directly use rules used by Mathematica to replace symbols with their values. You can get them using OwnValues
:
In[7]:= OwnValues[a]
Out[7]= {HoldPattern[a] :> 47}
In your case:
In[8]:= TeXForm[HoldForm[a^Divide[p, q]] /. Join[OwnValues[a], OwnValues[p], OwnValues[q]]]
Out[8]//TeXForm=
47^{\frac{17}{2}}
TeXForm@HoldForm[47^(17/2)]
? $\endgroup$