Ever type a chemical formula like C6H10O5, then stop to hunt down each subscript number one at a time?
It is a tiny formatting task, but it can turn into a surprisingly tough detour when formulas show up throughout your notes, reports, or replies.
A recent question sent me looking for a smoother option.
This JavaScript Snippet lets you enter a formula the way you normally would:
C6H10O5
Then TextExpander returns:
C₆H₁₀O₅
One Snippet can handle different formulas, so you do not need to build a separate one for H2O, H2SO4, C6H10O5, or every new compound you type.
Here is how to set it up, or you can get it from our public groups found here
-
Create a new Snippet.
-
Set the Content Type to JavaScript.
-
Paste in the code below.
-
Give it an abbreviation, such as ;sub.
var formula = “%filltext:Formula%”;
var subs = {
“0”: “₀”,
“1”: “₁”,
“2”: “₂”,
“3”: “₃”,
“4”: “₄”,
“5”: “₅”,
“6”: “₆”,
“7”: “₇”,
“8”: “₈”,
“9”: “₉”
};
formula.replace(/[0-9]/g, function (digit) {
return subs[digit];
});
When you expand the Snippet, a Formula field will appear. Type your formula using regular numbers, press Enter, and TextExpander will return it with subscript digits.
JavaScript can look a little intimidating, but this one is really just a tiny translator for the numbers 0 through 9.
A small heads-up: the Snippet converts every digit you enter. It works best for a standalone formula. If you enter a full equation with a leading coefficient, that number will become a subscript too.
Try it today with one formula you use regularly and see whether it saves you a formatting detour.
Question for the community
What text containing subscripts do you find yourself typing repeatedly?