vibesketch.render.chemfig

chemfig source for LaTeX: structures as a walk with angles, schemes with arrows.

chemfig (Christian Tellechea, CTAN) draws a molecule from a walk over its atoms: every bond carries an absolute angle and a length coefficient, branches are parenthesised, and a bond that closes a ring is a hook, ?[a] at the first atom and ?[a,{=}] at the second. This module reproduces the drawing rather than idealising it: a spanning tree by depth-first search from the leftmost terminal atom, the bond angles and relative lengths read off the 2D coordinates, and the remaining bonds as hooks. Fused and bridged rings therefore come out exactly as drawn, which is why chemfig’s ring macros (*6(...)) are not used.

Atom labels follow chemfig’s rule that a new atom starts at every capital letter: carbon is implicit, a heteroatom carries its hydrogens on the side away from its bonds (HO- and -OH) and its charge as a superscript, and every bond names the atom it leaves and arrives at (-[:30,,2,1]) so a label such as H_2N attaches at the nitrogen. A heteroatom with bonds on both sides draws its hydrogens as atoms of their own in the free direction. Stereo bonds are chemfig’s Cram bonds, > and >: widening towards the atom in front, with the direction taken from the walk. Aromatic rings are written in a Kekulé form.

Reactions become \schemestart ... \schemestop with \+ between species, reagents above and conditions and the yield below the arrow, and compound numbers under their structures with \chemname. The arrow kinds map to ->, <=> and <->; a retrosynthetic arrow is drawn with TikZ’s double -Implies tip, which chemfig loads. The result is a fragment for \input in a document that loads the chemfig package.

A drawing with shapes is not a scheme but a picture: chemfig_drawing() writes it as one tikzpicture with every object where it is drawn.

Functions

chemfig_drawing(canvas[, nanostructures])

The whole drawing as one tikzpicture, everything where it is drawn.

chemfig_molecule(mol)

\chemfig{...} for an RDKit molecule with 2D coordinates.

chemfig_source(document)

The document's structures and reactions as a chemfig fragment.

Exceptions

ChemfigRefused

The structure or scheme cannot be expressed in chemfig.

exception vibesketch.render.chemfig.ChemfigRefused[source]

Bases: ValueError

The structure or scheme cannot be expressed in chemfig.

vibesketch.render.chemfig.chemfig_drawing(canvas, nanostructures=None)[source]

The whole drawing as one tikzpicture, everything where it is drawn.

The figure the SVG export prints, in TeX: every molecule is a \chemfig walk placed so that the atom it starts at sits at that atom’s drawing coordinates, and rectangles, ellipses, lines, arrows, plus signs and text are TikZ paths and nodes in the same coordinates, one median bond of the drawing to chemfig’s default atom sep of 3 em.

chemfig puts a walk’s first atom at the origin of its own picture, but the box TeX places is the whole molecule’s. The walk’s picture therefore resets its bounding box to its origin, so the box is a point that can be placed exactly, and an invisible bond of a hundredth of a bond leads into the first atom so that it is the element of a label such as HO, not the label’s baseline, that lands on the point. The molecules’ extent is added back as an invisible path. Contracted abbreviations are written as their labels and a polymer repeat unit in brackets, as the SVG figure draws them. Objects the drawing form cannot express are refused, as the SVG renderer refuses them.

Parameters:
Return type:

str

vibesketch.render.chemfig.chemfig_molecule(mol)[source]

\chemfig{...} for an RDKit molecule with 2D coordinates.

Hydrogens are implicit unless the molecule keeps them as atoms. Raises ChemfigRefused for a bond chemfig cannot draw or a structure with no coordinates.

Parameters:

mol (Any)

Return type:

str

vibesketch.render.chemfig.chemfig_source(document)[source]

The document’s structures and reactions as a chemfig fragment.

Every reaction is a scheme; species in no reaction follow as structures of their own, with contracted abbreviations as their labels. A drawing with shapes, or with a polymer repeat unit or other drawing that is no species, is written as a TikZ picture of the whole drawing instead (chemfig_drawing()), which refuses what it cannot draw. Raises ChemfigRefused when there is nothing to draw or a species a reaction needs has no structure.

Parameters:

document (Document)

Return type:

str