Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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)}} –  cgnieder 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. –  cgnieder Oct 27 '13 at 20:17
    

1 Answer 1

up vote 4 down vote accepted

Collecting the comments into an answer:

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.

Here are two definitions that should work:

  • \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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.