# Output for Latex

I have a Mathematica command WriteString[str,TeXForm[ToString[a]^ToString[Divide[p, q]]]] where a=47, p=17 and q=2, but the result is $47^{\text{17$\backslash $n--$\backslash $n2}}$

I hope I have $47^{\frac{17}{2}}$. Can someone help me please. Thank you

-
TeXForm@HoldForm[47^(17/2)]? –  Yves Klett Jun 2 at 13:17
Thank you. The command works in Mathematica Notebook, but is not working for the command TeXForm@HoldForm[a^(p/q)]. The result is $\text{a$\$$3208}^{\text{p\$$3208}/\text{q$\$$3208}}$ for a=47, p=15, q=8 –  wono Jun 2 at 15:18
You should post the code you are using to help reproducing your results. –  Yves Klett Jun 2 at 16:01

Something like the following should give you the form you want:

DisplayForm@StyleBox[Superscript[47, Divide[17, 2]], TeXForm]

-
Thank you for your help. The command works in notebook, but is not in this command –  wono Jun 2 at 14:58
fyi, I get "an improperly formatted" message when running your code, windows 7, version 9.01 : !Mathematica graphics –  Nasser Jun 2 at 15:50

## 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[8]:= TeXForm[HoldForm[a^Divide[p, q]] /. Join[OwnValues[a], OwnValues[p], OwnValues[q]]]