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:
