1

I am using the chemnum package of Clemens Niederberger to label my chemical compounds. Now I have quite a large amount of compounds and sometimes it would be helpful to see the label in the document without looking for the corresponding line of code (of course only during preparation of the manuscript). A similar feature comes with the showkeys package, but of course that works only for "real" labels.

I created an sample image to clarify, what I am talking about. Is it possible, to redefine \cmpd with an option to print the label?

Edit:

I am now aware of the show-keys option of the package, however this fails when using chemnum inside of tikz environments. \setchemnum{show-keys=ref} did not help, because it does not show the label when the compound appears the first time.

My workaround so far is to redefine cmpd to run like \cmpdshowref{compound}\cmpd{compound}. If there is a smarter way to do this, I am happy to read about it.

Edit 2 (added now a MWE):

• The show-keys option runs into errors, when the first appearance of a compound is inside a tikz environment.

• Apparently, the show-keys option runs into errors when the label contains an underscore (e.g. compound_2).

\documentclass{article}
\usepackage{chemnum,tikz}
\setchemnum{show-keys}

\begin{document}

Lorem ipsum \cmpd{foobar} dolor.

Lorem ipsum \cmpd{foobar} dolor.

\tikz \node[fill=yellow] at (0,0) {Lorem ipsum \cmpd{foobar} dolor.};

\tikz \node[fill=yellow] at (0,0) {Lorem ipsum \cmpd{foobar2} dolor.}; % <- fails

Lorem ipsum \cmpd{foobar_2} dolor. % <- fails

\end{document}

enter image description here

  • There very likely is a smarter way but as I said: it is hard if not impossible to say more without a minimal working example. – clemens Oct 21 '16 at 12:14
  • I edited my question and added an MWE. – es560 Oct 21 '16 at 12:26
  • I extended my answer to cover your additional questions… – clemens Oct 22 '16 at 12:02
1

Answer to original question:

chemnum has this built-in, see section 12 Debugging Information of the manual:

\documentclass{article}

\usepackage{chemnum}
\setchemnum{show-keys}% <<< this is what you're looking for

\begin{document}

Lorem ipsum \cmpd{foobar} dolor

Lorem ipsum \cmpd{foobar} dolor

\end{document}

enter image description here

The key of a newly defined label is printed to the margin while a call referencing an existing label is printed inline.

Answer to edited question:

The two “failure” cases you mention have different causes:

  1. \tikz \node[fill=yellow] at (0,0) {Lorem ipsum \cmpd{foobar2} dolor.};
    

    This leads to an error because the default definition of \cmpdshowdef uses a \marginpar. Since this is quite similar to a float you cannot use it inside a box (tikz node). A solution is to redefine it using the \marginnote command from the \marginnote package. The needed redefinitions are shown in the chemnum manual section 12 on page 25:

    \usepackage{marginnote}
    \renewcommand*\cmpdshowdef[1]{%
      \marginnote{\fbox{\normalfont\ttfamily#1}}%
    }
    \renewcommand*\subcmpdshowdef[2]{%
      \marginnote{\fbox{\normalfont\ttfamily#2 (#1)}}%
    }
    
  2. Lorem ipsum \cmpd{foobar_2} dolor.
    

    This fails due to the same reason you cannot use the underscore character _ in normal text and gives the expected error: Missing $ inserted.. This happens when trying to print the label text. \detokenize can help here:

    \renewcommand*\cmpdshowdef[1]{%
      \marginnote{\fbox{\normalfont\ttfamily\detokenize{#1}}}%
    }
    \renewcommand*\subcmpdshowdef[2]{%
      \marginnote{\fbox{\normalfont\ttfamily\detokenize{#2 (#1)}}}%
    }
    

The full example:

\documentclass{article}
\usepackage{chemnum,tikz}
\setchemnum{show-keys}

\usepackage{marginnote}
\renewcommand*\cmpdshowdef[1]{%
  \marginnote{\fbox{\normalfont\ttfamily\detokenize{#1}}}%
}
\renewcommand*\subcmpdshowdef[2]{%
  \marginnote{\fbox{\normalfont\ttfamily\detokenize{#2 (#1)}}}%
}


\begin{document}

Lorem ipsum \cmpd{foobar} dolor.

\bigskip

Lorem ipsum \cmpd{foobar} dolor.

\bigskip

\tikz \node[fill=yellow] at (0,0) {Lorem ipsum \cmpd{foobar} dolor.};

\bigskip

\tikz \node[fill=yellow] at (0,0) {Lorem ipsum \cmpd{foobar2} dolor.}; % <- fails

\bigskip

Lorem ipsum \cmpd{foobar_2} dolor. % <- fails 

\end{document}

enter image description here

  • Thanks for your reply. I browsed the manual of chemnum but somehow I did not find this feature. However, this solution does not work in my case, because I use chemnum inside of tikz environments and the option show-keys fails here. But due to your manual, I redefined \cmpd{compound} to \cmpdshowref{compound}\cmpd{compound}. Right now, I don't see any troubles, but this does not mean anything. Is there a smarter way to define this? – es560 Oct 21 '16 at 11:43
  • @es560 there will still be an error when using the underscore and the label is referenced. I'll add something later or tomorrow how to cover that case (depending on when I have time…) – clemens Oct 22 '16 at 12:27

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.