# Chemnum putting wrong numbers when compound label is introduced as a variable (stored as commands)

I'm on the process of migrating to the chemnum numbering system due to its advanced features (thank you!). For my purposes I need to input external files with the information (for each compound) stored as commands, and then applying those commands into a template (to "fill in" the information).

You can see the "minimal" code below (I have not cropped the packages that I'm using in the preamble just in case there's a conflict or order issue I'm unaware of). Basically there are three commands that store the information of the compound and a fourth command that inserts a new section and a EPS figure (with a label 'TMP1' to replace). When compiling, this label is OK in the second event but not in the first one (!!!?).

As the replacement occurs, I don't think is any problem with the psfrag part of it, but with the numbering of the compounds itself. If the "renewcommand" method is unsuitable for chemnum, is there any other alternative to introduce information from different files and apply consecutively the same template to them?

Thanks a lot!

\documentclass[a4paper,12pt,oneside]{article}
\usepackage{environ,fixltx2e}
\usepackage{titletoc}
\usepackage{fullpage}
\usepackage[detect-all]{siunitx}
\usepackage{graphicx}
\usepackage[runs=2,crop=off]{auto-pst-pdf}
\usepackage{chemnum}
\usepackage[journal=rsc]{chemstyle}
\usepackage[version=3]{mhchem}
\usepackage{bm}
\usepackage{multicol}
\usepackage{etoolbox}
\newcommand{\compoundtag}{}
\newcommand{\nameofthecompound}{}
\newcommand{\descriptiontext}{}

\newcommand{\compoundDATA}{
\section{\nameofthecompound{} - \cmpdplain{\compoundtag}}
\begin{scheme}[H]
\replacecmpd[tag=TMP1]{\compoundtag}
\includegraphics{a.eps}
\end{scheme}
\descriptiontext
}

\begin{document}

Main text mentioning \cmpd{A} and \cmpd{B}

%This data comes from a different file through an input event.
\renewcommand{\compoundtag}{A}
\renewcommand{\nameofthecompound}{Name of compound A}
\renewcommand{\descriptiontext}{Text of the compound A}
\compoundDATA

%This data comes from a different file through a second input event.
\renewcommand{\compoundtag}{B}
\renewcommand{\nameofthecompound}{Name of compound B}
\renewcommand{\descriptiontext}{Text of the compound B}
\compoundDATA

\end{document}

-

This is an expansion issue and not really chemnum's fault. If you add \setchemnum{log=true} or even \setchemnum{log=verbose} you'll get information on which compound labels are defined by chemnum in the log file:

.................................................
. chemnum info: defined new compound A' with output 1.
.................................................
.................................................
. chemnum info: defined new compound B' with output 2.
.................................................
.................................................
. chemnum info: defined new compound \compoundtag ' with output .
.................................................


This means that \compoundtag should be expanded once before fed to \cmpd and friends.

\expandafter\cmpdplain\expandafter{\compoundtag}


and

\expandafter\replacecmpd\expandafter{\compoundtag}


(tag=TMP1 is superfluous – it's the default)

Using options with \replacecmpd will require a little bit more effort. Maybe something like this:

\begingroup
\edef\x{\endgroup
\replacecmpd[tag=TMP1]{\expandonce\compoundtag}%
}\x


\expandonce is provided by etoolbox which you are loading already.

-
I don't know about this expansion problem but I'll look into this, probably will be useful for most of the information I'm filling in from the variables I input. So, thank you very much and thanks for developing chemnum! It's is great. –  abrantxo Jul 20 at 22:21
You don't need \noexpand in front of \replacecmpd, because it's defined with \NewDocumentCommand, so it's not expanded in \edef. –  egreg Jul 20 at 22:33
@egreg Ah, yes I forgot that it's robust (for obvious reasons...). Thanks. –  cgnieder Jul 20 at 22:36

Here's a solution that just requires defining a slightly different version of the command (I removed the inessential packages, the demo option to graphicx is just for the example, don't add it yourself).

\documentclass[a4paper,12pt,oneside]{article}
\usepackage[demo]{graphicx}
\usepackage{chemnum}
\usepackage[journal=rsc]{chemstyle}
\usepackage[version=3]{mhchem}
\newcommand{\compoundtag}{}
\newcommand{\nameofthecompound}{}
\newcommand{\descriptiontext}{}

\setchemnum{log=true}
\ExplSyntaxOn
\NewDocumentCommand \xreplacecmpd { t+ o m }
{ \chemnum_cmpd_replace:nno { #1 } { #2 } { #3 } }
\cs_generate_variant:Nn \chemnum_cmpd_replace:nnn { nno }
\ExplSyntaxOff

\newcommand{\compoundDATA}{
\section{\nameofthecompound{} - \cmpdplain{\compoundtag}}
\begin{scheme}[H]
\xreplacecmpd[tag=TMP1]{\compoundtag}
\includegraphics{a.eps}
\end{scheme}
\descriptiontext
}

\begin{document}

Main text mentioning \cmpd{A} and \cmpd{B}

%This data comes from a different file through an input event.
\renewcommand{\compoundtag}{A}
\renewcommand{\nameofthecompound}{Name of compound A}
\renewcommand{\descriptiontext}{Text of the compound A}
\compoundDATA

%This data comes from a different file through a second input event.
\renewcommand{\compoundtag}{B}
\renewcommand{\nameofthecompound}{Name of compound B}
\renewcommand{\descriptiontext}{Text of the compound B}
\compoundDATA

\end{document}


Note \xreplacecmd instead of \replacecmpd.

Here's the relevant part of the log, generated by the option log=true:

.................................................
. chemnum info: defined new compound A' with output 1.
.................................................
.................................................
. chemnum info: defined new compound B' with output 2.
.................................................

-
\replacecmpd is not deprecated, \cmpdref (its predecessor) is –  cgnieder Jul 20 at 22:28
@cgnieder Sorry, I badly interpreted the line in the package. Isn't my solution neater? ;-)` –  egreg Jul 20 at 22:31