\subsection{Using generated code in composite figures}

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:

\input{chapters/tpic1}

The code for this graphic is 

\lstinputlisting{chapters/tpic1}

The two structures were placed on the canvas using \tkz nodes. The \tkz nodes cannot contain \lstinline!\input! macros. Therefore, as a workaround, we first render the structures as submol definitions with 

\lstinputlisting{plp.sh}

and then reference those definitions from within the \lstinline!\chemfig! macros inside the nodes.

Reactions such as the mesomeric transitions within pyridoxal phosphate should also show some electron pushing. The \chf package offers a mechanism for naming atoms and bonds in molecules; these names can then be used to attach push arrows. We can ask \mcf to generate atom and bond names for us, using the \lstinline!--markers! or \lstinline!-g! option:

\lstinputlisting{mp.sh}

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

\lstinputlisting[firstline=1,lastline=5]{mp.tex}

\ldots

In order to attach drawing elements to the nodes defined in the \mcf-rendered structures, we need to use a separate \lstinline!tikzpicture! environment, with the optional arguments \lstinline![remember picture, overlay]!. Inside this environment, we can use arbitrary \tkz commands to decorate our rendered structures. Here is an example:

\lstinputlisting{chapters/tpic2.tex}

which gives

\input{chapters/tpic2}

One important thing to note is that, when the overlay mechanism is used, the document has to be processed \emph{twice} by pdftex---otherwise, the overlaid elements tend to get misaligned. 

The \lstinline!\draw! commands used in the example for the electron push arrows employed the  \lstinline!mcfpusharrow! \tkz style that is defined by the \mcf package. You can adjust this style to your own tastes with the \lstinline!\tikzset! macro; see the \tkz documentation for details.

If the push-arrows are the \emph{only} drawing element you need, you can avoid the need for explicitly creating an overlaid second \lstinline!tikzpicture! environment by using macros predefined by either \chf and \mcf. The next example illustrates the use of the \lstinline!\mcfpush! macro defined by \mcf. 

\lstinputlisting{chapters/tpic3.tex}

which gives

\input{chapters/tpic3}

The arguments of the \lstinline!\mcfpush! macro are, in order, the named anchor of the origin, the parameters of the first control point (departure angle:distance), and the second anchor and second control point (arrival angle:distance). Two optional arguments set the \lstinline!shorten <=! and \lstinline!shorten >=! lengths for the arrow in question, which override the global values that can be adjusted using the \lstinline!\tikzset! mechanism. 

The \lstinline!\mcfpush! macro uses the \lstinline!mcfpusharrow! \tkz style (see above). This style references two color definitions, which were redefined using \lstinline!colorlet! (supplied by package \lstinline!xcolor!) in this example. The background color (\lstinline!mcfbgcolor!) defaults to white; you will only want to change it when drawing on a non-white canvas. This color definition is also used when drawing crossing bonds in the foreground (see section \ref{sec:tweaking}). 

Note that, even though the the overlaid \lstinline!tikzpicture! environment is no longer explicitly declared, the overlay mechanism is still used internally, so the need for processing the file with \TeX\ twice remains. 


.
