Skip to content

Molecules

A molecule is an ordered sequence of atoms and groups, with an optional leading stoichiometric coefficient.

H_2O
H 2 O
CO_2
C O 2
C_6H_12O_6
C 6 H 12 O 6

Each atom carries its own subscript. The model is Molecule(nodes: [Atom, Atom, ...]).

Leading digits on a molecule are its coefficient:

2H_2O
2 H 2 O
3H_2
3 H 2

The Molecule#coefficient field holds the string. The MathML renderer emits it as an <mn> before the atoms.

Parenthesised sub-formulas are groups. A group can carry an outer multiplicity.

Ca(OH)_2
Ca ( O H ) 2
(NH_4)_2SO_4
( N H 4 ) 2 S O 4

For Ca(OH)_2:

  • Atom Ca
  • Group OH with multiplicity 2, bracket :paren

For (NH_4)_2SO_4:

  • Group NH_4 with multiplicity 2
  • Atoms S, O with subscripts 4

AsciiChem supports three bracket kinds:

BracketOpen / CloseUse
Paren( )Generic grouping (Ca(OH)_2)
Square[ ]Coordination complexes ([Fe(CN)_6]^4-)
Brace{ }Optional / rare

The bracket kind is preserved through round-trip — (OH)_2 stays as (OH)_2, not [OH]_2.

Square brackets wrap coordination complexes. The complex’s charge sits outside the brackets.

[Fe(CN)_6]^4-
[ Fe ( C N ) 6 ] ^4-
[Co(NH_3)_6]^3+
Install the asciichem gem to see this rendered.

These are parsed as Group(bracket: :square, nodes: [...]) followed by an Atom with the charge. (In v1, the charge attaches to the last atom; v2 may promote it to a complex-level attribute.)