0

I was searching for a better xbar and found an answer here from about 11 years ago:

Can I get a \widebar without using the mathabx package?

I used the abbreviated code and it works fine. However, when I put it in a section heading I get a compiler error. Here is the code I'm using:

\documentclass{amsbook}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amssymb}
%%%%%%%%%%%% Code to make a nicer xbar %%%%%%
\makeatletter
\newcommand*\rel@kern[1]{\kern#1\dimexpr\macc@kerna}
\newcommand*\Xbar[1]{%
  \begingroup
  \def\mathaccent##1##2{%
    \rel@kern{0.8}%
    \overline{\rel@kern{-0.8}\macc@nucleus\rel@kern{0.2}}%
    \rel@kern{-0.2}%
  }%
  \macc@depth\@ne
  \let\math@bgroup\@empty \let\math@egroup\macc@set@skewchar
  \mathsurround\z@ \frozen@everymath{\mathgroup\macc@group\relax}%
  \macc@set@skewchar\relax
  \let\mathaccentV\macc@nested@a
  \macc@nested@a\relax111{#1}%
  \endgroup
}
\makeatother
%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%

\begin{document} $\Xbar{X}$ \section{All about $\Xbar{X}$} \end{document}

The first instance is fine but the one in \section blows up. This is way beyond my skill level -- I've no idea what the code between \makeatletter and \makeatother does so I can't even begin to think about debugging. I don't even know what tags to use for this question.

TonyK
  • 625
  • 2
  • 9

1 Answers1

1

Add \protect before \Xbar{X}, such as \section{All about $\protect\Xbar{X}$}.

Or use \NewDocumentCommand{\Xbar}{m}{ instead of \newcommand*\Xbar[1]{.

enter image description here

Clara
  • 6,012
  • Thanks Clara. The first option (protect) didn't work for me but the second did. I'm back in business. – TonyK Mar 02 '23 at 03:59