I have written the following two \newcommand
for inserting figures with or without a caption, respectively.
% for fig with caption: #1: width/size; #2: fig file; #3: fig caption
\newcommand{\figwithcaption}[3]{
\begin{figure}[htp]
\centering
\includegraphics[#1]{#2}
\caption{#3}
\end{figure}
}
% for fig without caption: #1: width/size; #2: fig file
\newcommand{\fignocaption}[2]{
\begin{figure}[htp]
\centering
\includegraphics[#1]{#2}
\end{figure}
}
How to unify them into one \newcommand
called, say, \fig
which differentiates the two cases above according to the actual number of arguments it receives?
\fig{width = 0.50\textwidth}{fig/duck.pdf} % for fig without caption
\fig{width = 0.50\textwidth}{fig/duck.pdf}{A duck.} % for fig with caption
xparse
package (it uses a different syntax), it can do optional arguments in{}
's (I think it is theg
specifier), then you can test of#3
has a value, and if it has add a caption. – daleif Mar 16 '17 at 13:38\NewDocumentCommand\fig{mmo}{..\includegraphics[{#1}]{#2}..\IfValueT{#3}{\caption{#3}}..}
. That way it still has the usual LaTeX way for optional arguments\fig{..}{..}[optional caption]
. – Manuel Mar 16 '17 at 13:57