3

This is a follow-up question to Numbering of compounds in the order it appears in the schemes (bpchem or similar)

I had the same problem of the author of the previous question and I tried the solution proposed by @Werner: it works almost perfectly. Anyway, there is a minor issue I would like to solve (but most of all I would like to understand why it doesn't work as I expect).

Here is my code:

\documentclass{article} 
\usepackage[runs=2]{auto-pst-pdf}
\usepackage{bpchem}
\usepackage[tracking=bpchem]{chemstyle}
\usepackage[version=3]{mhchem}

\usepackage{letltxmacro}% http://ctan.org/pkg/letltxmacro
\LetLtxMacro\oldschemeref\schemeref
\DeclareRobustCommand{\schemeref}[2][]{%
\CNlabelnoref{#2}%
\oldschemeref[#1]{#2}%
}

\begin{document}
\begin{scheme}
  \schemeref{comp1}
  \schemeref{comp2}
  \schemeref{comp3}
  \includegraphics{scheme1}
\end{scheme}

Compound number \CNref{comp1}, compound number \CNref{comp2}.
\end{document}

It's almost the same code as the previos question, there is only a difference: the command \schemeref is used here without the optional argument [TMPX]. If I add the optional argument (which I don't need since my schemes have the standard label TMP1, TMP2, ecc...) everything works fine: the output pdf has correct compound numbering in the schemes and correct compound referencing in the test. If I drop the option argument the referencing in the text continues to work fine (the command \CNref is substituted with the number of the corresponding compound) but the labels TMP1, TMP2, ecc... in the schemes are no more substituted with the expected number! They are left as they are, TMP1, TMP2...

Here an image to clarify: labels not sbustituted

I was wondering why the redefinition of \schemeref doesn't handle properly the empty optional argument... No errors or warnings from Latex.

I am a new italian user of StackEschange, so forgive any mistake I made!

Thank you for any help.

  • This won't work: as schemes can float, you have to set up your labelling before the float (outside the scheme). – Joseph Wright Jul 24 '15 at 8:02
  • Do you mean that I need to declare the compounds out of the floating scheme environment? – Rackbox Jul 24 '15 at 8:06
  • @Rackbox Yes, much the same as citations or any other moving references – Joseph Wright Jul 24 '15 at 16:00
  • @JosephWright: why? How do you explain the fact that \schemeref[TMP1]{comp1} works perfectly while \schemeref{comp1} doesn't? – Rackbox Jul 24 '15 at 20:41
  • 1
    @Rackbox you need to test if #1 is blank and if it is use \oldschemeref without optional argument, I guess. Otherwise you're probably telling \oldschemeref to replace an empty string instead of the default one… – clemens Jul 31 '15 at 15:17
3

You need to test if the optional argument is blank and call \oldschemeref without optional argument if it is. You can do this in a variety of ways. Manually:

\DeclareRobustCommand{\schemeref}[2][]{%
  \CNlabelnoref{#2}%
  \if\relax\detokenize{#1}\relax
    \oldschemeref{#2}%
  \else
    \oldschemeref[{#1}]{#2}%
  \fi
}

Load etoolbox and use \ifblank:

\usepackage{etoolbox}
\renewrobustcmd{\schemeref}[2][]{%
  \CNlabelnoref{#2}%
  \ifblank{#1}
    {\oldschemeref{#2}}
    {\oldschemeref[{#1}]{#2}}%
}

Use xparse:

\usepackage{xparse}
\RenewDocumentCommand{\schemeref}{om}{%
  \CNlabelnoref{#2}%
  \IfNoValueTF{#1}
    {\oldschemeref{#2}}
    {\oldschemeref[{#1}]{#2}}%
}

Or … any number of other ways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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