3
$\begingroup$

I'm trying to get an expression to display, without it evaluating it's internal arguments, but for some reason, when given the Quantity function, HoldForm doesn't seem to do what I expect:

Code

HoldForm[Quantity[1 + 1, ("Seconds")/("Centimeters")]]

Expected

(1+1) s/cm

Result

2 s/cm

Strangely enough, if I use the following code:

HoldForm[Quantity[1 + 1, "Seconds"]]

I get the expected result:

(1+1) s
$\endgroup$
5
  • 4
    $\begingroup$ Examine HoldForm[Quantity[1 + 1, ("Seconds")/("Centimeters")]] // FullForm. I think the problem is with the Front End typesetting the expression. $\endgroup$ – Michael E2 Oct 7 '20 at 3:24
  • $\begingroup$ I'm not very experienced with Mathematica to know exactly how to fix that, but if I examine the FullForm of both expressions: HoldForm[Quantity[Plus[1,1],Times["Seconds",Power["Centimeters",-1]]]] vs HoldForm[Quantity[Plus[1,1],"Seconds"]] there doesn't seem to be much difference between them. So either the function in the second argument is an issue or the back end is do something I'm unaware of. Is this what you mean? $\endgroup$ – Vince McKinsey Oct 7 '20 at 3:31
  • 1
    $\begingroup$ I don't know how to fix it either. What is sent to the Front End is the output of MakeBoxes[HoldForm[Quantity[1 + 1, ("Seconds")/("Centimeters")]], StandardForm]. In it, the 1+1 evaluates to 2 at some step. In the kernel, it is still 1 + 1, which can be seen as Plus[1, 1] in the FullForm. If you're mainly interested in what's displayed in the notebook, then you have a problem. If you're mainly interested in the what happens in the kernel, then there's no problem. If it's a problem, you could try Quantity[HoldForm[1+1],...], but I don't know if that will work in your use case. $\endgroup$ – Michael E2 Oct 7 '20 at 3:48
  • 1
    $\begingroup$ Yeah, it looks like the FE is the culprit. Try, for example, HoldForm[Quantity[1 + x, "Seconds"]] and setting values for x. The FE doesn't have the value of x and therefore can't evaluate 1 + x, leaving it as is. It's a rather bizarre behaviour for sure. Looks like Quantity[HoldForm[1 + 1], ...] is the only way around it right now (short of defining your own MakeBoxes rules for Quanitity). $\endgroup$ – Sjoerd Smit Oct 7 '20 at 10:20
  • $\begingroup$ This is very strange. If you take HoldForm[Quantity[1 + 1, "Seconds"]]and replace Seconds by any other simple unit (such as Hours, Minutes, Weeks, etc.), The first evaluation gives 2, but reevaluating immediately after yields 1+1. If you return to a previously used unit, it will always yield 1+1. $\endgroup$ – Jean-Pierre Oct 7 '20 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.