# \widehat in \index inside align environment does not show up properly in the index

Why does the \widehat mathematical symbol show up as follows in the MWE below?

\documentclass{scrartcl}
\usepackage{bm}
\usepackage{amsmath}
\newcommand*{\xv}{\operatorname{xv}}
\usepackage[noautomatic]{imakeidx}
\makeindex[intoc]

\newcommand*\lettergroupDefault[1]{\textbf{\sffamily Symbols}}
\newcommand*\lettergroup[1]{%
\par\textbf{\sffamily#1}\par
\nopagebreak
}

\begin{document}
\begin{align*}
E=mc^2\index{. xv@$\widehat{\xv}_n$}
\end{align*}
\printindex
\end{document}

• \protect\widehat The contents of \index are written to an external file which always triggers full expansion and \widehat is not protected. – Henri Menke Nov 16 '18 at 1:24
• That solved the problem, Henri, thanks. Do you want to provide it as answer? – Marius Hofert Nov 16 '18 at 3:24

If you look in the .idx file, you will find

\indexentry{. xv@$\mathaccent "0362\relax {\xv }_n$}{1}


which fails because " is a quoting character in .idx files, so this is interpreted as if it were \mathaccent 362 changing hexadecimal into decimal.

A way out could be \protect\widehat, but I'd not recommend this. The problem is that

\widehat{\xv}


does not behave as an operator as far as spacing is concerned, because it ends up being an Acc atom, which is later treated as an ordinary one. Try with

\begin{align*}
\xv A &= 2 \\
\hxv A &= 2 \\
\widehat{\xv} A &= 2
\end{align*}


where \xv and \hxv are properly defined by

\DeclareMathOperator{\xv}{xv}
\DeclareMathOperator{\hxv}{\widehat{\xv}}


The output is

and you can immediately see the problem.

Using \hxv in the \index command will write \hxv (and similarly \xv will write itself) instead of doing nasty expansions. The proposed definition of \xv is to be preferred to \newcommand{\xv}{\operatorname{xv}} also for this reason.

Beware that the same problem with " will appear with delimiters: protect also things such as \lbrace.