3
$\begingroup$

I have a $\LaTeX$ equation that contains coefficients of the form $\delta x_1^{-1}$ and $\delta x_1^1$. I would like to convert them from TeXForm to Mathematica HoldForm:

ToExpression[{"\delta x_1^{-1}", "\delta x_1^{1}"}, TeXForm, HoldForm] (*{δ/Subscript[x, 1], δ Subsuperscript[x, 1, 1]}*) 

which gives $\dfrac{\delta}{x_1}$ and $\delta x_1^1$. I want to keep the same form of $\delta x_1^{-1}$ in HoldForm instead of $\dfrac{\delta}{x_1}$.

How can I do this?

$\endgroup$
3
  • $\begingroup$ If you examine the FullForm[] of the output of ToExpression[], you see that what you get is HoldForm[Times[\[Delta], Power[Subscript[x, 1], -1]]]. The trouble is not with ToExpression[], it's with the typesetting of the result by done by the FrontEnd. I may be able to find a Style[] option to get what you want...or someone may be me to it. But perhaps the output formatting is not a concern? $\endgroup$ Commented Jul 31 at 15:45
  • 2
    $\begingroup$ Related: mathematica.stackexchange.com/questions/20566/…, mathematica.stackexchange.com/questions/147415/… $\endgroup$ Commented Jul 31 at 15:50
  • $\begingroup$ You would like to convert it to HoldForm. But if you evaluate HoldForm[x^(-1)] you will see that it does not do what you think it should do. $\endgroup$ Commented Jul 31 at 17:47

2 Answers 2

2
$\begingroup$

Look at your expression using: FullForm:

ToExpression[{"\delta x_1^{-1}", "\delta x_1^{1}"}, TeXForm, HoldForm] // FullForm 

enter image description here

You see, the offending expression is: Power[Subscript[x, 1], -1]]]. To get what you want, we need to replace this by Supersubscript like:

ToExpression[{"\delta x_1^{-1}", "\delta x_1^{1}"}, TeXForm, HoldForm] /. Power[Subscript[x_, y_], z_] -> Subsuperscript[x, y, z] 

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you so much @Daniel Huber. $\endgroup$ Commented Aug 1 at 13:18
3
$\begingroup$

Possible goals (unspecified in OP):

Goal 1 is fairly easy. The others are a bit harder.

  1. The output looks right.
  2. The output, if copied and pasted into an input cell, can then be correctly evaluated.
  3. The result can be correctly evaluated (if stored in a variable or using %).

Note that the TeX string has syntax error: The backslashes should be escaped. Luckily the expression wasn't "\nu x_1^{-1}". (Fixed below.)

Output that looks right (and with effort can be evaluated)

The simplest approach might be to wrap a negative (integer) power in HoldForm[]. One can use ?Negative for negative numeric powers or ?Internal`SyntacticNegativeQ for arbitrary exponents typeset with a leading minus sign. The latter is probably unnecessary, since things like x^-y are typeset with negative powers as desired. Example:

ToExpression[{"\\delta x_1^{-1}", "\\delta x_1^{1}"}, TeXForm, HoldForm] //. Power[b_, n_Integer?Negative] :> Power[b, HoldForm[n]] 
the list HoldForm of the quantity Delta times x sub 1 to the power of HoldForm of minus 1, HoldForm of the quantity Delta times x sub 1 to the 1st power

Here is an operator to assist with putting the results of the OP's ToExpression[] into the desired form.

(* Semi-success *) toExpForm = ReplaceRepeated[Power[b_, n_Integer?Negative] :> Power[b, HoldForm[n]]]@* ReplaceAll[Power[b_, k_.*HoldForm[n_Integer?Negative]] :> b^(k*n)]; 

Note that the result cannot be used in calculations unless ReleaseHold[] is used twice, once for the HoldForm[] from ToExpression[] and once for the held negative integer powers. (You might have to use ReleaseHold[] more times in the ?Internal`SyntacticNegativeQ case, since it could produce nested held exponents).

tex1 = ToExpression[{"\\delta x_1^{-1}", "\\delta x_1^{1}"}, TeXForm, HoldForm] // toExpForm tex2 = ReleaseHold[tex1]^2 ReleaseHold[tex2] (* or: ReleaseHold[ReleaseHold[tex1]] *) 

the list HoldForm of the quantity Delta times x sub 1 to the power of HoldForm of minus 1, HoldForm of the quantity Delta times x sub 1 to the 1st power

x sub 1 to the power 2(-1)

x sub 1 squared
(* Second ReleaseHold in toExpForm *) ReleaseHold[tex1]^2 // toExpForm 
x sub 1 to the power -2

So one can compute with the formatted result, but you to release two layers of holds instead of just one (from the ToExpression[..., HoldForm]

Satisfying all three goals

MakeBoxes[] is the main tool for formatting output. When it is applied by the system after the evaluation process, it does not change the result. It creates the box form of the result to be displayed by the front end.

There are a couple of ways it could be used. One could make a permanent change, or, as I've done below, one could create a noFractionForm, that works a bit like TraditionalForm. To do that, one has to add the new form to $BoxForms and make some definitions for MakeBoxes[]. I like to record the original values for system parameters like $BoxForms:

$BoxForms $BoxForms // Attributes (* {StandardForm, TraditionalForm} {ReadProtected} *) 

Here is the definition of noFractionForm

$BoxForms = DeleteDuplicates@Append[$BoxForms, noFractionForm]; MakeBoxes[noFractionForm[Power][Subscript[b_, k_], n_Integer?Negative], form_] := SubsuperscriptBox[Parenthesize[b, form, Power, Left], MakeBoxes[k, form], MakeBoxes[n, form]]; MakeBoxes[noFractionForm[Power][b_, n_Integer?Negative], form_] := SuperscriptBox[Parenthesize[b, form, Power, Left], MakeBoxes[n, form]]; MakeBoxes[expr_, noFractionForm] := With[{newExpr = HoldComplete[expr] //. Power[b_, n_Integer?Negative] :> noFractionForm[Power][b, n]}, Replace[newExpr, HoldComplete[e_] :> FormBox[MakeBoxes[e, StandardForm], StandardForm]] ]; noFractionForm /: ParentForm[noFractionForm] := StandardForm; 

The last line isn't needed. (At one point, I did, but the FormBox[..., StandardForm] eliminated that need.) In any case, it turns noFractionForm black, which I found pleasing.

ToExpression[{"\\delta x_1^{-1}", "\\delta x_1^{1}"}, TeXForm, HoldForm] // noFractionForm 
the list HoldForm of the quantity Delta times x sub 1 to the power of HoldForm of minus 1, HoldForm of the quantity Delta times x sub 1 to the 1st power
ReleaseHold[%]^2 // noFractionForm 
x sub 1 to the power -2
previous output cubed leads to sixth powers

Finally, here is how to unset noFractionForm:

(** Unset noFractionForm *) MakeBoxes[noFractionForm[Power][Subscript[b_, k_], n_Integer?Negative], form_] =.; MakeBoxes[noFractionForm[Power][b_, n_Integer?Negative], form_] =.; MakeBoxes[expr_, noFractionForm] =.; ClearAll[noFractionForm] $BoxForms = DeleteCases[$BoxForms, noFractionForm] (* {StandardForm, TraditionalForm} *) 

Note that the patterns being Unset[] must match exactly the pattern that was set.

$\endgroup$
2
  • $\begingroup$ Thank you so much @Michael E2 for this solution. $\endgroup$ Commented Aug 4 at 10:46
  • $\begingroup$ @Gallagher You're welcome. $\endgroup$ Commented Aug 4 at 13:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.