Generalhow to make an \underrightarrow in \caption

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
lobsterzzz
Posts: 1
Joined: Fri Jan 11, 2008 1:25 pm

how to make an \underrightarrow in \caption

Post by lobsterzzz »

Ah, I hope anybody can help me.
I have the following code to input a picture

\begin{figure}
\begin{center}
\includegraphics[width=8cm]{pics/notinward2}
\caption{blablabla}

which works fine.
then I have the following code to make an arrow below the letter x

$\underrightarrow{x}$

which works fine as well. Now I want to have this x with the arrow below in the \caption of the figure.
so I write

\begin{figure}
\begin{center}
\includegraphics[width=8cm]{pics/notinward2}
\caption{blablabla $\underrightarrow{x}$}

but this gives me 4 error messages, which say

illegal parameter number in definition of reserved\@
<to be read again>

What am I doing wrong? how can I do what I want?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

how to make an \underrightarrow in \caption

Post by Juanjo »

Instead of $\underrightarrow{x}$, write $\protect\underrightarrow{x}$. This is a subtlety of LaTeX. Commands can be robust or fragile. The latter ones can be prematurely interpreted when used as arguments of other commands, causing compilation errors. This mainly happens when a fragile command appears in a moving argument, like those of \caption, \chapter, \section,... So, fragile commands in such arguments must be "protected" (made robust), by prefixing them with the \protect command.

Tipically, commands with optional arguments are fragile. This is why the following code fails:

\section{\framebox[12em]{Section title}}

However, we can protect \framebox and solve the problem:

\section{\protect\framebox[12em]{Section title}}

So, coming back to your problem, it seems that \underrightarrow is fragile. It suffices to protect it.
Post Reply