# Different Latex is generated when changing a number from 2 to 1. Using HoldForm

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?

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 :

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

• Why do you think there is no $\exp$ command in LaTeX? Sep 14, 2021 at 8:52
• @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. Sep 14, 2021 at 8:58
• 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. Sep 14, 2021 at 14:38

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;


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)}}

• 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. Sep 14, 2021 at 16:37
• @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"}] Sep 14, 2021 at 16:49
• 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. Sep 14, 2021 at 16:54