# Automatic formatting of isotope symbols in a text cell

InputAutoReplacements is an obvious choice for formatting isotope names in text cells. I'd like to be able to type "1H", for example, and when I hit the space bar have it replaced with Row[{Superscript["",1],"H"}] inline. But my initial attempts at this have not been productive.

Is it possible to use string patterns in InputAutoReplacements, such as the pattern in the following StringReplace?

StringReplace["14C",
n:NumberString~~atom:(CharacterRange["A","Z"]~~Repeated[CharacterRange["a","z"],{0,1}]
~~WordBoundary) :>
ToString[Row[{Style[Superscript["",Style[n,Smaller]],
ScriptBaselineShifts->{Automatic,.4}],atom}],


I expected that entering that pattern into the Option Inspector (Global preferences for InputAutoReplacements, using RuleDelayed) would cause occurrences of "1H " typed into a text cell to raise the "1" to a superscript. Nothing happens. You might imaging that formatting the superscripts according to the above rule by hand is tedious.

-

You can set it as an option as follows —

SetOptions[SelectedNotebook[], InputAutoReplacements -> {"1H" -> SuperscriptBox["H", "1"]}]


Now if you enter $1\mathrm{H}$ in a text cell, it will automatically convert it to $\mathrm{H}^1$.

Another possibility is using InputAliases, where you enter the replacement as Esc1HEsc. IMHO, this is a better way to handle auto replacements, because the intent to replace is quite clear, whereas with the first approach, it'll replace it even when you don't want it to (e.g., addresses in some countries). You can set an alias as:

SetOptions[SelectedNotebook[], InputAliases -> {"1H" -> SuperscriptBox["H", "1"]}]


### Creating auto-replacements for all isotopes:

I don't think creating a blind pattern based rule replacements to handle isotope auto-replacements is a good way to approach it, because you wouldn't want invalid isotopes like 2C or 207U to actually be replaced — you'd rather they be untransformed so that it serves as a visual error/typo check. A better and safe approach would be to whitelist the known isotopes and create rules only for those isotopes.

Fortunately, you can use Mathematica itself to query this info and create your custom rules as follows:

(* get info about known isotopes *)
isotopeInfo = {ElementData[#, "Abbreviation"], ElementData[#, "KnownIsotopes"]} & /@
ElementData[];

(* function to assemble the superscripts *)
createSuperscripts[{e_String, w_List}] :=
ToString[#] <> e -> SuperscriptBox[e, ToString[#]] & /@ w

(* map over all elements and create the aliases *)
isotopeRules = createSuperscripts /@ isotopeInfo // Flatten;
SetOptions[SelectedNotebook[], InputAliases -> isotopeRules]


With this set, you can now enter valid isotopes between two Esc presses and it will convert it to a superscript.

You can also extend this to include the atomic number by default as seen in common notation (although it is redundant info). Here's an example (you can do the styling as you please) that shows how, and it also uses an \[InvisibleSpace] from my very first question

(*get info about known isotopes*)
isotopeInfo = {ElementData[#, "Abbreviation"],
ElementData[#, "AtomicNumber"], ElementData[#, "KnownIsotopes"]} & /@ ElementData[];

(*function to assemble the subsuperscripts*)
createSuperscripts2[{e_String, n_Integer, w_List}] :=
ToString@# <> e -> RowBox[{SubsuperscriptBox["\[InvisibleSpace]", ToString@n,
ToString@#], e}] & /@ w


The last two steps are the same (except, use this function instead). Here's how it looks on mine:

-
Thanks for the tip on InputAliases. I guess I'm asking about arbitrary string patterns (with blanks) in the InputAutoReplacements, so I don't have to have a rule for each isotope. –  JxB Apr 16 '12 at 22:47
@JxB Please see my edit for a better way than replacements –  The Toad Apr 16 '12 at 23:17
@R.M.: In SetOptions[SelectedNotebook[], InputAutoReplacements -> {"1H" -> SuperscriptBox["H", "1"]}] you seem to have the superscript to the right of the element's symbol. But shouldn't superscript should appear to the $left$ of the element notation, as the O.P. indicated he wanted? –  murray Apr 17 '12 at 15:34
@murray Oh the first one? Yeah.. well, it's just a switch and it's more a proof of concept.. I was more careful with the second example :) –  The Toad Apr 17 '12 at 15:42
@R.M +1 Congratulations on 10 000 reputation and the second gold badge! –  Artes Apr 17 '12 at 16:10