# Using mathsf in a section title

I'm trying to use the mathsf font in a section title and it wont compile. Here is a MWE:

\documentclass{amsart}
\usepackage{verbatim, rotating, stmaryrd, bbm, pict2e, ytableau, mathrsfs, amssymb, mathtools}

\newcommand{\Dsg}[1]{{\mathsf{D}^b_\mathsf{sg}(#1)}}
\newcommand{\APC}[1]{{\underline{\mathsf{APC}}(#1)}}

\begin{document}

\tableofcontents

\section{$\APC{S} \simeq \Dsg{S}$}

hi

\end{document}


It works without the table of contents tag, but with it when I compile this I get an error:

Latex Error: ./temp.toc:1 \mathsf allowed only in math mode.


The ./temp.toc file consists of just one line:

\contentsline {section}{\tocsection {}{1}{${\relax$\@@underline {\hbox {\mathsf {APC}}}\mathsurround \z@ $\relax (S)} \simeq {\mathsf {D}^b_\mathsf {sg}(S)}$}}{1}


I'm not sure how to parse that to figure out what's wrong. Why can't I use \mathsf in math mode in a section title?

-
The problem isn't \mathsf but \underline. This should work: \newcommand{\APC}[1]{{\protect\underline{\mathsf{APC}}(#1)}}. Or \DeclareRobustCommand{\APC}[1]{{\underline{\mathsf{APC}}(#1)}} –  clemens Oct 27 '13 at 19:46
Yep, that did the trick, thanks! I don't actually know what \protect does, is there a short explanation of why \underline was causing problems and \protect fixes it? –  Jim Oct 27 '13 at 20:01
When section titles are written to the .toc file expandable macros get expanded. Sometimes this breaks things like in this case. \protect prevents the unwanted expansion (actually it does something else and very clever... :) Defining \APS as a robust command prevents its expansion in such contexts (through something similarly clever) so \underline doesn't even get to be expanded then. –  clemens Oct 27 '13 at 20:17
–  Qrrbrbirlbel Oct 27 '13 at 20:18

The problem isn't \mathsf but \underline. The latter is a fragile command and those frequently pose problems in moving arguments such as section titles.
• \newcommand{\APC}[1]{{\protect\underline{\mathsf{APC}}(#1)}} or
• \DeclareRobustCommand{\APC}[1]{{\underline{\mathsf{APC}}(#1)}}
When section titles are written to the .toc file expandable macros get expanded. Sometimes this breaks things like in this case. \protect prevents the unwanted expansion. Defining \APS as a robust command (kind of) prevents its expansion in such contexts so \underline doesn't even get to be expanded then. More details on this can for instance be found in What is the difference between Fragile and Robust commands?