Skip to content

Getting started

AsciiChem is a language for chemistry. This page walks through using the Ruby reference implementation (asciichem-ruby). Other implementations (TypeScript, Python) may follow; the syntax and model are the same across all of them.

  1. Install the gem

    Terminal window
    gem install asciichem

    Or add to your Gemfile:

    gem "asciichem"
  2. Require and parse

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

    Terminal window
    $ asciichem convert -i "2H_2 + O_2 -> 2H_2O" -t mathml
    $ asciichem version
    0.2.0

AsciiChem parses text into a model tree. Each node has typed fields — chemistry semantics, not just typography.

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)
  • Why AsciiChem? — the gaps in AsciiMath it closes.
  • Atoms — elements, isotopes, charges, oxidation states.
  • Molecules — stoichiometry, groups, brackets.
  • Reactions — forward, equilibrium, conditions.