2
$\begingroup$

This is the most bizzar thing I saw with Mathematica TeXForm. Could someone please explain why it happens and if there is a workaround?

First, will show screen shot. I have an expression, when I have 2*x in it, TeXForm generates \exp for exp when using With[{...},TeXForm[HoldForm[...]]. When changing 2*x to 1*x, now e Latex is generated, same as TeXForm

This only happens when using With[{...},TeXForm[HoldForm[...]] construct. Otherwise TeXForm works same in both cases. But why?

enter image description here

Here is the code

ClearAll[x]

expr = 1/(Exp[(5*x - 2)/((10*x^2 - 10*x)*Exp[x] + (2*x))])
With[{z0 = expr},
  exprAsLatex = TeXForm[HoldForm[z0]];
  ];
exprAsLatex

expr = 1/(Exp[(5*x - 2)/((10*x^2 - 10*x)*Exp[x] + (1*x))])
With[{z0 = expr},
  exprAsLatex = TeXForm[HoldForm[z0]];
  ];
exprAsLatex

Using 12.3.1 on windows 10.

You might ask, why Am I using HoldForm? the reason is that this is part of larger code, where I needed to prevent evaluation of an expression while taking its Latex. The above is a MWE of the problem.

TeXForm works the same on both 2*x and 1*x :

enter image description here

The problem only shows up when using the With[{...},TeXForm[HoldForm[...]] method.

$\endgroup$
3
  • $\begingroup$ Why do you think there is no $\exp$ command in LaTeX? $\endgroup$
    – TimRias
    Sep 14, 2021 at 8:52
  • $\begingroup$ @mmeent I just checked, yes, there is \exp in latex. I never used it and Mathematica did not use it before, that is why. All the CAS systems I use generate e for exp. Still, why the difference in output is my main question. I'll adjust the question now. $\endgroup$
    – Nasser
    Sep 14, 2021 at 8:58
  • 1
    $\begingroup$ TeXForm is just converting the TraditionalForm to LaTeX. Replace TeXForm with TraditionalForm in your code to see this. Why is TraditionalForm formatting these two expressions differently? I don't know, that is a good question. $\endgroup$
    – Jason B.
    Sep 14, 2021 at 14:38

1 Answer 1

3
$\begingroup$

Update to give a simpler fix

As @jasonb says, TeXForm is based on TraditionalForm, and TraditionalForm checks the LeafCount of superscripts and uses exp instead of e when the LeafCount is too large. I suppose the thinking is that enormous superscripts are less readable. Unfortunately, the limit at which this switchover occurs is not configurable. The simplest fix is to wrap E in HoldForm:

expr = 1/(Exp[(5*x - 2)/((10*x^2 - 10*x)*Exp[x] + (2*x))]);
With[{z0 = expr},
    TeXForm[HoldForm[z0] /. E -> HoldForm[E]]
]

e^{-\frac{-2+5 x}{2 x+e^x \left(-10 x+10 x^2\right)}}

Another possibility is to do surgery on the TraditionalForm formatting rules, something like:

$SuperscriptLimit = 100;
Unprotect[TraditionalForm];
FormatValues[TraditionalForm] = FormatValues[TraditionalForm] /. 50 :> $SuperscriptLimit;
Protect[TraditionalForm];

Then:

expr = 1/(Exp[(5*x - 2)/((10*x^2 - 10*x)*Exp[x] + (2*x))]);
With[{z0 = expr},
    TeXForm[HoldForm[z0]]
]

e^{-\frac{-2+5 x}{2 x+e^x \left(-10 x+10 x^2\right)}}

$\endgroup$
3
  • $\begingroup$ Thanks. Will use your method for work around. But why then TeXForm behaves differently outside the With....HoldForm? Since LeafCount did not change. It is the same expression. $\endgroup$
    – Nasser
    Sep 14, 2021 at 16:37
  • 1
    $\begingroup$ @Nasser It is the LeafCount of the boxes that are generated. The HoldForm version has things like RowBox[{RowBox[{"-", "2"}], "+", RowBox[{"5", " ", "x"}]}] instead of RowBox[{RowBox[{"5", " ", "x"}], "-", "2"}] $\endgroup$
    – Carl Woll
    Sep 14, 2021 at 16:49
  • $\begingroup$ May be Mathematica should consider using LeafCount of the actual raw expression, not including any formatting/wrappers overheads such as RowBox. That is what I think of, when using LeafCount on expression. But your workaround worked. Thanks. $\endgroup$
    – Nasser
    Sep 14, 2021 at 16:54

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.