Another question gave me hope that there is an elegant way (without using additional packages) to refer to a comma separated argument in \newcommand
, maybe something like (just a guess, not tested):
\newcommand*\myfigure[3]{
\begin{figure}
\includegraphics[width=#3]{#1}
\caption[\getfirst#2]{\getsecond#2}
\end{figure}
\relax
}
\def\getfirst#1,#2\relax{{#1}}
\def\getsecond#1,#2\relax{{#2}}
that should allow to use a command such as:
\myfigure{imagefile}{short caption,long caption}{0.8\textwidth}
Unfortunately commas are often present in captions, so I would like to know if there is a way to obtain the desired result using another delimiter than comma.
Altough there are many similar question around, I did not find an elegant solution to my need.
UPDATE
Being a newbie, I discovered just now that I can use a custom delimiter in \def
(any character rarely found in captions, such as §
), so to avoid altogheter the comma issue. The guessed code does not work yet though, it seems that the second \def
breaks it, with the error Argument of \getsfirst has an extra }
. Does anybody know how to fix the code?
FINAL UPDATE
No wonder egreg knew how to fix the code (thanks!) which I report here (§
chosen as delimiter), should it be useful to somebody (even if egreg's own answer, using another approach, should fit better most of similar needs):
\def\getfirst#1§#2\relax{#1}
\def\getsecond#1§#2\relax{#2}
\newcommand\myfigure[3]{
\begin{figure}
\includegraphics[width=#3]{#1}
\caption[\getfirst#2\relax]{\getsecond#2\relax}
\label{fig:#1}
\end{figure}
\relax
}
\myfigure
over afigure
environment?{}
so it should be{short caption}{long caption}
or use named (key-val) argument as in\includegraphics
so{shortc=short caption,longc=long caption}
That way the short caption can be made optional. Also if you do define a command, don't forget%
at the ends of lines!!