# HoldForm: how does it interact with nested evaluations?

Here's an example to illustrate the issue I'm having.

right = 8 - y;
left = y^2 + 6;

integrand = HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]] (I realize this is a strange form for integrand but it is the form I need to work with.) Here, HoldForm allows its argument to be evaluated, and then holds the form of the resulting output. This is what I wanted.

Then

simpleintegrand = ReleaseHold[integrand] which is fine.

However,

HoldForm[Integrate[Evaluate[simpleintegrand], {y, 0, 1}]]


yields (removing the Evaluate doesn't help) when I was after $$\int_0^1 (2-y-y^2)\,dy.$$

(In another direction, I'd actually prefer to get $$\int_0^1 (-y^2-y+2)\,dy$$ and am happy to hear how, but the crux of my question is how to use HoldForm when its argument involves nested commands like above.)

• Integrate[HoldForm[Evaluate@simpleintegrand], {y, 0, 1}] but I've failed to apply PolynomialForm[#,TraditionalOrder->True]& to get the second form.
– Kuba
Aug 15, 2013 at 23:56
• The simple answer is that Evaluate only works when it is at the first level of a held expression. Consider: Hold[1, Evaluate[2 + 2], 3] versus Hold[{1, Evaluate[2 + 2], 3}] -- in the second case nothing evaluates. But instead of telling us that this "is the form I need to work with" why don't you tell us what you are trying to accomplish and we may be able to give you a different approach. Aug 16, 2013 at 0:06
• Almost the exact same question was already asked. There are million ways to do this and the keyword is non-standard evaluation. This is not restricted to HoldForm! One possible solution to your first question is HoldForm[Integrate[#, {y, 0, 1}]]&[Evaluate[simpleintegrand]]. I'll mark it as duplicate so you'll see the original Q&A. Aug 16, 2013 at 0:32
• possible duplicate of Question about evaluation control Aug 16, 2013 at 0:32
• @halirutan: Before posting, I studied that example. However, it seemed to me that with my example it was the combination of the Integrate and the evaluation of the integrand, whereas there was only the former in the linked question. Aug 16, 2013 at 0:41

Looking at comments such as this:

For example, right = 9-y^2; left = 5; HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]] doesn't preserve the "right minus left" ordering, yet HoldForm[HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]]] fails. Ideas?

I think you didn't understand what I said about Evaluate so I'll try again.

• Evaluate only works when it is the explicit head of an expression at level one.

• You cannot use Evaluate in the way you keep attempting: it won't work.

Instead you need to use a different method to control evaluation in your expressions.
Let me direct you to some useful posts on the subject:

Replacement inside held expression

Injecting a sequence of expressions into a held expression

But here you don't even need these advanced methods, you can do a simple replacement:

right = 9 - y^2; left = 5;
HoldForm[r - l] /. {r -> right, l -> left}

(9-y^2)-5


Or a Function:

right = 9 - y^2; left = 5;
HoldForm[# - #2] &[right, left]

(9-y^2)-5


Kuba criticizes this code if e.g. left = -5 is used saying that "in general it is not useful." I disagree. It outputs exactly what you instructed it to.

right = 9 - y^2; left = -5;
HoldForm[# - #2] &[right, left]

(9-y^2)--5


You introduce two separate Times[-1, _] expressions here; look at the FullForm:

HoldForm[# - #2] &[right, left] // FullForm

HoldForm[Plus[Plus[9,Times[-1,Power[y,2]]],Times[-1,-5]]]


You should use a form that does not if you want proper formatting of Plus by MakeBoxes:

HoldForm[+##] &[right, 5]
HoldForm[+##] &[right, -5]

(9 - y^2) + 5

(9 - y^2) - 5


How can I reorder the factors in the terms of a polynomial?

• The problem is, that in general it is not useful, try: left = -5;
– Kuba
Aug 17, 2013 at 9:02
• This is helpful, especially the links. Several other methods I tried kept running into the issue Kuba points out here. The $--5$ isn't a problem (for my application) if it would write it as $-(-5)$. Aug 17, 2013 at 14:16
• @Kuba Please see update. Aug 17, 2013 at 14:42
• @Mr.Wizard Thanks for update. Yes, it looks like I criticized the code but I should have added that that answer was good (+1ed earlier). I know we are getting what we are asking for :). I just wanted to point out this issue which would appear for everyone who was going to play with this code. p.s. - I like those tiny details in your codes, here the + ##.
– Kuba
Aug 17, 2013 at 14:50
• Very helpful indeed. Thus far, this seems to be doing best what I need across the widest variety of cases: HoldForm[Subtract[#1, #2]] &[right, left]. Aug 17, 2013 at 16:31

right = 8 - y; left = y^2 + 6;
integrand = HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]] With[{f = ReleaseHold@integrand}, HoldForm@Integrate[f, {y, 0, 1}]] % // ReleaseHold

7/6

• This is helpful. I'm still trying to better understand HoldForm especially when I want to pass arguments some of which I want evaluated. For example, right = 9-y^2; left = 5; HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]] doesn't preserve the "right minus left" ordering, yet HoldForm[HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]]] fails. Ideas? Aug 16, 2013 at 14:13
• @JohnD. At this point, I think the best thing I can do is recommend David Wagner's book, which has a good discussion of working with held forms. Go to this link for info on how to get for free. Also, what you bring up with the ordering of right = 9 - y^2; left = 5; has more to do with the Orderless attribute of Plus than it does with held forms. Aug 16, 2013 at 14:33

As suggested in my comment, for your first question please look at the explanations here

For your second question about the polynomial ordering, you can use the same approach to create and fix the presentation

(* your original code *)
right = 8 - y;
left = y^2 + 6;
integrand = HoldForm[Evaluate[right]] - HoldForm[Evaluate[left]]

(* tweaked presentation *)
Integrate[#, {y, 0, 1}] &[
HoldForm[Plus[##]] & @@ MonomialList[ReleaseHold[integrand]]]
`

and you get

$$\int_0^1 \left(-y^2-y+2\right) \, dy$$