# print the name of the variable in a list without evaluation

a := az + 1
b := bz + 5
list := {a, b}


I'd like mathematica to print

a = az + 1
b = bz + 5


so basically it needs to first print the name of the variable in the list, followed by "=", and followed by the actual content of the variable.

update (1):

so here is something close to what I want

a := az + 1
b := bz + 5
list := {Hold@a, Hold@b}
Column[Table[Print[list[[i]], "=", ReleaseHold@list[[i]]], {i, 1, 2}]]


outputs:

Hold[a]=1+az
Hold[b]=5+bz


However, I don't know how to get rid of Hold[]. I'm also hoping there is a more elegant way.

• HoldForm works like Hold except that it isn't printed. Therefore I guess replacing Hold with HoldForm should satisfy your needs. – celtschk Apr 27 '12 at 8:07

Also

SetAttributes[f, HoldAll];
f[x_] := #[] <> "=" <> #[] &@ StringSplit[ToString@Definition@x, ":="]

• I like the use of Definition which is not new to me but is nonetheless something that I don't usually consider +1. – Andy Ross Apr 27 '12 at 5:42
• this one is a little hard to understand. can you add more info? # is the argument of a pure function, <> is string joint. What does &@ mean?I can see it starts the pure function. – kirill_igum Apr 27 '12 at 5:55
• @kirill_igum The function #[] <> "=" <> #[] & takes it's argument from the list StringSplit[ToString@Definition@x, ":="] – Dr. belisarius Apr 27 '12 at 5:59
• StringReplace[ToString[Definition[x]], ":=" -> "="] is simpler :) – rm -rf Apr 27 '12 at 5:59
• @R.M yep. you are right! – Dr. belisarius Apr 27 '12 at 6:01
StringForm[" =  \n = ", HoldForm[a], a, HoldForm[b], b]


or

  StringForm[" =  \n = ", Defer[a], a, Defer[b], b]


both give EDIT: To deal with the az=1 issue noted by belisarius, i steal Andy's OwnValues-based approach with a slight variation:

  SetAttributes[prntHF, {HoldAll, Listable}];
prntHF[sym_] := (OwnValues[sym] /.
{RuleDelayed[Verbatim[HoldPattern][lhs_], rhs_]}
:> {HoldForm[lhs], HoldForm[rhs]})


with StringForm

  StringForm[" 1 = 2 \n 3 = 4\n5 = 6",
Sequence @@ Sequence @@@ prntHF[{a, b, az}]]


to get Of course, another version

  SetAttributes[prntHF2, {HoldAll, Listable}];
prntHF2[sym_] := (OwnValues[sym] /.
{RuleDelayed[Verbatim[HoldPattern][lhs_], rhs_]} :>
Row[{HoldForm[lhs], " = ", HoldForm[rhs]}])


would be much easier to use:

  Column[prntHF2[{a, b, az}]]

• HoldForm is a very useful trick for this sort of thing. I'm surprised my own answer didn't include it :) +1 – Andy Ross Apr 27 '12 at 5:44
• +1 This reminded me why I hate the controlstrings of StringForm[] – Dr. belisarius Apr 27 '12 at 5:49
• @Andy, thank you for the vote. Actually, I was just thinking about OwnValues the same way :) – kglr Apr 27 '12 at 5:49
• mmm does not work if you set az=1 – Dr. belisarius Apr 27 '12 at 5:52
• @belisarius, me too:) One those things that I need to check docs every time I use it. – kglr Apr 27 '12 at 5:55

This seems to work. I'm doubtful that it is very robust though.

SetAttributes[printVar, {HoldAll, Listable}]

printVar[a_] := Row[{Defer[a], " = ", OwnValues[a][[1, 2]]}]


For example...

Column[printVar[{a,b}]]

==> a = 1 + az
b =  5 + bz


Edit:

Due to @belisarius' comment regarding setting az to some value.

printVar[a_] :=
Row[{Defer[a], " = ",
OwnValues[a] /. {RuleDelayed[_, expr_]} :> HoldForm[expr]}]


Which is admittedly sort of ugly.