Skip to content
In development: Molkit Script and the AI chat are not available to accounts yet. These pages describe work in progress and may change.

The language

The rules of Molkit Script: how lines, values, names, and parts are written, how species and charges are drawn, and how a run behaves. For each command and its options, see the Command reference; for each error, see Error codes.

Each line holds one command: the command name, then at most one argument, then options written name: value, in any order. In draw water view: lewis, draw is the command, water is the argument, and view: lewis is an option.

draw water view: lewis caption: title
  • # starts a comment, to the end of the line. (% is not a comment; Molkit says so if you type one.)
  • A line continues onto the next only inside an open [ or {, so a long list may span lines.
  • A file may start with molkit-script 1 (the version). A file without it is read as version 1.
molkit-script 1
# three molecules, one per line of the list
foreach m in [water,
ammonia,
methane] { draw m }
Kind Written Examples
word letters, digits, -, _, +; no spaces water, lewis, p-only, acetic-acid
quoted text "..." "Ammonia", "1,2-dichloroethane"
length a number and a unit 2 bond, 12 pt, 5 mm, 1 cm, 0.5 in, 3 px
number a number alone, where an option takes one columns: 3
point (x, y) in bond lengths, from the page’s top left at: (4, 2.5)
switch on / off copy: off
list [a, b, c] [water, ammonia], views: [lewis, vsepr]
table a list of lists [[caffeine, "Caffeine"], [aspirin, "Aspirin"]]
set set "Name" set "Solvents"
LaTeX $...$, as in LaTeX: equations, formulas, math $\mathrm{SO_4}{}^{2-}$, $K_\mathrm{a} = 1.8 \times 10^{-5}$
mhchem \ce{...}, if you use the mhchem package \ce{SO4^2-}

A length with no unit is in the option’s default unit, which the Command reference lists for each option. bond is a multiple of the document’s bond length, so a figure keeps its proportions under any preset.

Inside quoted text a backslash is an ordinary character (a SMILES such as "C/C=C\C" needs no escaping). Write \" for a quote mark, \{ for a brace, \\ for a backslash, and \n for a new line. Nothing inside "...", \ce{...}, or $...$ is read as script.

Every object has a name, the one word a script uses for it, unique in the document, and a title, what it is called in words (“hydrogen sulfide”, “ethanol, VSEPR view”).

  • A command that makes an object names it: draw water makes water; derive water views: [vsepr] makes water-vsepr. name: gives another: draw water name: solvent.
  • A name is found whatever its case (draw quatN, then derive quatn), and keeps the case it was first written in.
  • A name already in the document is an error, unless the line says if-exists: reuse: then the existing object is kept, and the line’s into:, title:, and caption: still apply to it. With reuse, you can run a script again on a document that already has its objects.
  • title: sets the title, and rename water to: solvent renames an object; a title that came from the old name changes with it.
  • Objects drawn by hand get names the first time a script runs: molecule-1, text-1, arrow-1, and so on. describe lists them all.

A word means, in this order: a variable (from let, foreach, or a recipe) that is set at that line; as the argument of draw, a species; anywhere else, the name of an object. A name that does not exist is an error that lists the nearest names.

Word Means
last the result of the latest command in this run that makes something (the Command reference says what last is for each command)
selection what you had selected when the run started; with one thing selected, its name
all everything (delete all)
all.sets every set (delete all.sets deletes the sets and keeps their members)
draw water view: lewis
derive last views: [vsepr]

Inside quoted text, {m} is the name a variable holds, {m.title} its title, and {m.formula} its formula. A salt’s formula counts every ion it is made of, so {calcium-chloride.formula} is CaCl2 even when the drawing shows one chloride with a 2. To build a name, use quoted text with placeholders where the name goes:

foreach m in [water, ammonia] {
draw m view: lewis into: set "{m.title}"
write "{m.title}: {m.formula}" below: m
}

A part is a path into a named molecule, written with dots:

Part Means
water.O the oxygen; every oxygen if there are several
water.H1, water.H2 atoms numbered per element, in the molecule’s own atom order. One atom of an element has no number (water.O; water.O1 works too). The numbers never change when the molecule is moved or turned
water.atoms, water.bonds every atom, every bond
water.bond(O, H1) the bond between those atoms. With elements only (bromomethane.bond(C, Br)), the one bond between an atom of each; if there are several, an error (E-BOND) asks you to number the atoms
water.O.lone-pair1 a lone pair of that atom, numbered clockwise from 12 o’clock as the molecule was drawn when it got its name; the numbers then stay (a turn, flip, or move never renumbers)

describe water lists every part, with where each lone pair sits at that moment: parts: O (lone-pair1 top-right, lone-pair2 top-left), H1, H2; bonds: bond(O, H1), bond(O, H2). Read it before you name a lone pair. describe water.O describes one part.

A lone pair keeps its number:

  • A pair that appears later (change water.O charge: 0 after +1) takes the next free number, and describe lists it as “(added later)”.
  • A pair that goes away is listed as gone, and its number is not used again.
  • In a molecule whose atoms lie on one line (HO−, HCl), a flip cannot be seen. After one, its pairs above and below the line trade numbers.

Parts work in change, select, describe, arrow, add, and delete (see Adding and deleting atoms and bonds). Every other command needs a whole object, and a part is an error (E-OBJECT):

draw water view: lewis
draw bromomethane view: lewis right-of: water
draw hydroxide view: lewis charge: atom right-of: bromomethane gap: 1.5 bond
change water.O color: red # an atom: color:, element:, charge:, label:, lone-pairs:, radicals:
change water.bond(O, H1) bond-width: 2 # a bond: color:, bond-width:, order:, bond-style:
arrow from: hydroxide.O.lone-pair3 to: bromomethane.C
change hydroxide.O charge: 0 # its lone pairs are counted again
select water.O # left selected after the run
describe water.O

A part the molecule does not have is E-PART, and the error lists the parts it has (“water has O, H1, H2”). A loop name or last can start a part: change m.O color: red.

add and delete change the structure of a molecule by its parts:

draw water view: lewis
add water.O atom: H
delete water.H1
draw cyclohexane view: bondline below: water
delete cyclohexane.bond(C1, C2)
add cyclohexane.bond(C1, C2) order: 2
  • add water.O atom: H bonds a new atom to the atom named, one bond length away in its widest gap. order: 2 or order: 3 makes the new bond double or triple.
  • add cyclohexane.bond(C1, C2) bonds two atoms of the molecule that are not bonded yet, for example to close a ring.
  • delete water.H1 deletes an atom and its bonds, delete cyclohexane.bond(C1, C2) deletes a bond, and delete water.O.lone-pair1 deletes one lone pair; the other pairs stay where they are drawn.
  • A delete that would split the molecule in two, or delete all of it, is an error: delete a bond in a ring, or delete the whole molecule.
  • On a molecule drawn with lone pairs, Molkit counts the lone pairs of the new atom and of the changed atoms again, as change ... charge: does. Carbon atoms keep theirs. To set a different count, write change ... lone-pairs: after the line.
  • A new atom takes the last number of its element, so the other part names stay. When an element gets its second atom, its first atom’s name changes (O becomes O1). A delete renumbers the atoms of that element that come after it: after delete water.H1, H2 becomes H1. The log says when a name changes.

An arrow between parts is a curved mechanism arrow. It is attached at both ends, like one drawn with the Arrow tool, so it follows the molecules when they move. It is drawn in your mechanism arrow style, and Molkit picks its curve.

  • Start at the electrons that move: a lone pair (hydroxide.O.lone-pair3) when a pair makes a new bond, a bond (bromomethane.bond(C, Br)) when a bond’s electrons leave.
  • End where they go: an atom (bromomethane.C, the atom that gets the new bond, or bromomethane.Br, the atom that takes the bond’s pair), or a bond when they make a double bond out of a single one.
  • An atom is never a start, and a lone pair is never an end (the electrons start at the pair).
  • An arrow end must be one atom: bromomethane.H matches three, so use bromomethane.H1.
  • Pick the lone pair that faces the other molecule (describe says top, left, and so on). If that pair has no clean arrow, Molkit uses another pair of the same atom, and the run’s notes say which.
  • bow: left, bow: right, or bow: auto picks the side the arrow bulges to, seen from its start. auto, the default, is Molkit’s choice. When Molkit finds no clean arrow on the side you asked for, it keeps its own and adds a note.
  • bow: bigger makes the bow 15% bigger, and bow: smaller takes back one such step. Steps add up: bow: [right, bigger, bigger]. For an arrow drawn as an S, use s: more or s: less.
  • A curved arrow has no label. Leave out kind:; an arrow between parts is curved without it.
# SN2: hydroxide attacks the carbon from the side away from the bromine
draw bromomethane view: lewis
rotate bromomethane by: 90 deg
draw hydroxide view: lewis charge: atom right-of: bromomethane gap: 1.5 bond
arrow from: hydroxide.O.lone-pair3 to: bromomethane.C
arrow from: bromomethane.bond(C, Br) to: bromomethane.Br

Leave room between the molecules (about 1.5 bonds) and put the attacking atom on the side it attacks from. Arrows between whole objects (arrow from: water to: ammonia) stay straight reaction arrows.

Molkit looks up the argument of draw in this order:

  1. smiles: "..." on the line, when given. It always wins: draw caffeine smiles: "..." draws that SMILES and names it caffeine.
  2. Molkit’s species table (names of common molecules and ions; help species <word> says whether a word is in it).
  3. A formula that exactly one species in the table has: draw H2O draws water, named water. A formula that several species share is an error that asks which one you mean; a formula that no species in the table has is an error (give smiles: or the name).
  4. PubChem, by name, when the table does not have it: common, IUPAC, and trade names (propan-2-ol, isopropanol). The log says what came back and writes the line with that SMILES, so a saved script runs again offline. If the name is not found, or there is no network, the error says to give smiles:.
draw ethanol smiles: "CCO" view: bondline
help species water

A species written as quoted text is named after it, in lower case, with spaces and punctuation made one -: "1,2-dichloroethane" is named 1-2-dichloroethane.

charge: auto is the default for draw, reaction, and salts. It follows the IUPAC 2008 recommendations for drawing chemical structures: a charge spread over the ion goes after a square bracket around it, and a charge that sits on one atom goes on that atom. (Molkit’s notes cite the recommendations by section number, such as GR-5.7.) In order:

  1. One atom: a cation is its letter and charge (Na+); an anion is bracketed where its lone pairs are drawn.
  2. A small inorganic ion (at most one carbon, no C–C bond, at most 10 heavy atoms: sulfate, nitrate, ammonium, hydronium, phosphate, carbonate, methoxide): bracketed as a whole.
  3. Several separate charge centers (a glycine zwitterion, a betaine, a dicarboxylate such as phthalate): always on the atoms.
  4. Delocalized by resonance (an anion or carbocation beside a π bond: carboxylate, enolate, phenoxide, allyl, benzyl; an N+ in a π bond: iminium, pyridinium): bracketed.
  5. Otherwise the charge is localized (an alkoxide on a larger molecule, tetramethylammonium and other quaternary ammonium ions, an amine’s N+ in a drug, the tert-butyl cation): on the atom.

The run’s notes say which rule auto chose and why (acetate: bracket (charge: auto): charge delocalized from the O- over the pi bond beside it). charge: bracket and charge: atom override it. The log writes charge: only when the line gave one.

A species that comes in several pieces (a salt such as potassium hydrogen phthalate, sodium chloride, or calcium chloride, a hydrate, or a smiles: with a .) is drawn as its pieces side by side, centers level, about one bond length apart, the way the IUPAC recommendations draw salts:

  • Order, left to right. Ions of similar size (the larger has at most 1.5 times the other’s heavy atoms) put the positive ion on the left: Na+ then Cl−, Ca2+ then Cl−. Ions of different sizes put the larger on the left: hydrogen phthalate then K+, sulfate then Cu2+. Neutral pieces (a hydrate’s water) come last.
  • Brackets. A polyatomic ion is in square brackets with its charge after the right bracket, in a bondline too. (A charge on a bare carbon vertex stays on that atom.) A one-atom cation is its letter and charge (K+, Ca2+: no dots, no bracket). A one-atom anion drawn with its lone pairs (Lewis) keeps its bracket, as Lewis-dot teaching draws it; with no lone pairs drawn it is its letter and charge. charge: atom puts every charge on its atom instead. With neither written, charge: auto decides (above).
  • Repeats. A piece that occurs several times is drawn once with its count (repeats: count, the default). A bracketed piece gets the count as a subscript at the foot of its right bracket ([:Cl:]− with a 2 for CaCl2). An unbracketed piece gets the count to its left (2 Cl−; a pentahydrate’s water with a 5). repeats: each draws every copy.
  • Each piece is a molecule of its own, named after its species when Molkit’s species table has it (potassium-ion, sodium-ion, calcium-ion, chloride, water; with repeats: each a second one is chloride-2), otherwise <name>-cation or <name>-anion (khp-anion); a neutral piece that the table does not have is named <name>-1, <name>-2.
  • The whole keeps the compound’s name (draw "potassium hydrogen phthalate" makes potassium-hydrogen-phthalate; draw khp makes khp): place, arrange, caption:, delete, and describe treat it as one object. describe lists its pieces with their counts (pieces: calcium-ion, chloride x2) and gives the whole formula (CaCl2).
  • The species table has the common salts of its ions, so a formula finds them offline: NaOH, KOH, LiOH, NaCl, KCl, LiCl, NaBr, KBr, NaI, KI, NaF, CaCl2, MgCl2, NaNO3, KNO3, and NH4Cl. reaction draws a salt term the same way, its ions side by side: reaction $\mathrm{HCl} + \mathrm{NaOH} \longrightarrow \mathrm{NaCl} + \mathrm{H_2O}$. A piece takes a numbered name when the reaction already uses its ion’s name for a term (CaCl2 to Ca2+ and 2 Cl−: the salt’s chloride is chloride-2).
draw "potassium hydrogen phthalate" view: lewis # the anion on the left, K+ on the right
draw cacl2 smiles: "[Ca+2].[Cl-].[Cl-]" below: potassium-hydrogen-phthalate # Ca2+ and [Cl]- with a 2
draw cacl2-all smiles: "[Ca+2].[Cl-].[Cl-]" repeats: each below: cacl2 # both chlorides drawn
draw khp-paired smiles: "C1=CC=C(C(=C1)C(=O)O)C(=O)[O-].[K+]" charge: atom view: lewis below: cacl2-all # K+ beside the O-

This happens with charge: atom, and with auto when it puts the larger ion’s charges on its atoms (as for disodium phthalate’s two separate carboxylates). Each one-atom counter-ion (K+, Na+, Ca2+, Cl−) then stands against the oppositely charged atom of the larger ion:

  • It sits where a single bond from that atom would end: one bond length out, in the free direction with the most room. No bond is drawn. This is the paired placement in the IUPAC recommendations.
  • K+ sits beside KHP’s carboxylate O−. In disodium phthalate each Na+ sits beside its own O−, so the two are drawn apart (sodium-ion, sodium-ion-2).
  • The O−‘s lone pairs never move. If there is no clear room beside the atom, the ion stays side by side, and the run’s notes say so.
  • A salt of one-atom ions only (CaCl2) has nothing to pair with and keeps its row.

A Lewis drawing of one central atom with four terminal atoms (sulfate, phosphate, perchlorate, ammonium, CCl4, BF4−) is drawn square by default, the textbook cross; layout: angled keeps the angled drawing.

Write chemistry in plain LaTeX inside $...$, the way a textbook sets it. Molkit’s examples follow these conventions:

  • A formula is upright, in \mathrm{}, with subscripts: \mathrm{H_2O}, \mathrm{C_8H_{18}}.
  • A charge is a superscript. After a subscript, offset it with an empty group so it does not stack over the subscript: \mathrm{SO_4}{}^{2-}, \mathrm{NH_4}{}^+. After a letter it sits on it: \mathrm{Na^+}, \mathrm{Ca^{2+}}, \mathrm{H_3O^+}.
  • A coefficient is followed by ~: 2~\mathrm{H_2O}. A coefficient of 1 is left out.
  • The arrow: \longrightarrow for a reaction that goes to completion, \rightleftharpoons for an equilibrium, \xrightarrow[under]{over} when a condition sits on it (\xrightarrow{\Delta}), \leftrightarrow between resonance forms only. Never \rightarrow.
  • A state symbol (s), (l), (g), (aq) is upright and tight against its formula: \mathrm{H_2O(l)}.

State symbols belong to a written equation only. reaction draws no state symbol beside a structure, because a structure does not say that a molecule in that phase has that shape. An equation’s states appear only where the equation itself is written with write, including an equation that layout: under-equation sets structures under.

reaction $2~\mathrm{H_2(g)} + \mathrm{O_2(g)} \longrightarrow 2~\mathrm{H_2O(l)}$
write $\mathrm{CH_3COOH(aq)} + \mathrm{H_2O(l)} \rightleftharpoons \mathrm{H_3O^+(aq)} + \mathrm{CH_3COO^-(aq)}$ name: eq
reaction from: eq layout: under-equation

reaction reads the species, coefficients, and states from the equation. Each species may have its own \mathrm{}, or one \mathrm{} may hold the whole equation. A \species{water}{\mathrm{H_2O}} wrap names the species to draw. If you use the mhchem package, you can write \ce{...} instead: reaction \ce{2 H2 + O2 -> 2 H2O}.

LaTeX is set in STIX 2. font: "Computer Modern" on write uses the classic LaTeX font instead.

let series = [water, ammonia, methane]
foreach m in series { draw m view: lewis }

foreach goes over a list, or over the rows of a table, naming each column. It can also be written as two words, for each:

foreach [n, s, t] in [[caffeine, "CN1C=NC2=C1C(=O)N(C(=O)N2C)C", "Caffeine"],
[aspirin, "CC(=O)OC1=CC=CC=C1C(=O)O", "Aspirin"]] {
draw n smiles: s title: t caption: title
}

A loop variable, and each let inside the loop, lasts for one item only. A variable cannot share a name with an object.

Normally one failing line undoes the whole run. On a foreach, on-error: skip undoes only the failing item’s own work, says why it was skipped, and goes on:

foreach m in [ethanol, nitrate] on-error: skip {
draw m view: bondline name: "{m}-x"
derive "{m}-x" views: [condensed]
}

A recipe is a reusable block with parameters (a parameter can have a default). do runs it:

recipe teaching-set(m, view: lewis) {
draw m view: view into: set "{m}" if-exists: reuse
derive m views: all into: set "{m}"
}
do teaching-set water
do teaching-set ethanol view: bondline

The second do draws ethanol as a bondline, then its other views (Lewis in two layouts, VSEPR, condensed), all in the set “ethanol”.

A recipe lasts for the console session, so a later run can do it. It sees its parameters and the variables set where it was defined, not the variables where it runs.

defaults sets an option for every later line that takes it, until the run ends:

defaults view: lewis
draw water
draw ammonia

A default that does not fit a line is left out of that line, with a note. The same option written on the line itself would be an error.

Where a new object goes, by the first rule that applies:

  1. Its own placement: at: (x, y), or below:, above:, left-of:, or right-of: another object, with gap: (default 1.5 bond) and align: (left, center, or right with above: and below:; top, middle, or bottom with left-of: and right-of:). beside: another object takes the first side with nothing in the way, trying right, below, left, and then above; when every side is crowded, it takes the side that ends nearest the object. beside: takes no align:.
  2. into: set "X": to the right of the set’s last member.
  3. Made from a molecule (derive, resonance, orbital-picture): in a row to the right of the molecule.
  4. The page flow: the run’s first object at the top left of the free space, each next one to the right at the standard gap, wrapping at the page’s margin.

A placement target can be a list, or all for the box around everything:

draw water view: lewis
draw ammonia view: lewis right-of: water gap: 2 bond align: middle
write "Figure 1" size: 14 pt style: bold above: all align: left

A target for below:, above:, left-of:, or right-of: can also be one atom, such as propane.C2. The new object is lined up on that atom and kept clear of the rest of its molecule. place takes an atom target too, so a label written under an atom can be put back under it after the drawing moves. An object cannot be placed by one of its own atoms.

draw propane smiles: "CCC" view: lewis name: pr
write "tetrahedral" below: pr.C2 name: g2
place pr at: (20, 20)
place g2 below: pr.C2

Placement never depends on the window size or the zoom. Boxes include captions and brackets, so nothing is placed onto a label. After a run, Molkit reports anything the run made or moved that overlaps something, or runs past the right margin.

Sets are placed, moved, and arranged as one block. Captions (caption: on a command that makes an object, or later with change) take title, formula, name, none, or your own "text"; a caption is written under the object and moves with it.

  • A run is one undo step. If any line fails, the whole run is undone, and the error names the line and column.
  • Check reads the whole script and reports every problem at once, changing nothing.
  • Step runs one line at a time, each its own undo step; a failing line undoes only itself.
  • Stop ends a run between two lines and undoes it. A PubChem lookup in progress stops too.
  • Limits: recipes can call each other up to 20 levels deep, and a run carries out at most 100,000 lines (each pass of a loop counts). Past either limit, the run stops and is undone (E-LIMIT); on-error: skip does not skip these.
  • on-error: skip also does not skip a Stop (E-STOP), an internal Molkit error (E-INTERNAL), or a stepping run whose tab changed (E-MODE): each one ends the run.
  • A run does not start in animation mode or inside an entered group (E-MODE).
  • Script files end in .mkscript (plain text).
Not in the language Use instead
if, else, while foreach over a list
arithmetic (+, -, * outside \ce{} and $...$) write the number
true / false on / off
view: on derive views:, a list
on: as a target of:
move ... by place
a caption option other than caption: caption:
a name built outside quotes (m-vsepr is only that word) "{m}-vsepr"
resonance of one group of a larger molecule resonance of the whole molecule