Don't. You lose in flexibility while not gaining much in input speed, because you always have to remember the order of the arguments.
Also, the option [H]
should be avoided, because it leads to several pagination problems, which the standard optional arguments to figure
try to overcome. Also center
is not the best and the \centering
declaration is best.
Let's see how you might do. You need
- an optional argument for the float placement
- an optional argument for the “short” caption
- a mandatory argument for the caption
- a mandatory argument for the label
- an optional argument for the figure parameters
- a mandatory argument for the image file.
Placing two consecutive optional arguments that deal with different aspects is bad; so we could insert the label in the second position. Thus we need
\NewDocumentCommand{\addfigure}{
O{htp} % the suggested default
m % the label
O{#4} % the short caption (defaults to the long one)
m % the caption
O{} % the options to \includegraphics
m % the file name
}{%
\begin{figure}[#1]
\centering
\caption[#3]{\label{#2}#4}
\includegraphics[#5]{#6}
\end{figure}
}
Let's see possible calls:
\addfigure{fig:A}[short desc]{long caption}[width=\columnwidth]{filename}
\addfigure{fig:B}[short desc]{long caption}{filename}
\addfigure[!htbp]{fig:C}{long caption}[width=6cm]{filename}
Compare the last with
\begin{figure}[!htpb]
\centering
\caption{long caption\label{fig:C}}
\includegraphics[width=6cm]{filename}
\end{figure}
and decide which is clearer.
figure
environment and I have unfortunately usedsubfigure
package. A few months ago I discovered the subcaption package. It is just better. Now i just usecaption
andsubcaption
for new documents. I update my oldnewcommand
s syntax and I'm still replacing my usage ofsubfigure
package in "on working" projects.\begin{center}...\end{center}
as that gives inconsistent spacing, that's why egreg's and my answers use\centering
instead.