[Tex/LaTex] Use \raisebox with \depth in caption

boxescaptionsrobust-commands

I want to use \raisebox in a caption. I know that \protect is often needed in these situation, but this (and other combinations I have tried) does not work:

\documentclass{article}
\begin{document}
    \begin{figure}
        \caption{\protect\raisebox{\protect\depth}{X}}
    \end{figure}
\end{document}

gives

! Missing number, treated as zero.
<to be read again> 
                   \protect 
l.4 ...ption{\protect\raisebox{\protect\depth}{X}}

While

\documentclass{article}
\begin{document}
    \begin{figure}
        \caption{\protect\raisebox{\depth}{X}}
    \end{figure}
\end{document}

gives

! Undefined control sequence.
<argument> ...respaces \protect \raisebox {\depth 

Best Answer

\protect does not help here, because it is \relax, when the caption is set in the figure. Also \depth is defined locally by \raisebox. That allow a trick, if \depth is not used otherwise. Instead of an undefined command, \depth can be made robust:

\let\depth\relax

File:

\documentclass{article}
\let\depth\relax
\begin{document}
    \listoffigures
    \begin{figure}
        \caption{\protect\raisebox{\depth}{g}}
    \end{figure}
\end{document}

A different way is the definition of a robust macro, with includes the argument:

\documentclass{article}
\DeclareRobustCommand*{\RaiseBoxByDepth}{%
    \raisebox{\depth}%
}
\begin{document}
    \listoffigures
    \begin{figure}
        \caption{\RaiseBoxByDepth{g}}
    \end{figure}
\end{document}

Result