2
$\begingroup$

TeXForm does not always change fraction a/b to $\frac{a}{b}$. If the fraction was an exponent for example, it keeps it a/b.

Is there a way to make TeXForm always change fraction to correct Latex output which is $\frac{a}{b}$ regardless of where the fraction is located in the expression?

Here is an example

 TeXForm[3/2]
 (*   \frac{3}{2}  *)

But

 TeXForm[y^(3/2)]
 (*   y^{3/2}  *)

I prefer the above to be

 y^{\frac{3}{2}}

Which, for me, looks a little better when compiled. Compare for example

$y^{3032/7}$ and $y^{\frac{3032}{7}}$

Using V 12 on windows.

$\endgroup$
4
$\begingroup$

You will need to adjust the TraditionalForm formatting of such expressions. In order to only affect TeXForm, I think the most convenient approach is to modify a TeXForm internal function so that its behavior is controlled by a global variable:

Unprotect[Convert`TeX`ExpressionToTeX];
Convert`TeX`ExpressionToTeX[e_, opts___?OptionQ] /; !TrueQ@$TeX := Block[
    {$TeX = True},
    Convert`TeX`ExpressionToTeX[e, opts]
]
Protect[Convert`TeX`ExpressionToTeX];

Now, give your expression a TraditionalForm format that is conditioned on the global variable $TeX:

Unprotect[Power];
Power /: MakeBoxes[a_^b_Rational, TraditionalForm] /; TrueQ @ $TeX := MakeBoxes[
    a^Defer[b],
    TraditionalForm
]
Protect[Power];

Finally, you should have the desired output from TeXForm:

TeXForm[y^(3/2)]

y^{\frac{3}{2}}

$\endgroup$

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.