Hi Ilario,
welcome to the LaTeX community!
The problem is that both
\chaptermark and
\sectionmark (which are responsible for setting the corresponding headers) expand
\MakeUppercase on their argument. This makes
\cmpd+{test} effectively to
\cmpd+{TEST} and since the compound
TEST was never defined there is no number. If it had been defined you'd have gotten the wrong number and maybe not even noticed that there is a problem.
With the help of textcase which provides
\MakeTextUppercase{} and
\NoCaseChange{}, and etoolbox which provides means to patch commands the problem can be solved:
Code: Select all
\documentclass{book}
\usepackage{chemnum}
\usepackage{fancyhdr}
\pagestyle{headings}
\cmpdsetup{cmpd-delim=()}
\usepackage{etoolbox,textcase}
\patchcmd\sectionmark
{\MakeUppercase}% search
{\MakeTextUppercase}% replace
{}% success
{}% failure
\patchcmd\chaptermark
{\MakeUppercase}% search
{\MakeTextUppercase}% replace
{}% success
{}% failure
\begin{document}
\chapter{TeSt \noexpand\NoCaseChange{\cmpd+{test}}}
\cleardoublepage
\section{Synthesis of TeSt \protect\NoCaseChange{\cmpd+{test}}}
Mix Te and St to obtain a TeSt molecule \cmpd{test}.
\end{document}
Note that
\NoCaseChange needs to be unexpanded until it is needed in the header which is why premature expansion must be prevented with
\noexpand or
\protect.

- cmpdhdr1.png (2.98 KiB) Viewed 3576 times

- cmpdhdr2.png (12.96 KiB) Viewed 3576 times
By the way: with a non-standard class like
scrbook (a
KOMA-Script class) there is no problem since it doesn't use
\MakeUppercase in the first place.
Regards