2

This question was already asked on GitHub. The author of the great chemnum package initially proposed that one can define custom labels for chemical compounds. This is very useful when referencing compounds with only slightly differing substituents.

A MWE looks like this:

% arara: pdflatex

\documentclass{article}
\usepackage{chemnum}
\usepackage{chemformula}
\usepackage{hyperref}

\ExplSyntaxOn
\NewDocumentCommand \setcmpdlabel {mm}
  { \chemnum_compound_set_property:nnn {#1} {counter-representation} {#2} }
\ExplSyntaxOff

\cmpd*{Me2bdc}
\setcmpdlabel{Me2bdc}{\ch{Me2L^1}}
\cmpd*{H2bdc}
\setcmpdlabel{H2bdc}{\ch{H2L^1}}

\begin{document}

\section{About \texorpdfstring{\cmpd{Me2bdc}}{\cmpdplain{Me2bdc}}}
Lorem ipsum.

\section{About \texorpdfstring{\cmpd{H2bdc}}{\cmpdplain{H2bdc}}}
Lorem ipsum.

\end{document}

and will produce the following PDF output:

enter image description here

Within the section titles, the compounds Me2L1 and H2L1 (which are internally assigned by chemnum to <ID> 1 and 2, respectively) are represented as desired. However, the PDF bookmarks (left column) show the values of <ID> instead of Me2L1 (H2L1). How can I bring the user-defined values to PDF bookmarks? \cmpdproperty{H2bdc}{counter-representation} will also not print the value of counter-representation.

I'm aware that the inclusion of subscripts/superscripts within PDF bookmarks may need conversion to appropriate Unicode chars and maybe somebody has an idea on how to achieve this. It may be preferable to define a PDF bookmark representation for every user-defined compound, I guess?

Thanks for your help!

tstone-1
  • 467
  • 4
  • 13
  • Maybe I am missing something, but it seems to me that you are not actually using the main feature of the chemnum package, i.e. automatic numbering of "compounds". Why not just use mhchem? \section{About \ce{Me2L^1}} does exactly what you want, I think. – schtandard Jun 07 '20 at 13:02
  • You're right, I'm taking manual control of a limited number of compounds. However, I still need automatic numbering (1, 2, ... n) for other compounds within the document. The advantage of the approach mentioned above is that one can quite easily change the order of L^1 and L^2 when they are defined in the preamble to reflect the order of occurrence within the document. Using \ce{Me2L^1} (or, using chemformula: \ch{Me2L^1}) would require to adjust every single occurrence of every L throughout the document. – tstone-1 Jun 07 '20 at 18:21

1 Answers1

1

It is not quite clear what your question is actual about. If (as your example seems to imply) are willing to use \texorpdfstring you mainly need to setup some sensible representation for the bookmarks:

\documentclass{article}
\usepackage{chemnum}
\usepackage{chemformula}
\usepackage{hyperref}

\ExplSyntaxOn
\NewDocumentCommand \setcmpdlabel {mm}
  { \chemnum_compound_set_property:nnn {#1} {counter-representation} {#2} }
\ExplSyntaxOff

\cmpd*{Me2bdc}
\setcmpdlabel{Me2bdc}{\ch{Me2L^1}}
\cmpd*{H2bdc}
\setcmpdlabel{H2bdc}{\ch{H2L^1}}

\makeatletter
\@namedef{mypdfcmpd-Me2bdc}{Me2bdc whatever}
\@namedef{mypdfcmpd-H2bdc}{H2bdc something else}

\newcommand\cmpdbkm[1]{\csname mypdfcmpd-#1\endcsname}
\makeatother

\begin{document}

\section{About \texorpdfstring{\cmpd{Me2bdc}}{\cmpdbkm{Me2bdc}}}
Lorem ipsum.

\section{About \texorpdfstring{\cmpd{H2bdc}}{\cmpdbkm{H2bdc}}}
Lorem ipsum.

\end{document}

enter image description here

But you naturally will have to experiment a bit which representation looks okay there - with lualatex or xelatex you can use all unicode.

Ulrike Fischer
  • 327,261
  • Thank you. I've made the following changes to your code: 1) \usepackage[unicode]{hyperref}; 2) add \usepackage{bookmark}; 3) \@namedef{mypdfcmpd-Me2bdc}{Me\unichar{"2082}L\unichar{"00B9}}; 4) \@namedef{mypdfcmpd-H2bdc}{H\unichar{"2082}L\unichar{"00B9}}; This finally produces what I was looking for! Guess there might be more to simplify and automate here, but that's all my understanding allows at present. Won't move away from pdflatex for this project now, but will consider it in the future. Please excuse me for being unclear about my intentions. – tstone-1 Jun 07 '20 at 18:58