Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
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

2 Answers 2

up vote 0 down vote accepted

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

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

share|improve this answer
    
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 your case:

In[8]:= TeXForm[HoldForm[a^Divide[p, q]] /. Join[OwnValues[a], OwnValues[p], OwnValues[q]]]
Out[8]//TeXForm=
         47^{\frac{17}{2}}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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