# Compounds defined implicitly in captions (chemscheme/chemstyle/chemcompounds)

I'm using the chemstyle package to handle automatic numbering of compounds. It apparently uses the chemcompounds package to handle this. In the documentation for the latter, it explains compounds can be 'created' implicitly, just by referring to a label that doesn't yet exist: \compound{label}.

Generally this works great, but when I try to do it inside a caption, it seems to forget the compound straight away. For example, I have two figures side-by-side, I refer to different compounds in each caption, but they get assigned the same compound number. When I create a new compound in the text following, it gets the number assigned to the first compound in the figures... so basically as soon as the caption ends the package forgets all the compounds.

I should mention I'm aware it is possible to work around this (eg. by making sure things are referred to in text before captions), but I'm looking for a foolproof solution. After all I'm using LaTeX for this so I don't have to worry about misnumbered compounds constantly.

-
Welcome to TeX.SE. It is always best to compose a MWE that illustrates the problem including the \documentclass so that those trying to help don't have to recreate it. – Peter Grill Oct 19 '11 at 0:03
I'm not familiar with the chemstyle package, but the problem you describe with the use of the \compound command inside captions sounds like it could be solved by inserting a \protect command before the \compound command. – Mico Oct 19 '11 at 1:15
In moving arguments such like in \caption one should use the variant \compound+ (and maybe define the label before using the “invisible” variant \compound*). – clemens Mar 23 '12 at 9:42

Are you using the caption package as well? If yes, this bug was fixed in version 3.2 so updating your TeX distribution will help.

For example this MWE will work fine when compiled using caption package v3.2, but gives "1" every time when compiled using caption package v3.1m:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption,chemstyle}
\begin{document}
\begin{figure}[ht]
\includegraphics[height=1cm]{foo}
\caption{Compound number \compound{foo}.}
\end{figure}
\begin{figure}[ht]
\includegraphics[height=1cm]{bar}
\caption{Compound number \compound{bar}.}
\end{figure}

This should be three: \compound{foobar}
\end{document}


(Peter, sorry for "stealing" your MWE ;-))

Also take a look at question Chemcompounds package and subfloat comportement ? which contains a patch for the caption package if updating is not an option.

-

Without a full MWE it is difficult to provide much help. I tried the following and it does not seem to exhibit the problem you report:

If this example does not help, perhaps you can modify this so that it reproduces the problem you mention:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{chemstyle}
\begin{document}
\begin{figure}[ht]
\includegraphics[height=1cm]{foo}
\caption{Compound number \compound{foo}.}
\end{figure}
\begin{figure}[ht]
\includegraphics[height=1cm]{bar}
\caption{Compound number \compound{bar}.}
\end{figure}

This should be three: \compound{foobar}
\end{document}

-
When I compile your MWE, I get compound number 1 appearing three times. I'm using TeXShop and pdflatex... No idea if that helps but don't know what else it could be. – JMS Oct 19 '11 at 2:23
Interesting, I also get 1 appearing three times, with chemcompounds.sty 2006/12/01 v1.1.3. What version are you using? – Jake Oct 19 '11 at 2:31
Oh, and if I load chemcompounds directly, without loading chemstyle, the example works. chemstyle.sty 2011/01/05 v2.0k. – Jake Oct 19 '11 at 2:36
I too am using TeXShop with pfdlatex (TeXLive 2011). I updated all the packages yesterday so not sure if that has anything to do with it. I also have chemstyle.sty 2011/01/05 v2.0k – Peter Grill Oct 19 '11 at 3:12
I am using an older version... chemstyle.sty 2008/08/18 v1.3c. How does one force TexShop to use updated packages? When I put new versions in ~/Library/texmf/tex/latex/, latex still seems to load the older version (that comes packaged?) – JMS Oct 19 '11 at 3:33