[Tex/LaTex] Undefined control sequence, title, math and newcommand

errorsmacrosmath-modemusictitles

Deal all,

I have the following couple of new commands, using amsmath and relsize packages :

\newcommand*{\chiffraged}[2]{$\genfrac{}{}{0pt}{}{\mathlarger{#1}}{\mathlarger{#2}}$}
\newcommand*{\sixcinqbarre}{\chiffraged{6}{\cancel{5}}}

which allow me to display, in a regular text, a musical chord notation like this (chord C) :
enter image description here

When I use my \sixcinqbarre in my text, no problem, everyting works (example in french, sorry):

     On trouvera tout naturellement l'écriture \textit{do ré}\quartesixte~\textit{mi}6
précédée du \textit{si}\sixcinqbarre .
     L'accord de passage est en général faible tandis que les deux accords de tonique sont
    placés sur les temps forts.
    L'accord sur la sensible est ainsi lui aussi en position faible. On a
    donc \textit{si do ré mi}. Nous en donnons un exemple.

gives:

enter image description here

When I use it in a title with no toc reference, like \subsection*{\sixcinqbarre}, idem, no problem. But when I use it in a regular title, like this \subsection{\sixcinqbarre}, I have the following error :

! Undefined control sequence.
\rs@makelargerchoice ...displaystyle \rs@mathatom
\m@th $}}{\displaystyle \l...
l.197 ...\sixcinqbarre ~avec la basse descendante}
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

Does anyone have a clue about this?

Edit: After some tests, it seems that the use of the hyperref package is the cause of the error. Here is a code to test:

\documentclass[a4paper]{book}
\usepackage[]{hyperref}
\usepackage{cancel}
\usepackage{amsmath}
\usepackage{relsize}

\DeclareRobustCommand*{\chiffraged}[2]{$\protect\genfrac{}{}{0pt}{}{\protect\mathlarger{#1}}{\protect\mathlarger{#2}}$}
\DeclareRobustCommand*{\sixcinqbarre}{\chiffraged{6}{\cancel{5}}}

\begin{document}
\section{\sixcinqbarre}
\end{document}

Comment the \usepackage[]{hyperref}, and it works! However I would like to keep it…any idea?

Best Answer

The use of hyperref requires another level of protection:

\documentclass[a4paper]{book}
\usepackage{cancel}
\usepackage{amsmath}
\usepackage{relsize}
\usepackage[]{hyperref}
\usepackage{bookmark} % recommended

\DeclareRobustCommand*{\chiffraged}[2]{%
  $\genfrac{}{}{0pt}{}{\mathlarger{#1}}{\mathlarger{#2}}$%
}
\DeclareRobustCommand*{\sixcinqbarre}{\chiffraged{6}{\cancel{5}}}
% also protect the commands for the bookmarks
\pdfstringdefDisableCommands{%
  % provide an approximate representation for the bookmarks
  \renewcommand\chiffraged[2]{#1 #2}%
  \renewcommand\sixcinqbarre{6/5}%
}

\begin{document}

\section{\sixcinqbarre{} \chiffraged{6}{4}}

\end{document}

You may consider a different definition for \chiffraged:

\DeclareRobustCommand*{\chiffraged}[2]{%
  \begingroup\renewcommand{\arraystretch}{0}%
  \begin{tabular}{@{}c@{}}#1\\[1pt]#2\end{tabular}%
  \endgroup
}