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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
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