2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ How about 1+1//HoldForm//TeXForm $\endgroup$
    – Bill Watts
    Apr 7, 2020 at 7:25

3 Answers 3

5
$\begingroup$

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.

  • Extract has a similar 3rd argument
  • LinkRead has a similar 2nd argument
  • Read has Read[file, Hold[Expression]]
  • etc.
$\endgroup$
4
$\begingroup$
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?

$\endgroup$
1
  • 1
    $\begingroup$ 16 second difference :-) $\endgroup$
    – Szabolcs
    Apr 7, 2020 at 7:47
1
$\begingroup$

Another way:(from QQ: 2716697201)

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

whose FullForm is

HoldForm[Plus[1,1]]

$\endgroup$
2
  • 2
    $\begingroup$ You really should not do this though. $\endgroup$
    – Szabolcs
    Apr 7, 2020 at 7:42
  • 1
    $\begingroup$ I agree. That's pretty inventive, but it's a dark, dark art. $\endgroup$
    – Victor K.
    Apr 7, 2020 at 7:43

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.