6
$\begingroup$

I have a lists of variable names that include greek letters.

names = {"a", "b", "μ", "Σ"}

I would like to construct a lists of formal symbols from these such the above list will return

{\[FormalA], \[FormalB], \[FormalMu], \[FormalCapitalSigma]}

Mathematica graphics

I have tried mapping Symbol with no luck.

Symbol["$" <> #] & /@ names
{$a, $b, $μ, $Σ]}

How do I get a list of formal symbols from a list of variable names.

$\endgroup$
4
$\begingroup$

The ploddingly straightforward method:

SetAttributes[formalize, Listable];
formalize[s_String] /; StringLength[s] == 1 := 
          Module[{la = Alphabet[], ga = Alphabet["Greek"]},
                 Which[MemberQ[la, s], FromCharacterCode[ToCharacterCode[s] + 63391],
                       MemberQ[ToUpperCase[la], s], 
                       FromCharacterCode[ToCharacterCode[s] + 63449], 
                       MemberQ[ga, ToLowerCase[s]], 
                       FromCharacterCode[ToCharacterCode[s] + 62627]]]
$\endgroup$
  • $\begingroup$ Your character manipulations put me on a different tract with some version 10 functions that results. Thanks! (+1) $\endgroup$ – Edmund Nov 1 '17 at 15:30
3
$\begingroup$

Solution inspired by @JM's answer which resulted in finding CharacterName. With this the FullForm of the formal symbols can be obtained as strings and Symbol used to create the symbols.

ClearAll[formalSymbol];
formalSymbol[s_String] /; StringLength[s] == 1 :=
 Which[

  (* English characters *)
  StringContainsQ[Alternatives @@ Alphabet[Language -> "English"], IgnoreCase -> True]@s,
  Symbol@StringJoin[{"\\", "[Formal", If[UpperCaseQ@s, "Capital", ""],ToUpperCase@s, "]"}],

  (* Greek characters *)
  StringContainsQ[Alternatives @@ Alphabet[Language -> "Greek"], IgnoreCase -> True]@s,
  Symbol@StringJoin[{"\\", "[Formal", CharacterName@s, "]"}],

  True,
  Message[Symbol::symname, s]
  ]

Then

formalSymbol /@ {"a", "A", "σ", "Σ"}
{\[FormalA], \[FormalCapitalA], \[FormalSigma], \[FormalCapitalSigma]}

Mathematica graphics

$\endgroup$

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.