# How can I use \dbox in a caption? [duplicate]

I’m trying to use a \dbox from the dashbox package in the caption of a figure, like this:

\documentclass{article}

\usepackage{dashbox}

\begin{document}
\begin{figure}
\centering Imagine some figure here

\caption{\dbox{Boxed text} in caption.}
\end{figure}
\end{document}


The result I get looks like expected: However, I get plenty of errors:

Undefined control sequence. \caption{\dbox{Boxed text} in caption.}
Undefined control sequence. \caption{\dbox{Boxed text} in caption.}
Illegal parameter number in definition of \reserved@a. \caption{\dbox{Boxed text} in caption.}
Extra \or. \caption{\dbox{Boxed text} in caption.}
...


The package documentation says:

\dbox{text} works like \fbox, but the box is drawn with dashed lines.

But \fbox works fine inside a caption.

The boxes from the fancybox package seem to have similar problems.

What is the correct way to draw a dashed box in a caption?

• Use \caption{\protect\dbox{Boxed text} in caption.} – egreg Aug 13 at 10:29
• Thanks, this solved my problem. – Roman Langrehr Aug 13 at 10:32

If an alternative text for the list of figures is provided, \dbox can be used without further ado

\documentclass{article}

\usepackage{dashbox}

\begin{document}
\begin{figure}
\centering Imagine some figure here

\caption[text for list of figures]{\dbox{Boxed text} in caption.}
\end{figure}
\end{document}