3

Is it possible to use \includegraphics within the caption of a figure? When I try, I get an error. Here is a complete (sans graphics) minimal example that also demonstrates why I want to do this:

\documentclass{article}
\usepackage{graphicx}

\newcommand{\Triangle}{\raisebox{-0.3em}{\includegraphics[height=1.25em]{triangle}}}

\begin{document}

% this use of \Triangle below works fine:
\Triangle

\begin{figure}[ht]
%  \includegraphics{...}
   \caption{example caption \Triangle . }
\end{figure}

\end{document}  

I do this because I need to include small symbols into the text, which I have as PDF graphics (it's not just a basic triangle---I know I could get that as a character). If I remove the \newcommand part and just do a plain \includegraphics, I get the same error.

This is the error I get:

./Untitled.tex:12: Argument of \@caption has an extra }.
<inserted text> 
                \par 
l.12    \caption{example caption \Triangle . }
Szabolcs
  • 2,685

1 Answers1

5

You need to use \protect (because \Triangle is fragile) or create a robust command.

\documentclass{article}
\usepackage{graphicx}

\newcommand{\Triangle}{\raisebox{-0.3em}{\includegraphics[height=1.25em]{triangle}}}

\begin{document}

% this use of \Triangle below works fine:
\Triangle

\begin{figure}[ht]
%  \includegraphics{...}
   \caption{example caption \protect\Triangle .}
\end{figure}

\end{document}

With a robust command:

\documentclass{article}
\usepackage{graphicx}

\DeclareRobustCommand{\Triangle}{\raisebox{-0.3em}{\includegraphics[height=1.25em]{triangle}}}

\begin{document}

% this use of \Triangle below works fine:
\Triangle

\begin{figure}[ht]
%  \includegraphics{...}
   \caption{example caption \Triangle .}
\end{figure}

\end{document}
masu
  • 6,571