[pmwiki-users] Chemical formula markup

Karl Schilke schilkek at onid.orst.edu
Tue Oct 28 18:56:32 CDT 2008

I run the Wiki for our laboratory group in Chemical Engineering at  
Oregon State. Since we often write text that includes chemical  
formulae, I have introduced this markup in wikiroot/local/config.php:

     Markup('chemsub', '>links', '/([CHNOPS\)])(\d+)/', '$1<sub>$2</sub>');

The goal is to insert a <sub></sub> around each string of numbers in  
chemical formulae like these: "CH3OCH2CH2NH2" (2-methoxyethylamine),  
"CH3C=OCH3" (acetone), "H3PO4" (phosphoric acid) or "(C6H5)2NCH2CH3"  

This hack works tolerably well, but it occasionally catches things  
like the numbers in Library of Congress call numbers (e.g. both  
numbers in "TP7871.N45") and telephone prefixes (e.g. the 737 in  
"(541)737-4600"). Waiting until after the 'links' markup prevents it  
from incorrectly "fixing" page or attachment names like  

I'm fairly computer savvy, but I've reached the limit of my abilities  
at this point. What I'd really like to do is match any word that  
contains only the atoms CHONPS, optional parentheses, and numbers.  
Ideally, I'd also like to match the common two-letter atoms (Cl, Br,  
Si, Na, etc.) to handle compounds like 'CH2Cl2' or 'Na2HPO4'.  
Correctly detecting superscript ionic charges would also be a great  
help. These charges are always between +9 to -9, so simply matching a  
terminal /\d(+-)$/ string, e.g. the "2+" in 'Cu2+' or the "2-" in  
'SO42-', would work for this.

While it's convenient to write the formulae inline without markup,  
these more advanced fixups could be invoked by surrounding the formula  
with a pair of terminator characters (e.g. '&(CH3)2C6H5N=NC6H5SO42-&').

I've looked for a recipe for this, but without success. I'd appreciate  
any improvements or suggestions to my simple markup rule. It seems  
that there must be others using PmWiki for scientific purposes; a  
well-behaved chemistry markup recipe would probably be useful to a  
number of sites.

Thanks in advance for any ideas,

     -Karl "Rat" Schilke, Oregon State University, Chemical Engineering

"Much of the difference between rodent and human intelligence can be
attributed to quantity, not quality." -Kevin Warwick

More information about the pmwiki-users mailing list