Problems with numbering compounds using bpchem

I have a problem numbering my compounds using bpchem in chemstyle. In the eps files, I use TMP and TMP2 etc, when I insert the figure, I do it like this:

\begin{scheme}
\begin{center}
\schemeref[TMP]{1}
\schemeref[TMP2]{2}
\includegraphics[scale=0.6]{figure1}
\end{center}
\end{scheme}


When I first mention a compound, I use \CNlabel, later \CNref. The substitution of the TMP works just fine, my problem is that the labels in the text seem to be independent of the labels in the schemes. In the schemes, the numbers will run smoothly like 1, 2, 3... However, if I try to reference these compounds in the text, they get numbered by the order I mention them in the text. As the order is not the same the number assigned to a particular compound is not the same in the text as in the schemes. How can I fix this?

ps: I am sorry that I did not provide a minimal example to demonstrate this, I just don't know how to reproduce my problem in a short easy way without having to send you all my eps files.

Edit: Here is an example that gives a different numbering in the text relative to the pictures. In the pictures, the numbering is 1,2,3,4 and in the text the numbering is 1,2,3,4 as well, even though it should be 2,3,4,1. Before, I only had bpchem instead of tracking=bpchem. This old version works for this small example, but for some reason not in my large, more complicated file.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[runs=2]{auto-pst-pdf}
\usepackage{booktabs}
\usepackage{bpchem}
\usepackage[journal=rsc, tracking=bpchem]{chemstyle}
\usepackage{geometry}
\usepackage[version=3]{mhchem}
\usepackage{setspace}
\usepackage{bm}

\begin{document}

Text where I mention the compounds in a different order than in the schemes: \CNlabel{2}, \CNlabel{3}, \CNlabel{4}, \CNlabel{1}

\begin{scheme}
\begin{center}
\schemeref[TMP]{1}
\schemeref[TMP2]{2}
\includegraphics{bla}
\end{center}
\end{scheme}

\begin{scheme}
\begin{center}
\schemeref[TMP]{3}
\schemeref[TMP2]{4}
\includegraphics{blub}
\end{center}
\end{scheme}

\end{document}

-
With your edited code I get exactly the expected results: 1,2,3, and 4 in the text and 4,1,2, and 3 in the schemes... –  cgnieder Apr 13 '12 at 15:50
@cgnieder Just for clarification: By "edited code", you mean the one that is standing above, right? With tracking=bpchem? Strange... –  Helen Apr 13 '12 at 16:12
Yes, I mean the one you've added. I had to comment out \usepackage{E}, though. I don't know what that package is about? –  cgnieder Apr 13 '12 at 16:14
Ooops, sorry about that, thats a .sty file a friend gave me. But leaving it out doesn't change anything for me. I deleted it in the post above. –  Helen Apr 13 '12 at 16:29
What wouldn't be a solution but a workaround might be to switch to the chemnum package. It provides a single command for the numbering of compounds and its own version of \schemeref. –  cgnieder Apr 13 '12 at 20:18

You probably forgot to use the chemstyle's package option tracking=bpchem.

I can reproduce your problem using this MWE (which uses scheme-tmp.eps from the chemnum documentation):

\documentclass{article}
\usepackage[runs=2]{auto-pst-pdf}
\usepackage{bpchem}
\usepackage{chemstyle}
\begin{document}

\CNlabel{bla} some text \CNref{bla}
\begin{scheme}
\schemeref{blub}% should be '2' is '1'
\schemeref{bla} % should be '1' is '2'
\includegraphics{scheme-tmp}
\end{scheme}

\end{document}


but the behaviour is fixed if I use \usepackage[tracking=bpchem]{chemstyle}.

Edit: the problem you are having both here and in your follow-up question and both with chemstyle and chemnum is basically a problem with \psfrag (from the psfrag package they're both using internally).

If I understood you correctly the labels went wrong when you tried to declare a label for the first time inside/with \schemeref (or \cmpdref, resp.). The so declared label wasn't known afterwards. That's exactly what happens with \psfrag and an “ordinary” \label, too:

\documentclass{article}
\usepackage{psfrag}
\usepackage[runs=2]{auto-pst-pdf}

\begin{document}

\section{Section}
% setting a label with psfrag “inside the picture”...:
\psfrag{TMP1}{\label{test}test}
\psfrag{TMP2}{\gdef\foo{foo}}
\includegraphics{scheme-tmp}
% ...leaves it still undefined:
\ref{test}
% also \foo is still undefined:
\renewcommand\foo{bar}

\end{document}


I don't know enough about \psfrag and \includegraphics to know what's happening exactly but obviously macros defined inside \psfrag are not known elsewhere in the document. So every compound label you have declared with \schemeref was unknown later and hence newly declared.

You already came up with the most simple solution: declare the labels separately but “invisible” and Werner showed a way to automate this in case of \schemeref and bpchem. chemnum has been updated to v0.4b and does essentially the same now to solve the issue.

-
Including the option tracking=bpchemchanged the numbering in the text, but it is still different from the numbering in the pictures, i.e. one compound was 7 before in the text, and is now 12, but in the picture it is compound number 17. –  Helen Apr 13 '12 at 6:46
I will try to give an example in the original post to make it a little bit clearer... –  Helen Apr 13 '12 at 7:24

I know this is super late now, but just in case anybody else is having that issue, you have two options:

1.- You can continue to use the packages {chemstyle} and {bpchem}but yes, you should add it as \usepackage[tracking=bpchem]{chemstyle} otherwise it won't keep track. if you do so, you then will have to define all structures embedded in the eps file as follows:

\begin{scheme} %this could also be figure
\schemeref[TMP1]{XjU3o9}
\CNrefnolabel{XjU3o9} %Note that this **MUST** be identical otherwise the numbers will be different
\schemeref[TMP2]{A}
\CNrefnolabel{A}
\schemeref[TMP3]{B}
\CNrefnolabel{B}
...so on and so forth, depending on how many TMPX you have in your scheme/figure
\includegraphics{EPSfile}
\caption{This is what appears in the bottom of the scheme/figure as 'title'}
\label{sch:Nameit!}
\end{scheme}


2.- Don't use {bpchem} as your tracking package, instead use the newest version of {chemnum} (V 1.0). And actually if you don't want to have schemes in your document, you don't even have to use {chemstyle}! All you have to do is add \usepackage{chemnum} to your preamble and then you define your labels as follows:

\begin{scheme} %in this case you MUST have the {chemstyle} package loaded in your preamble
\replacecmpd{XjU3o9}
\replacecmpd{A}
\replacecmpd{B}
...so on and so forth, depending on how many TMPX you have in your scheme/figure
\includegraphics{EPSfile}
\caption{This is what appears in the bottom of the scheme/figure as 'title'}
\label{sch:Nameit!}
\end{scheme}


NOTE that you don't have to include TMP1 or TMPX, the program will do it automatically, starting with TMP1, so be careful where you place those labels in your EPS file. As far as I am concern, you can also explicitly say which compound you want to be nicknamed A (i.e. TMP1 or TMP4) all you have to do is add [tag=youridentifier] between \replacecmpd and {nickname}. So for the previous example it'll be:

\begin{scheme} %in this case you MUST have the {chemstyle} package loaded in your preamble
\replacecmpd[tag=TMP3]{XjU3o9}
\replacecmpd[tag=TMP1]{A}
\replacecmpd[tag=TMP2]{B}
...so on and so forth, depending on how many TMPX you have in your scheme/figure
\includegraphics{EPSfile}
\caption{This is what appears in the bottom of the scheme/figure as 'title'}
\label{sch:Nameit!}
\end{scheme}


And so TMP3 will be replaced by number 1, TMP1 by number 2 and TMP2 by number 3 once you typeset your document.

I hope this is useful for someone.

-