4
$\begingroup$

Considering the Lukas Lang’s solution to formalize symbols with a Button, given in the post: How can I create a shortcut to convert "normal Symbol" to Formal Symbol?, I've created the following Button to Unformalize:

Button["Unformalize", 
 NotebookWrite[EvaluationNotebook[], 
 NotebookRead[EvaluationNotebook[]] /. 
 s_String /; StringLength[s] == 1 :> 
 With[{char = StringReplace[CharacterName[s], {"Formal" -> ""}]}, 
 If[MemberQ[{"Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta",
             "Eta", "Theta", "Iota", "Kappa", "Lambda",
              "Mu", "Nu", "Xi", "Omicron", "Pi", "Rho",
              "Sigma", "Tau", "Upsilon", "Phi", "Chi", 
              "Psi", "Omega"}, char], "\\[" <> char <> "]",
 If[StringContainsQ[char, "Capital"], StringReplace[char, "Capital" ~~ l_ :> l],
 ToLowerCase[char]]]]]]

The "Unformalize" button works perfectly when applied to individual formal symbols, but when I select a list of formal symbols, the button does not behave as expected. It seems like the transformation is not applied to each symbol in the list.

unformalize

I attempted to modify the logic to process each element in a list recursively, but so far, I haven't managed to achieve the desired functionality.

How can I modify the "Unformalize" button so that it works correctly for both individual symbols and lists of symbols? Any help or insights would be greatly appreciated. Thanks in advance!

$\endgroup$
2
  • 1
    $\begingroup$ Can you add an example on which it fails? Also, you forgot to handle single letter characters that don't contain 'Formal' in the name, currently you are going to ToLowerCase them breaking RawSpace etc. $\endgroup$
    – Kuba
    Commented Dec 9 at 20:49
  • $\begingroup$ @Kuba I've added a GIF, please see the update! $\endgroup$ Commented Dec 9 at 20:56

1 Answer 1

4
$\begingroup$

Ok, so as I mentioned in the comment you forgot to handle single char strings that didn't contain Formal and you ended up converting "RawComma" to "rawcomma". Here is a fixed rule:

NotebookRead[EvaluationNotebook[]] /. 
 s_String /; StringLength[s] == 1 :> 
 Catch @ Module[
   {
     char = CharacterName[s],
     greek = {"Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta", "Eta", "Theta", "Iota", "Kappa", "Lambda", "Mu", "Nu", "Xi", "Omicron", "Pi", "Rho", "Sigma", "Tau", "Upsilon", "Phi", "Chi", "Psi", "Omega"}
   }
 , If[ StringFreeQ["Formal"]@ char, Throw @ s]

 ; char = char // StringDelete["Formal"]

 ; If[MemberQ[greek, char], Throw["\\[" <> char <> "]"]]

 ; If[StringContainsQ[char, "Capital"], Throw @ StringReplace[char, "Capital" ~~ l_ :> l]]

 ; ToLowerCase[char]
 ]
]
$\endgroup$
1
  • $\begingroup$ Your solution works perfectly, really appreciate your guidance. Thanks, Kuba! :) $\endgroup$ Commented Dec 9 at 21:46

Your Answer

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

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