Skip to content

Getting started

AsciiChem is a language for chemistry. It has three implementations — Ruby, TypeScript, and Python — that pass the same shared corpus byte-for-byte. Pick the one that ships in your stack; the syntax and the model are identical everywhere.

  1. Install the gem

    Terminal window
    gem install asciichem
  2. Parse and render

    require "asciichem"
    formula = AsciiChem.parse("H_2O")
    formula.to_mathml # => "<math>...</math>"
    formula.to_text # => "H_2O" (round trip)
    formula.to_structural_svg # 2D diagram
  3. Use the CLI

    Terminal window
    asciichem convert -i "2H_2 + O_2 -> 2H_2O" -t mathml
    asciichem lint -i "H_2 + O_2 -> H_2O"
    asciichem resolve --cas 50-78-2

Full reference: Ruby API.

Ruby
asciichem
gem install asciichemReference implementation.
TypeScript
asciichem-ts
npm install asciichemPowers this page.
Python
asciichem-py
pip install asciichem-pyZero runtime deps.

AsciiChem parses text into a model tree. Each node has typed fields — chemistry semantics, not just typography. The tree is the same whether you parsed in Ruby, TypeScript, or Python.

SourceModel
H_2OMolecule[Atom(H, sub:"2"), Atom(O)]
^14CMolecule[Atom(C, isotope:"14")]
Ca^2+Molecule[Atom(Ca, charge:"2+")]
Ca(OH)_2Molecule[Atom(Ca), Group(...,mult:"2")]
2H_2+O_2 -> 2H_2OReaction(reactants:[...], products:[...], arrow::forward)
  • Implementations — the three packages, side by side.
  • Atoms — elements, isotopes, charges, oxidation states.
  • Molecules — stoichiometry, groups, brackets.
  • Reactions — forward, equilibrium, conditions.
Play