給出「好的」xbar 的程式碼工作正常,但如果在節標題中則失敗

給出「好的」xbar 的程式碼工作正常,但如果在節標題中則失敗

我一直在尋找更好的 xbar,並在大約 11 年前在這裡找到了答案:

我可以在不使用 mathabx 包的情況下獲得 \widebar 嗎?

我使用了縮寫程式碼,效果很好。但是,當我將其放在節標題中時,我收到編譯器錯誤。這是我正在使用的程式碼:

\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}

第一個實例很好,但 \section 中的實例爆炸了。這遠遠超出了我的技能水平——我不知道 \makeatletter 和 \makeatother 之間的程式碼做了什麼,所以我甚至無法開始考慮調試。我什至不知道這個問題該使用什麼標籤。

答案1

添加\protect在前面\Xbar{X},例如\section{All about $\protect\Xbar{X}$}

或使用\NewDocumentCommand{\Xbar}{m}{代替\newcommand*\Xbar[1]{.

在此輸入影像描述

相關內容