Skip to content

Molecules

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

Source Rendered
H_2O CO_2 C_6H_12O_6
Install the asciichem gem to see this rendered.

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

Molecules can carry CML metadata via the @key("value") suffix:

Source Rendered
H_2O @name("Water")@inchi("InChI=1/H2O/h1H2")
Install the asciichem gem to see this rendered.

Supported annotations:

  • @name("...") — molecule name
  • @inchi("..."), @smiles("..."), @cas("...") — identifiers
  • @title("...") — molecule title
  • @formula("H 2 O 1") — concise formula
  • @label("solvent") — label
  • @mw("18.015") — any custom property
  • @meta("key","value") — metadata key-value pair

See Annotations for details.

Leading digits on a molecule are its coefficient:

Source Rendered
2H_2O 3H_2
Install the asciichem gem to see this rendered.

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

A whitespace-separated lowercase v or ^ after a molecule marks its phase state — mhchem’s precipitate and gas glyphs:

Source Rendered
AgCl v AgCl ^ Ag^+ + Cl^- -> AgCl v
Install the asciichem gem to see this rendered.

AgCl v carries state: :precipitate (typeset as the down arrow ↓, the solid falling out); AgCl ^ carries state: :gas (↑). The canonical text preserves the authored glyph — the round-trip contract — while the renderers typeset: MathML emits the arrow, HTML the entity, LaTeX \downarrow / \uparrow (mhchem’s own output form), linear SVG the unicode arrow. CML carries the state through the aci:state attribute, and the model JSON wire round-trips it losslessly.

A tight parenthesised phase after a molecule — mhchem’s spellings, grammar v0.11.0:

Source Rendered
NaCl(aq) H_2O(l) Fe(s) CO_2(g) AgCl(s) v
Install the asciichem gem to see this rendered.

The phase precedes the state mark (the phase is part of the formula; the mark is the annotation tail). Phases are lowercase-only by construction, so the group reading — element symbols are capitalised — can never collide: Fe(s) is solid iron while Fe(S) stays a sulfur group, and (l), (g), (aq) have no element readings at all. A spaced phase (AgCl (aq)) rejects — the mark vocabulary owns the spaced tail.

The canonical text round-trips the authored spelling verbatim; MathML typesets the phase as upright text, and CML carries it through the aci:phase attribute (mirroring aci:state). The model JSON wire round-trips it losslessly.

The mark is whitespace-separated and lowercase-only by construction: an uppercase V stays a vanadium atom, and the tight ^ forms keep their existing binding — AgCl^14C is the isotope prefix, Ca^2+ the charge superscript. A tight ^2+ after whitespace (Ca ^2+) rejects at parse. (aq) / (s) / (l) / (g) phase labels keep rendering through the quoted-text and group syntax; the state mark models the two glyphs mhchem typesets.

The mhchem middle-dot connective joins molecules at the formula level — CuSO4 . 5H2O hydrates:

Source Rendered
CuSO4 . 5H2O NaCl . KCl . CaCl2 CuSO4 · 5H2O
Install the asciichem gem to see this rendered.

The parse produces one n-ary Adduct node whose components are the dot-separated molecules (each component’s coefficient carries its multiplicity). The ASCII spelling is the period with whitespace on both sides; the literal middle dot (·) is accepted as the mhchem alias. The canonical text emits the ASCII . form while the renderers typeset: MathML the · glyph, HTML &middot;, LaTeX \cdot (mhchem’s own connective), linear SVG the unicode dot. The wire carries the adduct node (model v0.9.0) and CML the <aci:adduct> text channel.

Disambiguation by construction: the UNspaced period stays radical-suffix territory — Cl. is a chlorine with one radical electron and Cl.Fe reads as a radical-bearing molecule plus its continuation. Only the whitespace-separated period is the connective, so Cl., Cl.Fe, and CuSO4 . 5H2O coexist without rules.

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

Source Rendered
Ca(OH)_2 (NH_4)_2SO_4
Install the asciichem gem to see this rendered.

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])
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, and a subscript can apply to the bracketed group:

Source Rendered
[Fe(CN)_6] [OH]_2 [Fe(CN)_6]_3
Install the asciichem gem to see this rendered.

These are parsed as Group(bracket: :square, nodes: [...]) with the subscript as the group multiplicity.

Play