3

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?

1 Answers1

4

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.

egreg
  • 1,121,712
  • Would you say, four years later, that this is the recommended and preferable way to get a \widebar in LaTeX? – Gaussler Nov 23 '20 at 16:52
  • @Gaussler If not in shortage of math groups, I'd probably import \widebar from mathabx. But I nowhere said this is the preferable way, I just answered the question. – egreg Nov 23 '20 at 17:01
  • I have previously tried running out of math fonts. is the mathabx implementation better than this one? – Gaussler Nov 23 '20 at 17:04
  • @Gaussler In order to import \widebar from mathabx you must use a new math group. – egreg Nov 23 '20 at 17:06
  • So, is the solution provided by Vogt (properly protected) as good/equivalent to the one from mathabx? Because in that case, I think I prefer Vogt’s solution. – Gaussler Nov 23 '20 at 17:07