Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
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.

share|improve this question
1  
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
add comment

3 Answers

up vote 9 down vote accepted

Also

SetAttributes[f, HoldAll];
f[x_] := #[[1]] <> "=" <> #[[2]] &@ StringSplit[ToString@Definition@x, ":="]
share|improve this answer
    
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
1  
@kirill_igum The function #[[1]] <> "=" <> #[[2]] & takes it's argument from the list StringSplit[ToString@Definition@x, ":="] –  belisarius Apr 27 '12 at 5:59
5  
StringReplace[ToString[Definition[x]], ":=" -> "="] is simpler :) –  rm -rf Apr 27 '12 at 5:59
    
@R.M yep. you are right! –  belisarius Apr 27 '12 at 6:01
add comment
StringForm["`` = `` \n`` = ``", HoldForm[a], a, HoldForm[b], b]

or

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

both give

enter image description here

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`\n`5` = `6`", 
  Sequence @@ Sequence @@@ prntHF[{a, b, az}]]

to get

enter image description here

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}]]
share|improve this answer
    
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
2  
+1 This reminded me why I hate the controlstrings of StringForm[] –  belisarius Apr 27 '12 at 5:49
    
@Andy, thank you for the vote. Actually, I was just thinking about OwnValues the same way :) –  kguler Apr 27 '12 at 5:49
    
mmm does not work if you set az=1 –  belisarius Apr 27 '12 at 5:52
    
@belisarius, me too:) One those things that I need to check docs every time I use it. –  kguler Apr 27 '12 at 5:55
show 4 more comments

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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