# Using a widebar accent in a caption

I am using a \widebar accent as implemented here, since I don't want to use mathabx; but when I use the accent in the caption of a figure, I get a compiler error.

Here's the offending code:

\caption{(Blah blah) and $(\widebar{N},\widebar{X})$.
\label{fig:Markov_pf_notation}}


And the error:

Argument of \Hy@tempa has an extra }.\par ...n the right $(\widebar{N},\widebar{X})$.} Paragraph ended before \Hy@tempa was complete.\par ...n the right $(\widebar{N},\widebar{X})$.}

I have no trouble using \widebar in the body of my document, and when I comment out this instance in the caption, everything compiles correctly.

Can this implementation be fixed? Is there a comparably good implementation of \widebar that doesn't have this problem?

The code in Hendrik Vogt's answer has the line

\newcommand*\widebar[1]{%
[...code...]
}


but the definition is “unsafe” for usage in moving arguments, such as captions and sectional titles. So \widebar is to be considered fragile. See What is the difference between Fragile and Robust commands? for more information.

Either you keep the definition as is and use \protect\widebar in moving arguments, or remove the problem altogether by changing that line into

\newcommand{\widebar}{}% initialize
\DeclareRobustCommand*\widebar[1]{%
[...code...]
}


The “initialize” line is just for ensuring \widebar is not yet defined, because \DeclareRobustCommand silently overrides previous definitions.