How can I extract part of a held expression without evaluating it?

Part[HoldForm[w = 3],1,1]
(* w *)
w = 4
Part[HoldForm[w = 3],1,1]
(* 4 *)

Here I would like to obtain "w" again, not its current value. How can I extract the unevaluated symbol on the lhs of the definition, if the symbol has a value already before the definition is made in the hold form? "Part" seems to always evaluate the extracted part of the held expression. Is there a way to prevent this? Of course the problem is the same when I extract part of the rhs of the definition or when I extract any part of any held expression. I would like to use this inside of a function which takes as the argument any expression. This expression is then held and extractions made from it. So the solution should work without knowing in advance what the symbol on the lhs of the definition is and whether it is bound or not.

Thanks for help!

  • 5
    $\begingroup$ Use Extract instead, e.g., Extract[HoldForm[w = 3], {1, 1}, Hold] $\endgroup$
    – Carl Woll
    Sep 13, 2018 at 21:25
  • $\begingroup$ @CarlWoll, thanks, this is indeed what I was looking for. But this returns Hold[w]. How do I extract "w" from Hold[w], e.g. for printing it? $\endgroup$ Sep 13, 2018 at 21:56
  • $\begingroup$ Perhaps you could use HoldForm instead of Hold? $\endgroup$
    – Carl Woll
    Sep 13, 2018 at 22:00
  • $\begingroup$ Yes. Thanks again! $\endgroup$ Sep 13, 2018 at 22:18
  • 3
    $\begingroup$ w = 4; Hold[ w = 3 ][[ {1}, 1 ]] $\endgroup$
    – Kuba
    Sep 14, 2018 at 5:57

1 Answer 1


Like so:


Note that MakeBoxes is also HoldAllComplete, so it won't allow HoldForm to leak no matter what is inside it.

  • 1
    $\begingroup$ Why not HoldForm directly? Original Hold is a weak link anyway. $\endgroup$
    – Kuba
    Sep 14, 2018 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.