7
$\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$

2 Answers 2

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$
1
  • $\begingroup$ Your character manipulations put me on a different tract with some version 10 functions that results. Thanks! (+1) $\endgroup$
    – Edmund
    Nov 1, 2017 at 15:30
4
$\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 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.