[pmwiki-users] Chemical formula markup (Karl Schilke)

john.rankin at affinity.co.nz john.rankin at affinity.co.nz
Thu Oct 30 14:46:26 CDT 2008

> Subject: [pmwiki-users] Chemical formula markup
> To: pmwiki-users at pmichaud.com
> 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"
> (diphenylethylamine).

Here are a few (untested) ideas which may help.

I would be inclined to signal "this is a chemical formula" with an
explicit markup character. Because a formula is a string of known
characters, I think this can be done with just an opening tag.
Following your lead suggesting & and avoiding a possible
collision with &entity; markup, we need a regular expression
to match a general chemical formula, turn numbers into
subscripts and charges into superscripts.

The charge presents a possible challenge, to find a pattern
that will handle both NO3- and SO42- correctly. To begin, I'm
inclined to say the markup ++ is turned into <sup>2+</sup>.

Here is a suggested markup rule (I haven't tested this):

Markup('chem', 'inline',
    "BeChemical('$1').('$2' ? BeCharged('$2') : '')");

function BeChemical($f) {
    return preg_replace('/(\d)/', "<sub>$1</sub>");
function BeCharged($c) {
    $num = strlen($c);
    $c = $c[0];
    return '<sup>'.(($num>1) ? "$num$c" : $c).'</sup>';

The pattern could be refined to match only valid element
names, but on the other hand, the & tells the wiki that
this is a formula, and the chemist is likely to do a better
job than the algorithm in specifying a valid formula.

Hope this helps

More information about the pmwiki-users mailing list