# Defining a LaTeX macro with optional arguments as keywords inside square brackets

I want to define a macro to insert pictures in figures, called \centeredfigure, which I am hoping to use the same way as \includegraphics. In \includegraphics, all optional arguments are given in the form

\includegraphics[scale=, width=, height=, ...]{path_to_graphics}


What I want to define is is:

\centeredfigure[scale=, width=, height=, caption=, ref=, placing=]{path_to_graphics}


In contrast, the optional argument functionality LateX's \newcommand provides is like this:

\somecommanddefinedbynewcommand[value1][value2]{value3}


So \newcommand cannot be used to obtain what I want.

I have previously defined plain TeX macros before, but none with optional arguments of such kind. I am currently looking at the definition of \includegraphics defined in combinedgraphics.sty and am a bit baffled. I am trying to discover an underlying pattern. I would appreciate any help, like a minimal example that demonstrates a single optional argument.

The macro is going to be used in LaTeX, but is going to be defined plain TeX style.

Edit - the original macro using \newcommand

\newcommand{\centeredfigure}[4]{
\begin{figure}[htbp]
\begin{center}
\includegraphics[width=#1\textwidth]{#2}
\caption{#3}
\label{#4}
\end{center}
\end{figure}
}

-
do you want a plain tex macro or latex, your title and tag is plain but your examples use \includegraphics which is latex (but can be loaded into plain) –  David Carlisle Jul 5 '14 at 14:30
the key=val syntax as used in graphicx is defined in the keyval package which can be used with plain tex (although there are later packages such as xkeyva, l3keys and pgf with similar syntax) you can also load graphicx into plain, using the miniltx macros for plain tex. –  David Carlisle Jul 5 '14 at 14:34
It will be used in LaTeX, but the definition style is plain TeX, that is why I said plain TeX, sorry about the confusion. –  hos Jul 5 '14 at 14:36
why do you want a "definition style" like plain if you are not using plain? why not simply use \includegraphics (or add additional keys to \includegraphics as adjustbox does, your question is totally unclear. plain tex is a completely different format to latex. –  David Carlisle Jul 5 '14 at 14:38
The \includegraphics used in the macro will be embedded in a figure environment, also with a caption and reference, which will all be passed using optional arguments of the macro. –  hos Jul 5 '14 at 14:41

The [a=b] syntax used by graphicx package is defined by the keyval package which you can use to define similar commands. (xkeyval, pstricks, pgf, l3keys aslo define similar argument parsers, but it probably makes sense to use keyval if you want to fit in with graphix. See texdoc keyval for more details.