LaTeX/Chemical Graphics

From Wikibooks, open books for an open world
Jump to navigation Jump to search
LaTeX logo.svg


Getting Started
  1. Introduction
  2. Installation
  3. Installing Extra Packages
  4. Basics
  5. How to get help

Common Elements

  1. Document Structure
  2. Text Formatting
  3. Paragraph Formatting
  4. Colors
  5. Fonts
  6. List Structures
  7. Special Characters
  8. Internationalization
  9. Rotations
  10. Tables
  11. Title creation
  12. Page Layout
  13. Customizing Page Headers and Footers‎
  14. Importing Graphics
  15. Floats, Figures and Captions
  16. Footnotes and Margin Notes
  17. Hyperlinks
  18. Labels and Cross-referencing
  19. Initials


  1. Errors and Warnings
  2. Lengths
  3. Counters
  4. Boxes
  5. Rules and Struts

Technical Text

  1. Mathematics
  2. Advanced Mathematics
  3. Theorems
  4. Chemical Graphics
  5. Algorithms
  6. Source Code Listings
  7. Linguistics

Special Pages

  1. Indexing
  2. Glossary
  3. Bibliography Management
  4. More Bibliographies

Special Documents

  1. Scientific Reports (Bachelor Report, Master Thesis, Dissertation)
  2. Letters
  3. Presentations
  4. Teacher's Corner
  5. Curriculum Vitae
  6. Academic Journals (MLA, APA, etc.)

Creating Graphics

  1. Introducing Procedural Graphics
  2. MetaPost
  3. Picture
  4. PGF/TikZ
  5. PSTricks
  6. Xy-pic
  7. Creating 3D graphics


  1. Macros
  2. Plain TeX
  3. Creating Packages
  4. Creating Package Documentation
  5. Themes


  1. Modular Documents
  2. Collaborative Writing of LaTeX Documents
  3. Export To Other Formats

Help and Recommendations

  1. FAQ
  2. Tips and Tricks


  1. Authors
  2. Links
  3. Package Reference
  4. Sample LaTeX documents
  5. Index
  6. Command Glossary

edit this boxedit the TOC

chemfig is a package used to draw 2D chemical structures. It is an alternative to ochem. Whereas ochem requires Perl to draw chemical structures, chemfig uses the tikz package to produce its graphics. chemfig is used by adding the following to the preamble:


Basic Usage[edit | edit source]

The primary command used in this package is \chemfig{}:

\chemfig{<atom1><bond type>[<angle>,<coeff>,<tikz code>]<atom2>}

<angle> is the bond angle between two atoms (or nodes). There are three types of angles: absolute, relative, and predefined. Absolute angles give a precise angle (generally, 0 to 360, though they can also be negative), and are represented with the syntax [:<absolute angle>]. Relative angles require the syntax [::<relative angle>] and produce an angle relative to the angle of the preceding bond. Finally, predefined angles are whole numbers from 0 to 7 indicating intervals of 45 degrees. These are produced with the syntax [< predefined angle>]. The predefined angles and their corresponding absolute angles are represented in the diagram below.


Chemfig angles.png

<bond type> describes the bond attaching <atom1> and <atom2>. There are 9 different bond types:


Chemfig bonds.png


<coeff> represents the factor by which the bond's length will be multiplied.

<tikz code> includes additional options regarding the color or style of the bond.

A methane molecule, for instance, can be produced with the following code:


Methane chemfig.png

Linear molecules (such as methane) are a weak example of this, but molecules are formed in chemfig by nesting.

Skeletal Diagrams[edit | edit source]

Skeleton diagrams can be produced as follows:





Rings[edit | edit source]

Rings follow the syntax <atom>*<n>(code), where "n" indicates the number of sides in the ring and "code" represents the specific content of each ring (bonds and atoms).


Ring chemfig.png


Ring2 chemfig.png


Ring3 chemfig.png


Ring4 chemfig.png

Lewis Structures[edit | edit source]

Lewis structures use the syntax \charge{<position1>=<charge1>,<position2>=<charge2>...}{atom}, where <position> is in polar coordinates as <angle>:<shift>, though the :<shift> can be omitted if no additional shift is required. In the charge field, \. and \: will give unpaired and paired electrons respectively. Paired electrons can also be represented by a bar, using \|.


Carbon Lewis Structure PNG.png

Lewis structures can also be included within \chemfig{}.


H2O Lewis Structure PNG.png

Ions[edit | edit source]

For example, consider an acetate ion:



Because the chemfig commands enters the math mode, ion charges can be added as superscripts (one caveat: a negative ion requires that the minus sign be enclosed in brackets, as in the example).

The charge of an ion can be circled by using \oplus and \ominus:



Alternatively, charges can be placed above ions using \chemabove{}{}:



Resonance Structures and Formal Charges[edit | edit source]

Resonance structures require a few math commands:

% see "Advanced Mathematics" for use of \left and \right
% add to preamble:
%	\usepackage{mathtools}	% \Longleftrightarrow

Chemical Reactions[edit | edit source]

Commands \chemrel and \chemsign were removed from chemfig package in latest versions, so in order to draw chemical reactions, one must instead use respectively \arrow and \+ commands in a block surrounded with \schemestart and \schemestop.

There are a few types of arrows that can be drawn with the \arrow command:

\schemestart A\arrow{->}B\schemestop\par % by default
\schemestart A\arrow{-/>}B \schemestop\par
\schemestart A\arrow{<-}B \schemestop\par
\schemestart A\arrow{<->}B \schemestop\par
\schemestart A\arrow{<=>}B \schemestop\par
\schemestart A\arrow{<->>}B \schemestop\par
\schemestart A\arrow{<<->}B \schemestop\par
\schemestart A\arrow{0}B \schemestop\par
\schemestart A\arrow{-U>}B \schemestop\par
A\arrow[,,->] B\arrow[,,-{Triangle[slant=0.5,blue,width=10pt]}]
C\arrow[,,-{CF[sharp]}] D \+ E

Examples de l'instruction \arrow et \+ du paquet chemfig en \LaTeX

For more details on the \arrow command and chemical reactions in chemfig in general, consult the Part IV "Reaction schemes" of the chemfig documentation file.

Older versions[edit | edit source]

Chemical reactions can be created with the following commands:

\chemrel[<arg1>][<arg2>]{<arrow code>}
\chemsign+	% produces a +

In \chemrel{}, <arg1> and <arg2> represent text placed above and below the arrow, respectively.

There are four types of arrows that can be produced with \chemrel{}:


Naming Chemical Graphics[edit | edit source]

Molecules can be named with the command

\chemname[<dim>]{\chemfig{<code of the molecule>}}{<name>}

<dim> is inserted between the bottom of the molecule and the top of the name defined by <name>. It is 1.5ex by default.

<name> will be centered relative to the molecule it describes.

\chemname{\chemfig{R-C(-[:-30]OH)=[:30]O}}{Carboxylic acid} 

In the reaction above, \chemname{} inserts 1.5ex plus the depth of the carboxylic acid molecule in between each molecule and their respective names. This is because the graphic for the first molecule in the reaction (carboxylic acid) extends deeper than the rest of the molecules. A different result is produced by putting the alcohol first:

\chemname{\chemfig{R-C(-[:-30]OH)=[:30]O}}{Carboxylic acid} 

This is fixed by adding \chemnameinit{<deepest molecule>} before the first instance of \chemname{} in a reaction and by adding \chemnameinit{} after the reaction:

\chemname{\chemfig{R-C(-[:-30]OH)=[:30]O}}{Carboxylic acid} 

Lastly, adding \\ in <name> will produce a line-break, allowing the name to span multiple lines.

Advanced Graphics[edit | edit source]

For advanced commands and examples, refer to the chemfig manual, where a more thorough and complete introduction to the package can be found.

Chemical Formulae[edit | edit source]

If you need to typeset chemical formulae, you have the choice between two very good packages: mhchem and chemmacros.

Package mhchem[edit | edit source]

mhchem is a package used to typeset chemical formulae and equations. As well as typeset basic 2D chemical structures. To use this package, add the following to your preamble:


Chemical species are included using the \ce command. For example

\ce{3H2O} \\
\ce{1/2H2O} \\
\ce{AgCl2-} \\
\ce{H2_{(aq)}} \\


For more examples, see meta:Help:Displaying a formula#Chemistry 2.

A few things here are automatically typeset; The 2 in \ce{H2O} is automatically subscripted without requiring additional commands. The amount of the species precedes the formula. 1/2 and other fractional amounts are automatically typeset as in \ce{1/2H2O}. The charge in \ce{AgCl2-} is automatically superscripted. If the charge is neither +1 nor -1, a ^ will superscript it, as in \ce{AgCl2-}. The phase is not automatically subscripted and needs to be enclosed in parenthesis preceded with a _ as in \ce{H2_{(aq)}.

Since February 2016, the mhchem package is also available in TeX in MediaWiki sites like Wikipedia, using the tag <ce>...</ce>.

Package chemformula[edit | edit source]

chemformula is a package from a much bigger chemistry bundle, used to typeset chemical formulae, equations, and basic 2D chemical structures. The package uses a slightly different syntax compared to mhchem. Package chemmacros will be of great interest to people doing chemistry related work.

To use this package, add the following to your preamble:


Chemical species are included using the \ch command. For example

\ch{3 H2O} \\
\ch{1/2 H2O} \\
\ch{AgCl2-} \\
\ch{H2_{(aq)}} \\


As you can see, the syntax is almost the same.

XyMTeX package[edit | edit source]

The following code produces the image for corticosterone below.

Corticosterone as rendered by XyMTeX

Previous: Theorems Index Next: Algorithms