%%%%%%%%%%%%%%%%%%%%%%%cut here%%%%%%%%%%%%%%%%%%%%%%%
%% wright.sty--example of how to change LaTeX's \newcommand (and \renewcommand)
%%   to define a command with an optional argument.
%% Usage:
%%  \newcommand and \renewcommand are backward compatible.
%%  To define a command with an optional argument, these commands will take 
%%  a second optional argument:
%%
%%  \newcommand\kilroy[1][\blech]{\foo#1\bar#2\baz}%
%%
%%  will define \kilroy to have an optional argument with default value of
%%  \blech and expansion \foo#1\bar#2\baz, where the optional argument is #1.
%%  NB: the optional argument is the first parameter. You must bear this in
%%  mind when using the \newcommand macro.
%%  NB: If you wish to define a command that takes an optional argument, but no
%%    required arguments, usage is, eg:
%%
%%  \newcommand\kilroy[0][\blech]{\foo#1\bar#2\baz}%
%%
%% Implementation:
%%  modified commands:
%%   \newcommand
%%   \renewcommand
%%
%%  new commands:
%%   \@newcommand
%%   \@@newcommand
%%   \@@@newcommand
%%   \@ifredefinable
%%   \@@ifredefinable
%%   \@commanddef
%%   \@ldef
%%   \@hashchar
%%   \@macparm
%%
%%  commands that change their meaning when these macros are executed:
%%   \@@ifredefinable
%%   \@macparm
%%   
\def\newcommand{\let\@@ifdefinable\@ifdefinable\@newcommand}%
\def\renewcommand{\let\@@ifdefinable\@ifredefinable\@newcommand}%

\def\@newcommand#1{\@ifnextchar[{\@@newcommand#1}{\@@newcommand#1[\z@]}}%
\def\@@newcommand#1[#2]{\@ifnextchar[{\@@@newcommand#1[#2]}{\@@@newcommand#1[#2][]}}%
\long\def\@@@newcommand#1[#2][#3]#4{\@@ifdefinable#1{\@commanddef#1[#2][#3]{#4}}}%

\def\@ifredefinable#1#2{%
 \edef\@tempa{\expandafter\@cdr\string#1\@nil}%
 \@ifundefined{\@tempa}{\@latexerr{\string#1\space undefined}\@ehc}{}%
 \let#1\undefined
 \@ifdefinable#1{#2}%
 }%

\long\def\@commanddef#1[#2][#3]#4{%
 \let#1\relax\@tempcnta#2\def\@tempb{#3}\let\@macparm\relax
 \ifx\@tempb\@empty
  \@tempcntb\@ne
  \edef\@tempa{#1}%
  \else
  \@tempcntb\tw@
  \edef\@tempa{\csname\string#1\endcsname[\@macparm1]}%
  \def#1{%
   \@ifnextchar[{\csname\string#1\endcsname}{\csname\string#1\endcsname[#3]}%
   }%
  \fi
 \@whilenum\@tempcnta>\z@\do{%
  \edef\@tempa{\@tempa\@macparm\the\@tempcntb}%
  \advance\@tempcntb\@ne\advance\@tempcnta\m@ne
  }%
 \let\@macparm\@hashchar\expandafter\@ldef\@tempa{#4}%
 }%
\catcode`\?=12\relax
\def\@ldef{\long\def}%
\let\@hashchar#% the macro-parameter catcode
