3

I'm writing a document involving phasors and I'd like to use egreg's notation from this post: \bar below symbol, e.g. $\underaccent{\bar}{U}$.

It works well except if I put the phasor inside a caption. This is a MWE:

\documentclass[10pt]{article}
\usepackage{accents}
\usepackage{caption}
\usepackage{tikz}

\begin{document} \begin{itemize} \item An ugly phasor: $\underline{U}$. \item Another, better good-looking phasor: $\underaccent{\bar}{U}$. \end{itemize}

\begin{figure}[h]\centering \begin{tikzpicture} \draw[-latex] (0,0) -- (3,0) node [right] {$\underaccent{\bar}{U}$}; \end{tikzpicture} \caption{One phasor.} %\caption{One phasor $\underaccent{\bar}{U}$.} % <-- NOT WORKING \end{figure}

\begin{center} \begin{tikzpicture} \draw[-latex] (0,0) -- (3,0) node [right] {$\underaccent{\bar}{V}$}; \end{tikzpicture} \captionof{figure}{Another phasor.} %\captionof{figure}{Another phasor $\underaccent{\bar}{V}$.} % <-- NOT WORKING EITHER \end{center} \end{document}

As you can see I tried a workaround with the caption package, but to no avail. I think the relevant lines in my log file are probably the following, but I don't understand them:

! Undefined control sequence.
\cc@@accent #1#2->\let \cc@style 
                                 =#1\cc@fetch {#2}\mathaccent \cc@code {\ifc...

! Illegal parameter number in definition of \reserved@a.

I suppose that there is some kind of incompatibility with the accents package and the captions but I haven't seen anything about it in the documentation. Is there a better way to do this?

I'm working with TeX Live 2020, by the way.

Juan Castaño
  • 28,426
  • 1
    Do you need a list of figures? If not, probably \caption[]{One phasor $\underaccent{\bar}{U}$.}results in the expected output? Likewise \captionof{figure}[]{Another phasor $\underaccent{\bar}{V}$.} should work. – leandriis May 12 '21 at 20:09
  • @leandriis, yes it works, and no I don't need a list of figures, so this solves my problem, thanks!! – Juan Castaño May 12 '21 at 20:16
  • Also works \protect\underaccent{\bar}{U}, but only without \listoffigures – Ivan May 12 '21 at 20:18
  • @leandriis, I'm curious about the cause, what is the problem here? – Juan Castaño May 12 '21 at 20:20
  • @Ivan, indeed it works too. Thanks!! – Juan Castaño May 12 '21 at 20:20
  • Here https://tex.stackexchange.com/a/176277/231952 @egreg suggests to use \usepackage{fixltx2e} \MakeRobust{\underaccent} or \usepackage{etoolbox}\robustify{\underaccent}. Anyway \listoffigures returns an error even with these two solutions. So the issue is still open. – Ivan May 12 '21 at 20:56

1 Answers1

4

It works if you use \DeclareRobustCommand

\documentclass[10pt]{article}
\usepackage{accents}
\usepackage{caption}
\usepackage{tikz}

\DeclareRobustCommand{\ubar}[1]{\underaccent{\bar}{#1}}

\begin{document}

\listoffigures

\begin{itemize} \item An ugly phasor: $\underline{U}$. \item Another, better good-looking phasor: $\underaccent{\bar}{U}$. \end{itemize}

\begin{figure}[h]\centering \begin{tikzpicture} \draw[-latex] (0,0) -- (3,0) node [right] {$\underaccent{\bar}{U}$}; \end{tikzpicture} \caption{One phasor $\ubar{U}$.} \end{figure}

\begin{center} \begin{tikzpicture} \draw[-latex] (0,0) -- (3,0) node [right] {$\underaccent{\bar}{V}$}; \end{tikzpicture} \captionof{figure}{Another phasor $\ubar{V}$.} \end{center} \end{document}

yields

enter image description here

Willie Wong
  • 24,733
  • 8
  • 74
  • 106