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.

  • $\begingroup$ TeXForm@HoldForm[47^(17/2)]? $\endgroup$ – Yves Klett Jun 2 '14 at 13:17
  • $\begingroup$ 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 $\endgroup$ – wono Jun 2 '14 at 15:18
  • $\begingroup$ You should post the code you are using to help reproducing your results. $\endgroup$ – Yves Klett Jun 2 '14 at 16:01

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

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

| improve this answer | |
  • $\begingroup$ Thank you for your help. The command works in notebook, but is not in this command $\endgroup$ – wono Jun 2 '14 at 14:58
  • $\begingroup$ fyi, I get "an improperly formatted" message when running your code, windows 7, version 9.01 : !Mathematica graphics $\endgroup$ – Nasser Jun 2 '14 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

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

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

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

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]]]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.