chemfig
chemfig draws molecules from a line of text: atoms, the bonds between them, the angles of those bonds, branches and rings. fxTeX Pro draws each one as SVG, inside the equation, so a structure can sit in a sentence or beside an arrow like any other piece of maths. The editor below has the chemistry panels — chemfig's and mhchem's — with the maths a chemist needs around them. Click any example on this page to open it there.
Adding chemfig to your page
In fxTeX Pro, name chemfig when you render the page:
fxTeXPro.ready.then(() => {
fxTeXPro.unlock('fx_pk_your-key-here');
fxTeXPro.autoRender({ packages: ['chemfig'] });
});
In the equation editor, name it to EqEditor.init() and add its
panels to the toolbar. This is the editor above, with mhchem as well:
<script src="https://fxeditor.codecogs.com/fxeditor.api.min.js"></script>
<div id="toolbar"></div>
<div id="mathview"></div>
<div id="latex"></div>
<script>
const ready = EqEditor.init({
licence: 'fx_pk_your-key-here',
packages: ['mhchem', 'chemfig']
});
const toolbar = EqEditor.Toolbar.link('toolbar', [
['mhchem', 'mhchem_arrows', 'chemfig', 'chemfig_ext', 'arrows'],
['operators_core', 'subsupset', 'brackets', 'greeklower', 'greekupper', 'symbols', 'spaces']
]);
ready.then(() => {
const latex = EqEditor.LaTeXView.link('latex');
const mathview = EqEditor.MathView.link('mathview').addLaTeXView(latex);
toolbar.addLaTeXView(latex).addMathView(mathview);
mathview.setLatex('\\chemfig{H_3C-C(=[1]O)-[7]OH}');
});
</script>
The chemfig panel has rings, bonds, bond angles and branches, and writes inside
\chemfig{} — adding it only when the caret is not in one already, so each
button extends the molecule you are drawing. chemfig_ext has charges, lone pairs,
names, brackets and reaction schemes. chemfig is the largest of the packages, 112 kB
gzipped: name it only on the pages that use it. See Using
packages for more on both steps.
Structure Editor
You don't need to learn chemfig's code to draw a molecule. The Chemistry Structure
Editor draws it with the mouse and writes the \chemfig{…} for you. To
open it, press the first button of the chemfig panel. To change a structure that is already in the
equation, click its bonds or double-click anywhere on it. You can also put the cursor inside its
\chemfig and choose Edit chemistry structure… from the right-click menu. A
page can open it on its own with EqEditor.ChemfigEditor.open(latex), which resolves with
the new LaTeX, or null if the reader cancels.
- Zoom and close. Zoom out and in (or Ctrl+wheel), Fit to show the whole structure, and × to close without saving. Drag the title bar to move the dialog.
- Tools. Select; single, double and triple bonds; wedge and hashed wedge; rings of three, five and six atoms, benzene, and a ring of any size; atom label; join; and erase. Hover over a tool to see its key.
- The drawing. Click an atom with a bond tool to add a bond. It goes in the most open direction, so chains zig-zag by themselves. Drag from an atom to point the bond yourself, or drop it on another atom to join the two and close a ring. Click a bond with a bond tool to change it: the single-bond tool steps it through double and triple. Click an atom with a ring tool to grow a ring from it, or a ring's bond to fuse a second ring there, as in naphthalene. Drag an atom to turn its bond (Shift stretches it), or drag a ring's corner to turn the ring.
- Edit and Library. The side panel's two tabs: the settings below, or the library of common structures.
- Structure. The bond length for the whole structure, a name set beneath it
(
\chemname), and the steps that dragged bonds turn in. Arrange lays the structure out afresh, and the arrows turn all of it by 15°. - Selection. Whatever is selected. An atom has its label, a palette of common ones, + and − charges, and lone pairs placed around it. A bond has its type, angle, length, line style and colour. A ring can have an aromatic circle or alternating double bonds. Further down are the structure as the equation will show it, and its chemfig code, which you can also edit by hand.
- Status. How many atoms, bonds and rings there are, and a hint for the tool in use. Update (or Insert, for a new structure) puts it into the equation; Cancel leaves the equation as it was.
Library. Search over 220 molecules, rings and functional groups by name, formula or abbreviation: caffeine, COOH, Phe. With an atom selected, a click attaches the group to that atom. With nothing selected, it replaces the drawing.
Delete removes what is selected, Ctrl+Z and Ctrl+Y undo and redo, and Ctrl+Enter saves. An atom
left as an empty box ({}) can be typed into later, straight in the equation.
Atoms and bonds
Atoms are letters, with subscripts as in maths; bonds are the symbols between them.
\chemfig{A-B}Single bond.
\chemfig{A=B}Double bond.
\chemfig{A~B}Triple bond.
\chemfig{A>B}Wedge: towards the viewer.
\chemfig{A<B}Wedge the other way.
\chemfig{A>:B}Hashed wedge: away from the viewer.
\chemfig{A>|B}Hollow wedge.
\chemfig{CH_3-CH_2-OH}Ethanol: groups of atoms, with subscripts.
Bond angles
A bond goes to the right unless told otherwise, in square brackets after it: a number
n on its own is n×45°, :30 is an absolute angle in degrees,
and ::30 is relative to the bond before.
\chemfig{A-[1]B}45°.
\chemfig{A-[2]B}90°, straight up.
\chemfig{A-[7]B}315°, down to the right.
\chemfig{A-[:30]B}30°, absolute.
\chemfig{A-[::30]B-[::30]C}30° on from the bond before, each time.
\chemfig{A-[,2]B}The second option is the bond's length: twice as long.
\chemfig{-[:30]-[:-30]-[:30]-[:-30]}A skeletal chain: bonds with no atoms.
Branches
A bond in round brackets after an atom is a branch off it; the chain carries on after the bracket.
\chemfig{H-C(-[2]H)(-[6]H)-H}Methane, with branches up and down.
\chemfig{H_3C-C(=[1]O)-[7]OH}Acetic acid.
\chemfig{H-C(-[2]H)(-[6]H)-C(=[1]O)-[7]H}Acetaldehyde.
Rings
*6(...) is a ring of six, with its bonds in the brackets; **6 draws a
circle inside it. Anything in brackets on a ring's atom is a substituent, and a ring can be one.
\chemfig{*6(-=-=-=)}Benzene, with alternating double bonds.
\chemfig{**6(------)}Benzene, with a circle.
\chemfig{*6(------)}Cyclohexane.
\chemfig{*5(-----)}A five-membered ring.
\chemfig{CH_2*3(-CH_2-CH_2-)}Cyclopropane, with its atoms named.
\chemfig{*6(-=-(-OH)=-=)}Phenol: a substituent on the ring.
\chemfig{*6(=-*6(-=-=-)=-=-)}Naphthalene: a ring fused to a ring.
\chemfig{[:-30]*6(-=-=-=)}The whole molecule turned 30°.
Charges and lone pairs
\charge puts something beside an atom at an angle; \: is a lone
pair. \chemabove and \chembelow put it above or below.
\chemfig{\charge{90=\:,270=\:}{O}}Lone pairs above and below.
\chemfig{\Charge{45=$\scriptstyle\oplus$}{N}}\Charge sets it a little further out.
\chemfig{\chemabove{N}{\oplus}(-[2]H)(-[6]H)-H}A charge above an atom, with branches.
\chemfig{H_3\chemabove{N}{\oplus}-CH_2-COO^{\ominus}}Glycine as a zwitterion.
\setcharge{circle}\chemfig{\charge{90=+}{N}}\setcharge styles every charge after it.
Names and brackets
\chemname{\chemfig{*6(=-=-=-)}}{Benzene}\chemname sets a name under a molecule.
\chemleft[\chemfig{A-B}\chemright]^{\ominus}Brackets sized to the molecule, with a charge.
Reaction schemes
Between \schemestart and \schemestop, \arrow joins
whatever is either side of it: text, or molecules. Its type goes in braces, with labels in
square brackets above and below; \+ is a plus.
\schemestart A \arrow B \arrow C \schemestopA sequence.
\schemestart A \+ B \arrow C \schemestopTwo reactants.
\schemestart A \arrow{<=>} B \schemestopEquilibrium.
\schemestart A \arrow{<->} B \schemestopResonance.
\schemestart A \arrow{-/>} B \schemestopNo reaction.
\schemestart A \arrow{->[a][b]} B \schemestopLabels above and below.
\schemestart \chemfig{*6(-=-=-=)} \arrow{->[\chemfig{Br_2}][\chemfig{FeBr_3}]} \chemfig{*6(-=-=(-Br)-=)} \schemestopThe bromination of benzene.
Settings and shortcuts
Options in square brackets after \chemfig apply to that molecule;
\setchemfig sets them for every one after it. \definesubmol names a
piece of a molecule, to use again with !{name}.
\chemfig[atom sep=2em]{A-B-C}Shorter bonds, for this molecule.
\setchemfig{atom sep=3em}\chemfig{A-B}For every molecule after it.
\chemfig{A-[,,,,red]B}The fifth bond option styles it.
\definesubmol{x}{-[:30]-[:-30]}\chemfig{A!{x}!{x}B}A piece defined once, used twice.
The chemfig manual
This page covers the parts of chemfig used most. chemfig itself is by Christian Tellechea; its manual has every option, and many more molecules:
chemfig manual (PDF) — the LaTeX package's documentation, in English, on CTAN.
chemfig on CTAN — the package, its licence, and the manual in French.
More
mhchem — chemical formulae, equations and units.
Using packages — turning packages on, and their panels.
Supported commands — every chemistry command fxTeX reads.
