\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{mol2chemfig}

% reduce LaTeX's excessive page margins 
\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{}

\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 \texttt{mol2chemfig} 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}.

\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 -w 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
\setcrambond{2.5pt}{0.4pt}{1.0pt}%
\setbondoffset{1pt}%
\setdoublesep{2pt}%
\setatomsep{16pt}%

\begin{lstlisting}
% bond styling
\setcrambond{2.5pt}{0.4pt}{1.0pt}
\setbondoffset{1pt}
\setdoublesep{2pt}
\setatomsep{16pt}
% print atoms with smaller font and in sans-serif
\renewcommand{\printatom}[1]%
{\fontsize{8pt}{10pt}\selectfont{\ensuremath{\mathsf{#1}}}}%
\end{lstlisting}

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

\clearpage

\noindent After these settings have been adjusted, 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.

\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 -l plp plp.mol > plp.tex 
% and
% mol2chemfig -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 - only during development
\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. These atoms and bonds need to be given handles for the arrows to attach to. The listing below shows the edited version; the manual changes are highlighted.

\begin{center}
\lstinputlisting{plp3highlighted} 
\end{center}

\input{plp3} % don't try to compile the highlighted version, it will detonate

\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 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}

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

% draw grid of help lines - only during development
% \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) (plp3) {\chemfig{!{plp3}}};

% horizontal arrow 
\draw[<->] (plp) -- (plp3);

% 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. 
\mcfpush{atom4}{100:4mm}{bond3to4}{70:4mm}
\mcfpush{bond2to3}{10:4mm}{bond1to2}{-30:4mm}
\mcfpush[][2pt]{bond1to15}{0:4mm}{bond15to16}{-20:4mm}

% 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); 

\end{tikzpicture}

\end{center}

\section*{Invoking \texttt{mol2chemfig} from within \LaTeX}

Using the $-shell-escape$ option on Linux or its equivalents on other systems, \LaTeX\ can execute shell commands, capture the output and insert it directly into the document. We can use this with mol2chemfig. If you have \texttt{mol2chemfig} working and \LaTeX\ properly configured, the following command will insert the structure of FMNH directly into your document, without creating a separate file:

\begin{verbatim}
\mcfinput|"mol2chemfig -w fmn.mol"
\end{verbatim}

\noindent Note, however, that with large documents and numerous formulas the overhead of running \texttt{mol2chemfig} on each formula each time will add up.

%\begin{center}
%\mcfinput|"mol2chemfig -w fmn.mol"
%\end{center}


\end{document}