1
$\begingroup$

I want to get the name of a variable instead of its value in output. Say I have a function in which I have vectors e={1,0,0,1} and f={1,1,0,1}, and the input for the function is a symbol (e or f). Now, in output, I want to make a grid which shows the symbol entered in input in one row, and its value in the second row. How would I do that? I tried the function HoldForm, but that did not work.

$\endgroup$
2
$\begingroup$
e = {1, 0, 0, 1};
SymbolName[Unevaluated[e]]

Result:

e

Another option, if to be used inside a function (as noted in your comment), would be to design the function to accept the symbol name instead of the symbol (or require both to make things clearer). Here is a version with just the symbol name. The symbol is extracted in the function.

x = 5;
Plus5[symbolname_] := 
 (s = Symbol[symbolname];
     s = s + 5;
   Return[{symbolname, s}])

Plus5["x"]

This returns:

{x, 10}
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ It does not work if I enter it through a function. The function has an input, let's call it sign_, then I have the e vector inside of the function. So my goal is to input function[e], and get e in the output later. $\endgroup$ – Drobdien May 8 at 20:09
  • $\begingroup$ The Symbol function was what finally solved this problem, thank you very much! $\endgroup$ – Drobdien May 9 at 13:48
2
$\begingroup$

HoldForm[] is surely an important ingredient. The other important bit is that whatever function you write should have an attribute like HoldFirst or HoldAll to prevent premature evaluation. As a simple example:

SetAttributes[gg, HoldFirst];
gg[x_Symbol] := Column[{Row[{HoldForm[x], "="}], x}]

Then,

ff = {1, 1, 0, 1};

gg[ff]
   ff=
   {1, 1, 0, 1}
$\endgroup$
  • $\begingroup$ I tried this and it doesn't seem to work, which is probably my fault. If we just forget the grid or any formatting, just a way to get a variables name instead of its value, perhaps as a string. How would I do that? $\endgroup$ – Drobdien May 8 at 17:13
  • 2
    $\begingroup$ "it doesn't seem to work" - Can you try this on a fresh kernel, then? $\endgroup$ – J. M.'s technical difficulties May 8 at 17:21

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