Text FormattingNew Command with multiple optional Arguments

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

New Command with multiple optional Arguments

Post by eleanor »

Hi, I would like to create my own command like lstset.

The command would have the syntax:

Code: Select all

\verbset[a=first_arg, b=second_arg, ..., n=last_arg]
Where I could enter some arguments, which are all optional - like lstset.

How can I do the above.

Thanks in advance
Last edited by eleanor on Thu Jul 21, 2011 9:10 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

New Command with multiple optional Arguments

Post by eleanor »

Yes I already found that. But I don't thing this is relevant, since I'm only having a command like this:

Code: Select all

\verbset[a=aaa,b=bbb]
So my command code has to recognize the 'a' and 'b' part == key, and assign it the 'aaa' and 'bbb' == value.

So I would rather appreciate an example.
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

New Command with multiple optional Arguments

Post by josephwright »

You will need to define the key-value argument list yourself. You do not say what should happen to 'a', 'b', etc., so I'm assuming they should be stored. For this, you need \define@key to define keys and \setkeys to process them

Code: Select all

\documentclass{article}
\usepackage{keyval}
\makeatletter
\newcommand*\verbseta{}
\newcommand*\verbsetb{}
\define@key{verbset}{a}{\renewcommand*\verbseta{#1}}
\define@key{verbset}{b}{\renewcommand*\verbsetb{#1}}
\makeatother
\newcommand*\verbset[1][]{%
  \setkeys{verbset}{#1}%
  % Your code here
}
Joseph Wright
eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

New Command with multiple optional Arguments

Post by eleanor »

Thank you, you've helped me. But I would still like to tweak it a little further. Currently I'm having the following code:

Code: Select all

\documentclass{article}
\usepackage{keyval}
\makeatletter
\newcommand*\verbseta{aaa}
\newcommand*\verbsetb{bbb}
\define@key{verbset}{a}{\renewcommand*\verbseta{#1}}
\define@key{verbset}{b}{\renewcommand*\verbsetb{#1}}
\makeatother
\newcommand*\verbset[1][]{%
  \setkeys{verbset}{#1}%
  % Your code here
}


\newenvironment{env}
{
  Arguments: a = {\verbseta} ; b = {\verbsetb}
}
{ 
  \renewcommand*\verbseta{aaa}
  \renewcommand*\verbsetb{bbb}
}


\begin{document}

This is a document.
  \verbset[a=ccc,b=ddd]
  \begin{env}\end{env}

  \begin{env}\end{env}

\end{document}

So I'm setting the default value for \verbseta and \verbsetb. At first I'm setting those two variables to 'ccc' and 'ddd' for the first environment. But then the values stay 'ccc' and 'ddd' for the second environment too. Why is that, if I'm explicitly renewing the commands to default in the newenvironment definition.

Basically what I would like, is exactly the same code, except that the two variables would only be set for the first environment - for the second, they would fall back to default.

I would like to reset the values for each environment.

How can I do that?
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

New Command with multiple optional Arguments

Post by josephwright »

You seem to want an optional argument to the environment itself

Code: Select all

    \documentclass{article}
    \usepackage{keyval}
    \makeatletter
    \newcommand*\verbseta{aaa}
    \newcommand*\verbsetb{bbb}
    \define@key{verbset}{a}{\renewcommand*\verbseta{#1}}
    \define@key{verbset}{b}{\renewcommand*\verbsetb{#1}}
    \makeatother
    \newenvironment{env}[1][]
    {
      \setkeys{verbset}{#1}%
      Arguments: a = {\verbseta} ; b = {\verbsetb}%
    }
    {}


    \begin{document}

    This is a document.
      \begin{env}[a=ccc,b=ddd]\end{env}

      \begin{env}\end{env}

    \end{document}
Joseph Wright
eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

Re: New Command with multiple optional Arguments

Post by eleanor »

Thank you. Works like a charm.
Post Reply