Molecules
A molecule is an ordered sequence of atoms and groups, with an optional leading stoichiometric coefficient, optional stereo prefix, and optional CML annotations.
Simple molecules
Section titled “Simple molecules”H_2O
CO_2
C_6H_12O_6asciichem gem to see this rendered.
Each atom carries its own subscript. The model is Molecule(nodes: [Atom, Atom, ...]).
CML annotations
Section titled “CML annotations”Molecules can carry CML metadata via the @key("value") suffix:
H_2O @name("Water")@inchi("InChI=1/H2O/h1H2")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.
Stoichiometric coefficients
Section titled “Stoichiometric coefficients”Leading digits on a molecule are its coefficient:
2H_2O
3H_2asciichem gem to see this rendered.
The Molecule#coefficient field holds the string. The MathML renderer
emits it as an <mn> before the atoms.
State marks
Section titled “State marks”A whitespace-separated lowercase v or ^ after a molecule marks its
phase state — mhchem’s precipitate and gas glyphs:
AgCl v
AgCl ^
Ag^+ + Cl^- -> AgCl vasciichem 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.
Phases
Section titled “Phases”A tight parenthesised phase after a molecule — mhchem’s spellings, grammar v0.11.0:
NaCl(aq)
H_2O(l)
Fe(s)
CO_2(g)
AgCl(s) vasciichem 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.
Adducts — the middle dot
Section titled “Adducts — the middle dot”The mhchem middle-dot connective joins molecules at the formula
level — CuSO4 . 5H2O hydrates:
CuSO4 . 5H2O
NaCl . KCl . CaCl2
CuSO4 · 5H2Oasciichem 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 ·, 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.
Groups
Section titled “Groups”Parenthesised sub-formulas are groups. A group can carry an outer multiplicity.
Ca(OH)_2
(NH_4)_2SO_4asciichem gem to see this rendered.
For Ca(OH)_2:
- Atom
Ca - Group
OHwith multiplicity2, bracket:paren
For (NH_4)_2SO_4:
- Group
NH_4with multiplicity2 - Atoms
S,Owith subscripts4
Bracket variants
Section titled “Bracket variants”AsciiChem supports three bracket kinds:
| Bracket | Open / Close | Use |
|---|---|---|
| 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.
Coordination complexes
Section titled “Coordination complexes”Square brackets wrap coordination complexes, and a subscript can apply to the bracketed group:
[Fe(CN)_6]
[OH]_2
[Fe(CN)_6]_3asciichem gem to see this rendered.
These are parsed as Group(bracket: :square, nodes: [...]) with the
subscript as the group multiplicity.