# externalize pgfplots, and only pgfplots

I would like to externalize the plots in my document (using pgplots), but not other inline stuff, such as bonds automatically drawn by chemmacros. That is, in the following test document:

\documentclass{article}
\usepackage{chemmacros,pgfplots}

\usepgfplotslibrary{external}
\tikzexternalize

\begin{document}
\ch{A-B}
\begin{tikzpicture}
\begin{axis}[
xlabel={\ch{A-B}},
xmin = 0, xmax = 1,
ymin = 0, ymax = 1,
]
%  \addplot {x*x};
\end{axis}
\end{tikzpicture}
\end{document}


I'd like to externalize the tikzpicture environment, but not the \ch command (neither do I want the \ch command inside the environment to be recursively externalized, if that can happen).

I could use \tikzexternaldisable and \tikzexternalenable to switch off and on externalization when needed, but is there a better more automatic way to, for instance, prevent externalization of \ch?

PS. There is some unwanted interaction between pgplots and chemmacros. The above example gives wrong results if the \addplot is uncommented, or if the x and y limits are commented out. It does not happen if \ch is not used inside the plot. This, however, is not immediately related to the question, and might be worth a new one.

• Does the \ch{A-B} macro work properly for you in the axis label? For me, the A and B are positioned too far apart, and the connecting line is diagonal instead of horizontal (regardless of whether the external library is used or not). – Jake Jun 10 '13 at 17:19
• @jake Hmm... you're right, it doesn't work here. But it works fine in my working document, so there must be some other option affecting it. I'll investigate tomorrow and modify the example accordingly. Anyway, it should not matter for the main question. – Jellby Jun 10 '13 at 18:16
• Hmm, the plot is correct if the formula does not contain a bond (which is a tikzpicture itself)... – clemens Jun 11 '13 at 8:55
• Workaround: put the formula in a savbox and use the savebox for the xlabel – clemens Jun 11 '13 at 9:03
• Not a 'complete' answer, but would something like \RenewDocumentCommand { \ch } { O{} m } { \tikzexternaldisable \chemformula_ch:nn {#1} {#2} \tikzexternalenable } \ExplSyntaxOff help at all? – Joseph Wright Aug 18 '13 at 7:53

## 1 Answer

Thanks to @JosephWright and @percusse version 4.1 of chemformula (the package that provides \ch and is loaded by chemmacros) can be used in the axis environment and also in combination with externalization.

The following needs version 4.1 of chemformula (which already made it into TeX Live 2013). It provides the new option chemformula/tikz-external-disable=true|false (with true as default setting) that disables the externalization of the bonds and arrows (which are drawn with TikZ) by locally adding \tikzexternaldisable at the appropriate places.

However, if \ch is used inside a tikzpicture that is to be externalized this feature temporarily needs to be switched off. The following code

% arara: pdflatex: { shell: on }
\documentclass{article}

\usepackage{chemformula}[2013/08/24]
\usepackage{pgfplots}

\usepgfplotslibrary{external}
\tikzexternalize

\begin{document}
\ch{A-B}
\begin{tikzpicture}
\setchemformula{tikz-external-disable=false}
\begin{axis}[
xlabel={\ch{A-B -> A+ + B-}},
]
\addplot {x*x};
\end{axis}
\end{tikzpicture}

\end{document}


creates only one external file for the tikzpicture but none for the bond in \ch{A-B} called outside of the tikzpicture. The result also looks as it should: