1

I am trying to use the package options to define a new command type option which invokes the default argument when not provided with an argument. I have tried the following MWE which defines the new option type myopttype. It works for the example in Case I but not for the one in Case II where xxx should be typeset in italics.

I am not very familiar with the inner workings of the package options. Could someone come to my aid?

\documentclass{article}
\usepackage{options}

\makeatletter

\options{
   /handlers/new myopttype/.new handler = [\itshape]\optionsalso{%
      #1/.new cmd = \option@set{#1}{##1}\optionvalue,
      #1/.type    = mypottype,
      #1/.initial = {#2},
   },
}

\options{
   /mycommand/.new family,
   /mycommand/myoption/.new myopttype
}

\newcommand{\mycommand}[1][]{%
   \options{/mycommand,#1}%
}

\makeatother

\begin{document}

{\bfseries Testing the option:}
\vspace{1.5mm}

{\bfseries Case I:} Using \texttt{\textbackslash mycommand[myoption=\textbackslash bfseries]}

aaa {\mycommand[myoption=\bfseries] xxx}.
\vspace{1.5mm}

{\bfseries Case II:} Using \texttt{\textbackslash mycommand}

aaa {\mycommand xxx}.

\end{document}
2
  • 1
    It works if you set the key by default: \newcommand{\mycommand}[1][myoption=\itshape]{...}
    – egreg
    Commented Apr 2, 2018 at 12:48
  • Thank you very much @egreg. Your solution works. Your suggestion is one approach that I had not considered. I had imagined that the default could be set at the point where option type is defined. Thanks.
    – user24098
    Commented Apr 2, 2018 at 12:57

1 Answer 1

2

I know nothing about the options package, but it appears that you have to set a value for the key in order to state a default value. Defining

\newcommand{\mycommand}[1][myoption=\itshape]{%
   \options{/mycommand,#1}%
}

seems to solve the issue, but the whole thing seems quite complicated. If you add something to the initial part, you can simplify the definition. With

\options{
   /handlers/new myopttype/.new handler = [\itshape]\optionsalso{%
      #1/.new cmd = \option@set{#1}{##1}\optionvalue,
      #1/.type    = mypottype,
      #1/.initial = {#2},
      #1/.default = \itshape,
   },
}

the definition can be more simply

\newcommand{\mycommand}[1][myoption]{%
   \options{/mycommand,#1}%
}

which is much better because it avoids code duplication.

2
  • Thanks once more @egreg. The (second) solution that you have posted is the kind of solution that I have been seeking. I had tried it out earlier. However, it did not work for me because of the way I had defined \mycommand. I had not provided the argument myoption in the definition of the command. It is a matter of great curiosity for me as to why the option has to be provided the way you have. That said, the second solution is the type I have been seeking. Thank you very much.
    – user24098
    Commented Apr 2, 2018 at 16:34
  • I know that this is a tall order: Is there a way of defining the option type such that it ensures that the argument provided is nothing other than a LaTeX font command?
    – user24098
    Commented Apr 2, 2018 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .