1
$\begingroup$

I want to keep the form of the expression when I use TeXForm[HoldForm]] $$(x^{-1} y^3)^2 \left (\dfrac{x^2}{2y}\right)^{-2}\left (\dfrac{-2x^2}{y^3}\right )^{-4},$$ But when I use

TeXForm[HoldForm[(x^(-1) y^(3))^2 (x^2 /(2 y))^(-2) (-2 x^2 /y^3)^(-4)]]

I got $$\frac{\left(\frac{y^3}{x}\right)^2}{\left(\frac{x^2}{2y}\right)^2 \left(-\frac{2x^2}{y^3}\right)^4}.$$ How can I keep the original expression?

$\endgroup$
  • 1
    $\begingroup$ Look at FullForm[HoldForm[your expression]]. Mathematica does not have a Divide operator, so there is no difference between the representation of x/y and that of x y^-1. You would need to process the expression as a string to accomplish what you desire. $\endgroup$ – JJM Oct 27 '16 at 19:17
  • $\begingroup$ Possible duplicate: mathematica.stackexchange.com/questions/20566/… (since the problem has more to do with how Mathematica treats negative powers, than with TeXForm per se). $\endgroup$ – Michael E2 Nov 25 '16 at 21:19
2
$\begingroup$

Why does this happen? Because Mathematica interprets both x/y and x y^-1 as the same expression.

FullForm@HoldForm[x/y]
(* HoldForm[Times[x,Power[y,-1]]] *)

FullForm@HoldForm[x y^-1]
(* HoldForm[Times[x,Power[y,-1]]] *)

This happens during the parsing step, not evaluation. It cannot be prevented with HoldForm.

You can achieve your goal, but it requires some manual intervention. Here are a few ways to trick TeXForm into producing different output than the default by inserting HoldForm into the expression at strategic positions:

TeXForm[x y^-1]
(* \frac{x}{y} *)

TeXForm[x y^HoldForm[-1]]
(* x y^{-1} *)

TeXForm[x/y^a]
(* x y^{-a} *)

TeXForm[x/HoldForm[y^a]]
(* \frac{x}{y^a} *)

TeXForm[HoldForm@Divide[x,y^a]]
(* \frac{x}{y^a} *)

Note: Divide[x,y] is distinct from x/y/x y^-1. It evaluates immediately to x y^-1, but since this is now evaluation and not parsing, it can be prevented with HoldForm.

In certain cases it will be possible to insert HoldForm using ReplaceAll. Otherwise it may be easier to just edit the $\TeX$ code manually ... That is what I usually do.

$\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.