# How to hold the expression when using ToExpression with TeXForm

I know

{1, 1} // Apply[Defer@*Plus]


can return

1+1

But if I want hold the form from TeXForm,what should I do?

ToExpression["1+1", TeXForm]


will return 2 . But I don't know how to Defer the Plus.

• How about 1+1//HoldForm//TeXForm Apr 7, 2020 at 7:25

Use the 3rd argument of ToExpression.

ToExpression["1+1", TeXForm, Hold]


If the TeX string is coming from untrusted sources, it is better to use HoldComplete for complete safety. Try this: ToExpression["\\text{Evaluate}(\\text{Print}(gotcha))", TeXForm, Defer].

Every function that extracts an expression that might evaluate has some facilities to keep the result held.

ToExpression["\\frac{1}{\\sqrt{5}}+\\frac{1}{\\sqrt{5}}",TeXForm,Hold]


will return

(* Hold[1/Sqrt[5] + 1/Sqrt[5]] *)


which I assume is what you need?

• 16 second difference :-) Apr 7, 2020 at 7:47

Another way:(from QQ: 2716697201)

ToExpression["1+1", TeXForm] // Trace // #[[2]] &


whose FullForm is

HoldForm[Plus[1,1]]

• You really should not do this though. Apr 7, 2020 at 7:42
• I agree. That's pretty inventive, but it's a dark, dark art. Apr 7, 2020 at 7:43