Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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[linktocpage=true,hidelinks,unicode]{hyperref}
\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}
share|improve this question

2 Answers 2

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.

share|improve this answer
    
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.

enter image description here

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.
.................................................
share|improve this answer
    
\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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.