Message Boards Message Boards

Hold expression but Evaluate variable

Posted 8 years ago

I want my code to output TeX like this to be used in a website:

\sqrt{377.09}

The number part is determined programmatically and held in the variable sqrtRad:

sqrtRad=Round[RandomReal[{1,10000}],.01];

Then I try to HoldForm[] the square root expression with the following code:

TeXForm[HoldForm[Sqrt[sqrtRad]]]

Of course, this holds the variable unevaluated as well as the square root and produces this:

\sqrt{\text{sqrtRad}}

I have tried several different functions related to Hold[] without success. How do I get it to hold the Sqrt[] while evaluating the variable?

Thanks in advance,

Mark

POSTED BY: Mark Greenberg
2 Replies
Posted 8 years ago

Thank you, Gianluca. I'm a bit closer now to understanding what I'm doing.

POSTED BY: Mark Greenberg

You can evaluate inside a held expression with replacement rules:

TeXForm[HoldForm[Sqrt[sqrtRad]]] /. 
 sqrtRad -> Round[RandomReal[{1, 10000}], .01]

or with With:

With[{sqrtRad = Round[RandomReal[{1, 10000}], .01]},
 TeXForm[HoldForm[Sqrt[sqrtRad]]]]

or, more mysteriously, this way:

sqrtRad = Round[RandomReal[{1, 10000}], .01];
With[{sqrtRad = sqrtRad},
 TeXForm[HoldForm[Sqrt[sqrtRad]]]]

There are probably other ways.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract