Unevaluated symbols [duplicate]

This question already has an answer here:

How can I test properties of a symbol from the string name without the symbol completely evaluating

3 answers

I often find it useful to define a symbol from strings (often in a loop in which I define other symbols). This I accomplish with Symbol (and often ToString).

However, if the symbol has already been defined, my call to Symbol returns not the symbol, but that to which the symbol evaluates.

This may arise rarely, but I stumbled upon it when automatically generating usage messages, as in the following example.

vars = {x,y};
x$dN = {x1, x2};
y$dN = {y1, y2};
For[ i = 1, i <= Length[vars], i++, Evaluate[ Symbol[ ToString[vars[[i]]] <> “$dN”
“generic (no argument) thermodynamic quantity vector”

(* Symbol::symname: The string “{x1, x2}$dN” cannot be used for a symbol name.
A symbol name must start with a letter followed by letters and numbers” *)

This is the obvious error because I’m trying to define the usage for x$dN, which evaluates to x$dN = {x1, x2}.

So my question is: can we sometimes leave a symbol unevaluated, yet use it as a symbol?



1 Answer


In your example it is possible to assign a usage message although the variable has a value. Short example:

var = 1;
Function[v, v::usage = “blue”, HoldFirst] @@ MakeExpression[“var”]

The trick is to keep the variable var unevaluated all the time which can be tricky.

In my example above this is accomplished by three things:

MakeExpression does what Symbol does in your code, but it wraps the result automatically in HoldComplete. Therefore, var will not be evaluated at this point
Function[….,HoldFirst] creates a functions which too, holds its first argument unevaluated.
MessageName which is the symbol::msg operator too holds its first argument. Check Attributes[MessageName] to see that.



halirutan, thanks for the thorough explanation!
– Rico Picone
May 12 ’14 at 20:01