12

I'd like to create a shortcut to convert a selected symbol, say \[Delta], into its rough equivalent formal symbol (i.e., \[FormalCapitalDelta]). That is, how does one create a shortcut to perform a front-end operation on a selected character?

This would only make sense for those single characters which have formal rough equivalents, so the shortcut should not do anything when multiple or a non-convertible single character is selected.

(comment: I'm finding formal symbols to be handy for function definitions where SetDelayed isn't a great idea (or turning an expression into a function Turning Expression into a Function: e.g., instead of

f[x_] :=  expensiveSymbolicFunction[x]

use

f[\[FormalX]_] =  expensiveSymbolicFunction[\[FormalX]]

)

I find \[FormalCapitalDelta] (esc-.CapitalDelta-esc) tedious to type.

Craig Carter
  • 4,416
  • 16
  • 28

2 Answers2

15

Here's an attempt:

Button["Formalize",
 NotebookWrite[EvaluationNotebook[], 
  NotebookRead[EvaluationNotebook[]] /.
   s_String /; StringLength@s == 1 :>
    With[{res = ToExpression[
        "\\[Formal" <> StringReplace[
          CharacterName[s],
          {
           "LatinSmallLetter" ~~ l_ :> l, 
           "LatinCapitalLetter" ~~ l_ :> "Capital" <> l
           }
          ] <> "]"
        ]},
     ToString@res /; ! FailureQ@res
     ]
  ]
 ]

enter image description here

As can be seen, clicking the button replaces all single-letter symbols in the current selection by their formal counterpart. This is done by first getting the name of the symbol using CharacterName, then processing it to handle the different cases. Finally, it is converted back to the correct symbol using ToString[ToExpression["\["<>name<>"]"]]

Lukas Lang
  • 33,963
  • 1
  • 51
  • 97
8

An alternative is to create a notebook containing all the automatic replacements using InputAutoReplacements:

enter image description here

Test:

enter image description here

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44