[<< wikibooks] LaTeX/Chemical Graphics
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 ==
The primary command used in this package is \chemfig{}:

 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 [:]. Relative angles require the syntax [::] 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.

 describes the bond attaching  and . There are 9 different bond types:

\chemfig{C(-[:0]H)(-[:90]H)(-[:180]H)(-[:270]H)}
 represents the factor by which the bond's length will be multiplied. 
 includes additional options regarding the color or style of the bond. 

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

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


== Skeletal Diagrams ==
Skeleton diagrams can be produced as follows:


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


== Lewis Structures ==
Lewis structures use the syntax \charge{=,=...}{atom}, where  is in polar coordinates as :, though the : can be ommited if no additonal shift is required. In the charge field, \. and \: will give unpaired and paired electrons respectively.

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


== Ions ==
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 ==
Resonance structures require a few math commands:


== Chemical Reactions ==
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:

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 ===
Chemical reactions can be created with the following commands: 

In \chemrel{},  and  represent text placed above and below the arrow, respectively.
There are four types of arrows that can be produced with \chemrel{}:


== Naming Chemical Graphics ==
Molecules can be named with the command

 is inserted between the bottom of the molecule and the top of the name defined by . It is 1.5ex by default. 
 will be centered relative to the molecule it describes. 

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:

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

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


== Advanced Graphics ==
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 ==
If you need to typeset chemical formulae, you have the choice between two very good packages: mhchem and chemmacros.


=== Package mhchem ===
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

renders:

  
    
      
        
          3
          
          
            H
            
              2
            
            
              
            
          
          O
        
      
    
    {\displaystyle {\ce {3H2O}}}
  

  
    
      
        
          
            
              1
              2
            
          
          
            H
            
              2
            
            
              
            
          
          O
        
      
    
    {\displaystyle {\ce {1/2H2O}}}
  

  
    
      
        
          
            AgCl
            
              2
            
            
              −
            
          
        
      
    
    {\displaystyle {\ce {AgCl2-}}}
  

  
    
      
        
          
            H
            
              2
              
                (
                aq
                )
              
            
            
              
            
          
        
      
    
    {\displaystyle {\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 ....


=== Package chemformula ===
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

renders:

  
    
      
        
          3
          
          
            H
            
              2
            
            
              
            
          
          O
        
      
    
    {\displaystyle {\ce {3H2O}}}
  

  
    
      
        
          
            
              1
              2
            
          
          
            H
            
              2
            
            
              
            
          
          O
        
      
    
    {\displaystyle {\ce {1/2H2O}}}
  

  
    
      
        
          
            AgCl
            
              2
            
            
              −
            
          
        
      
    
    {\displaystyle {\ce {AgCl2-}}}
  

  
    
      
        
          
            H
            
              2
              
                (
                aq
                )
              
            
            
              
            
          
        
      
    
    {\displaystyle {\ce {H2_{(aq)}}}}
  

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


== XyMTeX package ==
The following code produces the image for corticosterone below.