Math & ScienceCompound Number not displayed in Page Header

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
ilario
Posts: 1
Joined: Thu Apr 18, 2013 12:46 pm

Compound Number not displayed in Page Header

Post by ilario »

Hi!

I have a problem when, using chemnum and fancyhdr, I put a \cmpd number in a section or chapter title and this title appears in a page header. In the page header there's no molecule number, only empty parentheses (). How can I have the cmpd number also in the header?

Code: Select all

\documentclass{book}

\usepackage{chemnum}
\usepackage{fancyhdr}

\pagestyle{headings}
\cmpdsetup{cmpd-delim=()}

\begin{document}

\chapter{TeSt \cmpd+{test}}

\cleardoublepage

\section{Synthesis of TeSt \cmpd+{test}}

Mix Te and St to obtain a TeSt molecule \cmpd{test}.

\end{document}
Thanks,
Ilario

P.S.:
If you get an error like this:

Code: Select all

Missing number, treated as zero. \cmpdsetup{cmpd-delim=()}
you can upgrade chemnum to 0.6a (the fix is this) or comment out this line.

Code: Select all

% \cmpdsetup{cmpd-delim=()}
Ilario Gelmetti - eigenLab - Scuola Normale Superiore di Pisa - Chimico

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Compound Number not displayed in Page Header

Post by cgnieder »

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
cmpdhdr1.png (2.98 KiB) Viewed 3442 times
cmpdhdr2.png
cmpdhdr2.png (12.96 KiB) Viewed 3442 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
site moderator & package author
Post Reply