0

I am using my own icons in a document using

\newcommand{\icon}{\includegraphics[scale=0.2]{myicon.pdf}}

as described in this post.

Now I am trying to use this newcommand in a caption environment, but I get errors

! Argument of \@caption has an extra }.
! Paragraph ended before \@caption was complete.

Is there any way to add figures to the caption environment? or any suggested workaround to this?


Minimal example

\documentclass{article}
\usepackage{graphicx}
\newcommand{\icon}{\includegraphics[scale=0.2]{myicon.pdf}}

\begin{document}

This is an inline \icon % this part compiles good

\begin{figure}
\includegraphics[width=\linewidth]{myfigure}
\caption{This is an \icon in a caption environment.} % this part gives errors
\end{figure}

\end{document}
rsaavedra
  • 125

1 Answers1

2

In recent releases \includegraphics is robust so should not prematurely expand when writing the .toc file, but in any release you could define your command via

\DeclareRobustCommand{\myicon}{\includegraphics[scale=0.2]{myicon.pdf}}

then it will not expand in moving environments.

Unrelated you may prefer to use something like height=1.1ex rather than scale=0.2 then the icon will adjust to font size commands such as \large.

David Carlisle
  • 757,742