Suppose I have an expression with Greek symbols in it. How would I convert that expression to another expression where the Greek symbols have been replaces with their names in plain text? I've included a small example of what I mean in the picture. Does anybody know of an easy way to do this?

enter image description here

up vote 12 down vote accepted

Extract all Greek letters from the documentation and make replacement rules:

nb = Get @ FileNameJoin[{$InstallationDirectory, "Documentation", "English", "System", 
     "Tutorials", "LettersAndLetterLikeForms.nb"}];

letters = Cases[nb, StyleBox[s_String, "TR"] :> s, {-2}];

letters = DeleteCases[letters, "π" | "∈"];  (* reserved Symbols *)

names = StringTake[ToString @ FullForm @ #, {4, -3}] & /@ letters;

rules = MapThread[Symbol@# -> Symbol@ToLowerCase@#2 &, {letters, names}];

Then simply:

α x + β y /. rules
alpha x + beta y

If any of the Symbols may already have assignments and you are working with held expressions use instead:

rulesHeld = MapThread[HoldPattern @@ MakeExpression@# :> 
   Evaluate[Unevaluated @@ MakeExpression@ToLowerCase@#2] &, {letters, names}];

In a comment Oleksandr posted an undocumented System function which could be used as follows:

names = Join @@ System`Private`NameCodeLookup @ ToCharacterCode @ letters;

Complete replacement rules in case anything goes wrong with the extraction:

{α->alpha,  ρ->rho, Ε->capitalepsilon,  Ψ->capitalpsi
β->beta,    ϱ->curlyrho,    Ζ->capitalzeta, Ω->capitalomega
γ->gamma,   σ->sigma,   Η->capitaleta,  Ϝ->capitaldigamma
δ->delta,   ς->finalsigma,  Θ->capitaltheta,    Ϟ->capitalkoppa
ϵ->epsilon, τ->tau, Ι->capitaliota, Ϛ->capitalstigma
ε->curlyepsilon,    υ->upsilon, Κ->capitalkappa,    Ϡ->capitalsampi
ζ->zeta,    ϕ->phi, Λ->capitallambda,   Σ->capitalsigma
η->eta, φ->curlyphi,    Μ->capitalmu,   Π->capitalpi
θ->theta,   χ->chi, Ν->capitalnu,   ϵ->epsilon
ϑ->curlytheta,  ψ->psi, Ξ->capitalxi,   μ->mu
ι->iota,    ω->omega,   Ο->capitalomicron,  Υ->capitalupsilon
κ->kappa,   ϝ->digamma, Π->capitalpi,   ς->finalsigma
ϰ->curlykappa,  ϟ->koppa,   Ρ->capitalrho,  ϝ->digamma
λ->lambda,  ϛ->stigma,  Σ->capitalsigma,    ϟ->koppa
μ->mu,  ϡ->sampi,   Τ->capitaltau,  ϛ->stigma
ν->nu,  Α->capitalalpha,    Υ->capitalupsilon,  ϡ->sampi
ξ->xi,  Β->capitalbeta, ϒ->curlycapitalupsilon, 
ο->omicron, Γ->capitalgamma,    Φ->capitalphi,  
ϖ->curlypi, Δ->capitaldelta,    Χ->capitalchi}

(Sorry for the formatting; I can't think of a nice way to paste this here.)

  • Slick! Thanks! Also, btw, it seems like phi and curlyphi got switched. I can't tell why. – jmbejara Feb 14 '15 at 12:51
  • @jmbejara They seem to be correct. I suspect a font issue? Try copying and pasting the rules into Mathematica -- they appear to work correctly for me. – Mr.Wizard Feb 14 '15 at 13:00
  • You're right. It's a font thing (just on my end?). When I copy-past the last code block into Mathematica, everything is correct. However, in this post, they appear switched (for me). – jmbejara Feb 14 '15 at 13:05
  • @jmbejara In my browser both look like curly phi, but \[CurlyPhi] is larger. I hadn't noticed before. – Mr.Wizard Feb 14 '15 at 13:10
  • 1
    Also, System`Private`CharacterNames[] lists all the named characters and FromCharacterCode@System`Private`NameCodeLookup@System`Private`CharacterNames[] gives a string of them (i.e., System`Private`NameCodeLookup is a bidirectional function). Extracting the Greek ones may be more difficult. (I also found that LetterQ recognises Greek letters, but contrary to its documentation, LetterCharacter does not.) – Oleksandr R. Feb 14 '15 at 19:21

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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