\documentclass[11pt,letterpaper]{article}

% the mol2chemfig package requires and loads the chemfig package,
% which must be installed on your system. chemfig is available 
% through TeXLive, MikTeX or from CTAN. 
\usepackage{ctfix3}

% reduce LaTeX's very wide page margins to 1 inch all around
\usepackage[margin=1in]{geometry}

% use postscript fonts
\usepackage{courier,times}
\usepackage[scaled=0.92]{helvet}
\usepackage{sansmath}

\usepackage{url,listings,textcomp}
\newcommand{\tb}{\textbackslash}
\newcommand{\rarr}{\ensuremath{\rightarrow}}

\lstMakeShortInline{\$}

\lstset{%
    basicstyle=\ttfamily,
    showstringspaces=false,
    escapechar=\&
}

\title{Sample \LaTeX\ document for \texttt{mol2chemfig}}

\author{}\date{}

% stop gap as per https://tex.stackexchange.com/questions/165929/semiverbatim-with-tikz-in-beamer/165937#165937
\makeatletter
\global\let\tikz@ensure@dollar@catcode=\relax
\makeatother

%\setchemfig{bond style=thick,atom sep=20pt, double bond sep=6pt}

\begin{document}

\maketitle

\noindent This sample document illustrates the use of \texttt{mol2chemfig} in conjunction with the \texttt{chemfig} package. This document should compile as is on any system that has a working installation of the \texttt{chemfig} package. A local installation of the \texttt{mol2chemfig} program is not required to compile it, but it \emph{is} required to run the example commands that were used to generate the code. As an alternative to local installation, you can use the web interface at \url{chimpsky.uwaterloo.ca/mol2chemfig}.

In any case, you will need the \texttt{mol2chemfig} \LaTeX\ package in order to compile the generated \LaTeX\ code. A copy of the package should be in the same directory as this source file. 

\section*{The structure of penicillin G}

The \texttt{chemfig} code contained in the file \texttt{penicilling.tex} was generated from the molfile with the command:

\begin{verbatim}
mol2chemfig -wf penicilling.mol > penicilling.tex
\end{verbatim}

\noindent With \texttt{chemfig}'s default settings, the structure comes out as follows:

\bigskip

\begin{center}
\input{penicilling}
\end{center}

\noindent This is a little bit out of proportion to the surrounding text. We can easily adjust the appearance by tweaking some of the settings provided by \texttt{chemfig}:

% these settings adjust the appearance of formulas rendered
% by chemfig - comment out to see chemfig's defaults
\setchemfig{
  cram rectangle=false,
  cram width=2.5pt,
  cram dash width=0.4pt,
  cram dash sep=1pt,
  atom sep=16pt,
  bond offset=1pt,
  double bond sep=2pt
}

% print atoms with smaller font and in sans-serif
\renewcommand{\printatom}[1]{\fontsize{8pt}{10pt}\selectfont{\ensuremath{\mathsf{#1}}}}%

\begin{lstlisting}
% bond styling
\setchemfig{
  cram rectangle=false,
  cram width=1.5ex,
  cram dash width=1pt,
  cram dash sep=2pt,
  atom sep=3em,
  bond offset=2pt,
  double bond sep=2pt
}
% print atoms with smaller font and in sans-serif
\renewcommand{\printatom}[1]%
{\fontsize{8pt}{10pt}\selectfont{\ensuremath{\mathsf{#1}}}}%
\end{lstlisting}

\noindent With these global settings, the same structure now appears as follows:

\bigskip

\begin{center}
\input{penicilling}
\end{center}

\noindent We will leave these settings in effect for the remainder of this document. See the \texttt{chemfig} documentation for additional available settings.

\section*{Incorporating rendered structures into composite graphics}

The \texttt{mol2chemfig} package loads \texttt{chemfig}, which in turn loads the general purpose graphics package \texttt{TikZ}. Through the latter package, we have access to the \texttt{tikzpicture} environment. Here is a \texttt{tikzpicture} that depicts two resonance structures of alanine bound to pyridoxal phosphate:

% load submol definitions - generated with
% mol2chemfig -f -l plp plp.mol > plp.tex 
% and
% mol2chemfig -f -l plp2 plp2.mol > plp2.tex
\input{plp}
\input{plp2}

\begin{center}

\begin{tikzpicture}%
[>=stealth, help lines/.style={very thin,draw=black!25}, x=1cm, y=1cm]

% draw grid of help lines - useful while editing
\draw[help lines] (0,0) grid (12,4);

% place  both structures
\node[anchor=south west, inner sep=10pt] at (0,0) (plp)  {\chemfig{!{plp}}};
\node[anchor=south west, inner sep=10pt] at (7,0) (plp2) {\chemfig{!{plp2}}};

\draw[<->] (plp) -- (plp2);

\end{tikzpicture}

\end{center}

\noindent Note that the $\input$ macro cannot be used inside a $\node$ in the \texttt{tikzpicture} environment. As a workaround, the two structures were rendered as $\submol$ definitions, which were $\input$ outside the \texttt{tikzpicture} environment and then referenced from within the $\node$ macros.

Of course, to be complete and valid, the scheme above should also include arrows that indicate the electron movements underlying the resonance effect. This is possible in \texttt{chemfig} but will in this case require manual annotation of the generated code. So that we can find our way through the code, we can first render the structure to be annotated with atom numbers and display it:

\begin{verbatim}
mol2chemfig -wn plp2.mol > plpn.tex
\end{verbatim}

\begin{center}
\input{plpn}
\end{center}

This tells us that we need to draw electron movement arrows from nitrogen 4 to the adjacent bond to carbon 3, from bond 2\rarr{}3 to bond 1\rarr{}2, and from bond 1\rarr{}15 to bond 15\rarr{}16. 

The mechanism provided by \texttt{chemfig} for drawing push arrows is based on named handles for bonds and atoms. Since version 1.4, \texttt{mol2chemfig} allows you to automatically generate named handles for each atom and bond, using the $--markers$ or $-g$ option:

\begin{verbatim}
mol2chemfig -f -l plpa -g a plp.mol  > plpa.tex
mol2chemfig -f -l plpb -g b plp2.mol > plpb.tex 
\end{verbatim}

\input{plpa}
\input{plpb}

This option adds a unique identifier to each atom and to each bond. The option value (\texttt{a} and \texttt{b} in our example) is used as a prefix; this allows to unambiguously reference atoms in multiple molecules in the same drawing. In our first example, atom 3 will be given the marker \lstinline!@{a3}! for atom 3. The bond between atoms 3 and 4 will be labeled with \lstinline!@{a3-4}!; in bond markers, the smaller atom number always precedes the larger one. The generated code now gets a wee bit harsh on the eyeballs: 

\lstinputlisting[firstline=1,lastline=6]{plpa.tex}
\lstinline!                ...!

\noindent We can now reference the handles to attach the electron movement arrows. Note that, for this to work, the document has to be processed \emph{twice} by \texttt{pdflatex}, since the commands internally use a PDF overlay mechanism; otherwise, the arrows may be misplaced. 

The $\mcfpush$ macro that is used to place the arrows is defined in the \texttt{mol2chemfig} package and is explained in the source code of this document; it is a convenience wrapper around the $\chemmove$ command provided by \texttt{chemfig}.

\begin{center}

\colorlet{mcfpusharrowcolor}{red} % set color for push arrows

\begin{tikzpicture}%
[>=stealth, help lines/.style={very thin,draw=black!25}, x=1cm, y=1cm]

\draw[help lines] (0,0) grid (12,4);

% place structures
\node[anchor=south west, inner sep=10pt] at (0,0) (plpa)  {\chemfig{!{plpa}}};
\node[anchor=south west, inner sep=10pt] at (7,0) (plpb) {\chemfig{!{plpb}}};

% horizontal arrow 
\draw[<->] (plpa) -- (plpb);

% enclosing brackets
\draw[semithick] (0.25,0) -- ++(-0.25,0) -- ++(0,4) -- ++(0.25,0); 
\draw[semithick] (11.75,0) -- ++(0.25,0) -- ++(0,4) -- ++(-0.25,0); 

% electron movement arrows. The \mcfpush macro is a convenience 
% wrapper around the \chemmove macro provided by chemfig. The 
% syntax of \mcfpush is 
% \mcfpush{handle1}{angle1:distance1}{handle2}{angle2:distance2}
% where angles and distances place the control points of the 
% arrow's bezier curve.
% Two optional arguments specify lengths by which to shorten the
% arrow at the start and end, respectively. These default to 3pt  
% and 2pt, respectively.
\mcfpush{a3-4}{70:4mm}{a4}{100:4mm}
\mcfpush{a1-2}{-30:4mm}{a2-3}{10:4mm}
\mcfpush{a15-16}{-20:4mm}{a1-15}{0:4mm}
\mcfpush{a17}{-20:4mm}{a16-17}{-20:4mm}

\mcfpush{b4}{100:4mm}{b3-4}{70:4mm}
\mcfpush{b2-3}{10:4mm}{b1-2}{-30:4mm}
\mcfpush[4pt][3pt]{b1-15}{0:4mm}{b15-16}{-20:4mm}
\mcfpush{b16-17}{-20:4mm}{b17}{-20:4mm}
\end{tikzpicture}

\end{center}

\end{document}
.
