1
$\begingroup$

I'd like to display only the variable (indexed variable) name (in symbol form, not in string form) instead of its value in Mathematica. Say I have a variable foo[i] with i being the index, How do I get something like {foo[1], foo[2]} as output?

I have tried i = 1; HoldForm[foo[i]] but Mathematica seems to hold all evaluations, including replacing the index i with its value. HoldForm[foo[i]] /. i -> i also won't work.

There exists another thread in SE that connects with this question, but it doesn't seem to work either.

Display variable name instead of value

$\endgroup$
  • $\begingroup$ HoldForm[foo[i]] /. i -> 1 or HoldForm[foo[i]] /. i -> j will work. $\endgroup$ – Jinxed Mar 18 '15 at 14:33
  • $\begingroup$ Also With[{i=1},HoldForm[foo[i]]] works. As does i=1;With[{i=i},HoldForm[foo[i]]]. $\endgroup$ – celtschk Mar 18 '15 at 16:43
  • $\begingroup$ Related: "Elegant manipulation of the variables list." $\endgroup$ – Alexey Popkov Mar 19 '15 at 12:14
4
$\begingroup$

I believe this is what you're seeking:

HoldForm[foo] /@ Range[2]

and if you want individual elements:

(HoldForm[foo] /@ Range[2])[[1]]

and

(HoldForm[foo] /@ Range[2])[[2]]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for you answer. Yes this is working, but what if I want to extract the elements in the list? HoldForm[foo] /@ {i}[[1]] won't work. $\endgroup$ – Vincent Wang Mar 18 '15 at 14:40
  • $\begingroup$ Well I also discovered HoldForm[foo]@i :) $\endgroup$ – Vincent Wang Mar 18 '15 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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