Defer, Hold, Unevaluated, Inactivate — which one when?

What are some guidelines on which function to select from among the superficially similar functions Defer, Hold, Unevaluated, and Inactivate?

Consider, for example:

Defer[Defer[Sum[1/k^2, {k, 1, Infinity}]]]
Evaluate @@ %

Hold[Sum[1/k^2, {k, 1, Infinity}]]
ReleaseHold[%]

Unevaluated[Sum[1/k^2, {k, 1, Infinity}]]
Evaluate @@ %

Inactivate[Sum[1/k^2, {k, 1, Infinity}], Sum]
Activate[%]

• Re: HoldForm[] vs. Defer[], the output of the latter can be copied into a new cell and evaluated, while the former's output will still remain held after copying into a new cell. – J. M.'s technical difficulties Nov 7 '15 at 17:10
• SO duplicate (does not cover Inactivate). – Leonid Shifrin Nov 7 '15 at 17:47
• OK, that "SO duplicate" link helps. Now, where does Inactivate fit? – murray Nov 8 '15 at 19:08

The SO duplicate Preventing evaluation of Mathematica expressions does a nice job of explaining the differences among and use-cases for Defer, Hold, and Unevaluated.

Here, I can give at least one use-case for Inactivate for which the other functions are less useful (if not completely useless). There are plenty of situations in which I want to control the algebraic steps that Mathematica does automatically: sometimes I'm doing derivations---using Mathematica to get all of the negative signs right on the first try---where I want to see all of the steps in order that I can write them nicely in a paper later; sometimes I want to show the steps to some students; etc.

The problem is, of course, that Mathematica has all kinds of built-in automatic simplifications, and Simplify does what it wants. So I use Inactivate on some of the functions/Heads in the expression in order to stop the unwanted automatic simplifications while still being able to use the rest of the automatic simplifications.

Here's a contrived example, taken from a previous answer of mine. We're trying to show that the expression

(1 - Tan[x])/(Sin[x] - Cos[x])


simplifies to

- 1/Cos[x]


or

- Sec[x]


but Sin[x]/Cos[x] automatically simplifies to Tan[x], so we can't do a Replacement to show the step:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] :> Sin[x]/Cos[x]
(* (1 - Tan[x])/(-Cos[x] + Sin[x]) *)


Instead, we can Inactivate all of the trigonometric functions:

expr = Inactivate[(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] :> Sin[x]/Cos[x], Tan | Sin | Cos]
expr // Simplify
% // Activate
(* (1 - Inactive[Sin][x]/Inactive[Cos][x])/(-Inactive[Cos][x] + Inactive[Sin][x]) *)
(* -(1/Inactive[Cos][x]) *)
(* -Sec[x] *)


Of course, we could have Replaced Heads instead, using sin, cos, etc., but Activate/Inactivate is a nice programmatic way of doing that that doesn't require a whole bunch of replacement rules, and you have the power of Activateing in steps:

Activate[1 - Inactive[Sin][x]/Inactive[Cos][x], Cos]
(* 1 - Sec[x] Inactive[Sin][x] *)


which can be quite nice.