0
$\begingroup$

I have the following list of indexed variable names:

list = {x1,x2,x3,x4};

I would like to build a list of formal symbols from the above list, i.e.

{\[Formax1], \[Formax2], \[Formalx3], \[Formalx4]}

I've tried mapping Symbol to the names in the list, but it doesn't work. ㋡

$\endgroup$
14
  • 1
    $\begingroup$ See also this. $\endgroup$
    – user293787
    Aug 17, 2022 at 10:32
  • 1
    $\begingroup$ The input {\[Formax1],\[Formax2],\[Formalx3],\[Formalx4]} gives a syntax error (V12.3). $\endgroup$
    – user293787
    Aug 17, 2022 at 10:46
  • 1
    $\begingroup$ Your variable names are already symbols. You can see this by looking the full form of an element. e.g.: list[[1]] //FullForm $\endgroup$ Aug 17, 2022 at 11:31
  • 1
    $\begingroup$ Does Symbol /@ StringReplace[ToString /@ list, "x" -> "\[FormalX]"] give you what you want? $\endgroup$
    – LouisB
    Aug 17, 2022 at 11:40
  • 1
    $\begingroup$ Some more context would be helpful. \[Formalx4] is not a named character. There are named characters that have "Formal" in their name, but you can't just make up new characters on the fly like this. What use do you need these "formal" characters for? $\endgroup$
    – lericr
    Aug 17, 2022 at 15:31

1 Answer 1

2
$\begingroup$

I think you need to be very careful--you might not be able to get what you want. You mentioned that you're getting a list of symbols from a function. Let's mimic that:

symbolGenerator[] := {x1, x2, x3, x4};
symbolGenerator[]
(* returns {x1, x2, x3, x4} *)

Okay, let's say you wanted to use Unique on these so that you avoid name collisions. Well, it should work fine if none of the symbols were defined.

Unique[symbolGenerator[]]
(* {x1$7526, x2$7526, x3$7526, x4$7526} *)

But let's try again in a situation where there's actually a name collision:

ClearAll[x1, x2, x3, x4, symbolGenerator];
symbolGenerator[] := {x1, x2, x3, x4};
x3 = 17;
Unique[symbolGenerator[]]

Now we get a message saying "17 is not a symbol or a valid symbol name".

If you implemented symbolGenerator, then you could return strings instead of symbols; you could wrap the output in Hold or maybe Unevaluated; you could add a context directly to the symbols; or you could use Unique to start with. For example:

symbolGenerator[] := Unique[{"x1", "x2", "x3", "x4"}]
(* or *)
symbolGenerator[] := Symbol[StringJoin["special`", #]] & /@ {"x1", "x2", "x3", "x4"}

If you didn't implement symbolGenerator, then we'd probably need to know more about that function before making concrete suggestions.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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