3

I have a document with a lot of diagrams created with PGFPlots and want to externalize those. All other tikz snippets should not get externalized. I achieve this by setting \tikzset{external/only named=true} and give the plots a sweet name.

This seems to work with anything but graphics created by the chemfigpackage. Consider following MWE:

%  file: chemfigbug.tex
% arara: pdflatex: { action: nonstopmode}
% arara: make: { task: -B -f chemfigbug.makefile }
% arara: pdflatex: { action: nonstopmode}
%
\documentclass{article}
\usepackage{chemfig}
%
\usetikzlibrary{external}
\tikzexternalize[shell escape=-enable-write18]
\tikzsetexternalprefix{extern/}
\tikzset{external/only named=true}
\tikzset{external/mode=list and make}
%
\begin{document}
%
\tikzsetnextfilename{house}
\begin{tikzpicture}
\draw[thick] (0,0) -- (0,2) -- (1,3.25) -- (2,2) -- (2,0) -- (0,2) -- (2,2) -- (0,0) -- (2,0);
\end{tikzpicture}

\begin{equation}
%
%\begin{tikzpicture}
\definesubmol\nobond{-[,0.2,,,draw=none]}
    \chemfig[][scale=0.4]{[:30]*6(=-=-=(-*6(-(!\nobond\bullet)=-=-=))-)}
    \chemsign{+}\chemfig{C_2H_2}\chemrel{->}
    \chemfig[][scale=0.4]{[:30]*6(=-=-=(-*6(-(-=_[0,]-[:-60])=-=-=))-)}
    \chemsign{+}\chemfig{H}
%\end{tikzpicture}
\end{equation}
\end{document}

Running make on the makefile failes to compile. house.log shows the following error:

Runaway argument?

! File ended while scanning use of \tikzexternal@laTeX@collect@until@end@tikzpicture.
<inserted text> 
                \par 
<*> ...rnalrealjob{chemfigbug}\input{chemfigbug}

A possible solution I found proposed putting the chemfig-code in a tikzpicture environment. Uncommenting those commands yields to flawless compilation, however the alignment of the equation gets totally messed up. Not exteralizing the "house" seems to solve it all, but I really need to externalize some in the real use-case.

Update

Further playing around with the code I managed to get horizontal alignment done by introducing not only a tikzpicture-environment but also putting the chemfig code into a \node. Vertically I have been experimenting with the [baseline] option of tikz and achieved OK-ish results. Some fine tuning is still required in my view.

What I came up with:

\documentclass{article}
\usepackage{chemfig}
\begin{document}
\begin{equation}
\begin{tikzpicture}[baseline=-1.15ex]
    \node at (0,0) {%
        \definesubmol\nobond{-[,0.2,,,draw=none]}
        \chemfig[][scale=0.4]{[:-30]*6(=-=(-*6(-=-=-=))-(!\nobond\bullet)=-)}
        \chemsign{+}\chemfig{C_2H_2}\chemrel{->}
        \chemfig[][scale=0.4]{[:-30]*6(=-=*6(-*6(-=-=-=)--=--)-=-)}
        \chemsign{+}\chemfig{H}
    };
\end{tikzpicture}
\end{equation}
\end{document}

What it looks like:

Centerd but not yet perfectly aligned

The alignment with the "(1)" still has room for improvement.

Possible solutions:

  1. How to make it compile without altering the code for the equation?
  2. How to get the alignment right when putting the equation in a tikzpicture? solved
  3. (Bonus: If someone more adapt with chemfig has a prettier solution for my workaround to create the 3-ring I would be more than thankful :)) solved
2

1 Answer 1

1

As for the alignment question, here is a "tricky" solution:

\definesubmol\nobond{-[,0.2,,,draw=none]}
\begin{equation}
    \mbox{Preceding text:}
  \schemestart[][185]
    \chemfig[][scale=0.4]{[:-30]*6(=-(!\nobond\vphantom{\bullet})=(-*6(-=-=-=))-(!\nobond\bullet)=-)}
    \arrow{0}[,0]\+ \chemfig{C_2H_2}
    \arrow
    \chemfig[][scale=0.4]{[:-30]*6(=-(-[,,,,draw=none])=*6(-*6(-=-=-=)--=--)-=-)}
    \arrow{0}[,0]\+ \chemfig{H}
  \schemestop
\end{equation}

Let me explain some details. The problem with the vertical misalignment of the first molecule is the bullet over the top. We therefore add a "phantom bullet" on the opposite side (!\nobond\vphantom{\bullet}). The same trick we do with the other molecule and adding a "phantom" bond (-[,,,,draw=none]). Then we have both molecules symmetric. To align it vertically with "+" signs we use a "phantom" arrows \arrow{0}[,0]. (we used a lot of phantoms:))

At last, to align it with the equation number we have to "fiddle" with the position of the whole scheme, using the second parameter of \schemestart[][] (185 seemed to me ok).

I am not sure why you need to wrap it in tikzpicture, but the way you used baseline option of the tikzpicture and \node at (0,0) is quite fine.

And always if you are not sure about alignment, use \schemedebug{true} or \printatom as it is explained in the chemfig manual.

1
  • Really nice. The reason I need to put the scheme in a tikzpicture is because without it, the externalized graphics won't compile with "naked" chemfig code present in the main document. That was the primary issue in this question. But I can totally live with that workaround now that the alignment is flawless.
    – Huugo
    Commented Nov 28, 2014 at 13:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .