Latest news 2020-07-03: SmashWords sale (ends 31st July 2020): 50% off crime/SF novel The Private Enemy and 100% off (free!) I’ve Heard the Mermaid Sing.

# Bug Tracker

ID: 45 🔗 Closed (Fixed) mfirstuc v1.06 Jan 2013-11-04 05:26:03 \xcapitalisewords doesn't work in document structure headings

## Report

\xcapitalisewords cannot be used inside the structuring commands, such as \subsection or \chapter, because a lot of errors are produced.

### MWE

\documentclass{scrbook}

\usepackage{xspace}
\usepackage{mfirstuc}

\newcommand{\MyCommandbase}{text to capitalise}
\newcommand{\MyCommand}{\xcapitalisewords{\MyCommandbase}\xspace}

\begin{document}

\chapter{First Chapter}
\MyCommand.

\section{\MyCommand}
\section{Test}
\MyCommand.

\subsection{\MyCommand}
\subsection{Test}
\MyCommand.

\chapter{\MyCommand}
\chapter{Test}
\MyCommand.

\end{document}

## Evaluation

\xcapitalisewords (and \capitalisewords, which underlies it) is a fragile command and therefore needs protecting in a moving argument:
\newcommand{\MyCommand}{\protect\xcapitalisewords{\MyCommandbase}\xspace}

I will make \capitalisewords robust in the next version.

Update (2013-11-04) 13:54 BST:

When the next version is released this will no longer be an issue, but in the interim you can make \capitalisewords robust via etoolbox's \robustify command:

\documentclass{scrbook}

\usepackage{hyperref}
\usepackage{xspace}
\usepackage{mfirstuc}

\usepackage{etoolbox}
\robustify\capitalisewords

\newcommand{\MyCommandbase}{text to capitalise}
\newcommand{\MyCommand}{\xcapitalisewords{\MyCommandbase}\xspace}

\begin{document}

\chapter{First Chapter}
\MyCommand.

\section{\MyCommand}
\section{Test}
\MyCommand.

\subsection{\MyCommand}
\subsection{Test}
\MyCommand.

\chapter{\MyCommand}
\chapter{Test}
\MyCommand.

\end{document}


Comment from Jan
Date: 2013-11-04 07:22:51 CST

Ok, this minimal working example works if you add \protect. But, it's not working if you add the hyperref package to the preamble:

\usepackage[pdftex]{hyperref}

This is the case in my document and that's why \protect doesn't work either for me.

Comment from Jan
Date: 2013-11-04 09:42:15 CST

Thanks a lot. This works like a charm.

Comment from Jan
Date: 2013-11-05 02:54:57 CST

Sorry, but I have one addition to this issue. The problem above originates in the collaboration with the hyperref package. Now I recognized that I get the following warnings each time I use my defined command in headings:

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):

(hyperref)                removing \capitalisewords' on input line 1.

The result is that I only get lowercase letters in the meta data and bookmarks of the pdf. Can this be solved anyhow?

Comment from Nicola Talbot
Date: 2013-11-04 09:09 BST

The command is too complicated to expand to a simple PDF string, so it gets ignored by the PDF bookmarks (which is what's generating the warning message). See, for example, Token not allowed in PDFDocEncoded string, Hyperref - Token not allowed and Hyperref warning - Token Not Allowed in PDF String for a discussion on the topic. You can suppress the warning with:

\pdfstringdefDisableCommands{\def\capitalisewords#1{#1}}

but the PDF bookmark will still appear in lower case. (Note that even a basic \chapter{\uppercase{s}ample}` will cause the same problem, so the issue can't be solved.)

Comment from Jan
Date: 2013-11-05 03:32:17 CST

Ok, thanks.

## Watch This Report

If you supply your name, it will be used in the email greeting, which provides a more personal message, otherwise you'll just get a generic greeting. If you have previously supplied your name when signing up for notifications, you don't need to resupply it unless you want to change it.

If you have previously subscribed to notifications for this report, you can unsubscribe by clicking on the "Stop Notification" button.

The "Confirm Bug ID" field helps to protect against spambots. Please enter the bug ID (which you can find at the top of this page).

Name: (Optional.)