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.

6
  • 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
    Commented May 12, 2021 at 20:09
  • @leandriis, yes it works, and no I don't need a list of figures, so this solves my problem, thanks!! Commented May 12, 2021 at 20:16
  • Also works \protect\underaccent{\bar}{U}, but only without \listoffigures
    – Ivan
    Commented May 12, 2021 at 20:18
  • @leandriis, I'm curious about the cause, what is the problem here? Commented May 12, 2021 at 20:20
  • @Ivan, indeed it works too. Thanks!! Commented May 12, 2021 at 20:20

1 Answer 1

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

3
  • 1
    The underlying issue is that the accents package did not follow the suggestion of the clsguide, section 2.6, and so all the accent commands are fragile. Commented May 12, 2021 at 21:05
  • This fails in section titles, even with \protect.
    – Cryme
    Commented Feb 18, 2023 at 0:11
  • @Cryme it works just fine in a minimal document I just tested. If for some reason it breaks for you, please consider asking a new question with an MWE included. Feel free to ping me on this comment thread if you'd like my input. Commented Feb 18, 2023 at 0:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .