Nicolas Vaughan | 16 Apr 18:49

Help with parsing bracketed parameters (non WinEdt related question)

Hi,
This is a non-WinEdt related question...  but elsewhere I've found no help with it!

I'm writing a class style for LaTeX. But I want my commands to parse optional commands enclosed in [ ].

I know I must use something like \ <at> ifnextchar[ , but I haven't been able to understand the examples I've looked up in the net. I found out, though, that the syntax of such a command is:

\ <at> ifnextchar<char>{truecase}{falsecase}

My problem is how to incorporate it in the command.

Note that I don't want to parse optional parameters with the LaTeX 2e command-definition syntax (\newcommand{\name}[1][1]...), since this doesn't allow me to use bracketed optionals.

Any tips?

Thanks in advance.

Nicolas Vaughan

Joseph Wright | 16 Apr 20:49

Re: Help with parsing bracketed parameters (non WinEdt related question)

Nicolas Vaughan wrote:
> Hi,
> This is a non-WinEdt related question...  but elsewhere I've found no help
> with it!
> 
> I'm writing a class style for LaTeX. But I want my commands to parse
> optional commands enclosed in [ ].
> 
> I know I must use something like \@ifnextchar[ , but I haven't been able to
> understand the examples I've looked up in the net. I found out, though, that
> the syntax of such a command is:
> 
> \@ifnextchar<char>{truecase}{falsecase}
> 
> My problem is how to incorporate it in the command.
> 
> Note that I don't want to parse optional parameters with the LaTeX 2e
> command-definition syntax (\newcommand{\name}[1][1]...), since this doesn't
> allow me to use bracketed optionals.
> 
> Any tips?
> 
> Thanks in advance.
> 
> Nicolas Vaughan
> 

I'm slightly lost as to why you can't do:

\newcommand{\foo}[2][]{You always give #2, but might not give #1!}

However,

\makeatletter
\def\foo{%
  \@ifnextchar[%]
    {\@foo}
    {\@foo[]}}
\def\@foo[#1]{%
  Do stuff with #1!%
}
\makeatother

What happens here is as follows.  The test occurs, and if it finds a
"[", it calls \@foo without adding anything to the input.  On the other
hand, if there is no "[", an empty optional argument [] is given.  This
is needed so that something matches the [] in the definition of the
argument to \@foo.  If \foo is supposes to take a mandatory argument as
well, the definition would read:

\def\@foo[#1]#2{%
  Do stuff with #1 and #2%
}

Joseph Wright

Nicolas Vaughan | 16 Apr 23:18

Re: Help with parsing bracketed parameters (non WinEdt related question)

Hi Joseph,
Thanks a lot! It worked quite fine!
Nicolas

On Wed, Apr 16, 2008 at 1:49 PM, Joseph Wright <joseph.wright <at> morningstar2.co.uk> wrote:
Nicolas Vaughan wrote:
> Hi,
> This is a non-WinEdt related question...  but elsewhere I've found no help
> with it!
>
> I'm writing a class style for LaTeX. But I want my commands to parse
> optional commands enclosed in [ ].
>
> I know I must use something like \ <at> ifnextchar[ , but I haven't been able to
> understand the examples I've looked up in the net. I found out, though, that
> the syntax of such a command is:
>
> \ <at> ifnextchar<char>{truecase}{falsecase}
>
> My problem is how to incorporate it in the command.
>
> Note that I don't want to parse optional parameters with the LaTeX 2e
> command-definition syntax (\newcommand{\name}[1][1]...), since this doesn't
> allow me to use bracketed optionals.
>
> Any tips?
>
> Thanks in advance.
>
> Nicolas Vaughan
>

I'm slightly lost as to why you can't do:

\newcommand{\foo}[2][]{You always give #2, but might not give #1!}

However,

\makeatletter
\def\foo{%
 \ <at> ifnextchar[%]
   {\ <at> foo}
   {\ <at> foo[]}}
\def\ <at> foo[#1]{%
 Do stuff with #1!%
}
\makeatother

What happens here is as follows.  The test occurs, and if it finds a
"[", it calls \ <at> foo without adding anything to the input.  On the other
hand, if there is no "[", an empty optional argument [] is given.  This
is needed so that something matches the [] in the definition of the
argument to \ <at> foo.  If \foo is supposes to take a mandatory argument as
well, the definition would read:

\def\ <at> foo[#1]#2{%
 Do stuff with #1 and #2%
}

Joseph Wright


Gmane