3

I like to define commands to denote multi-parameter (mathematical) functions, e.g., \myf{x}{C} expands to f(x;C). During writing, however, it is sometimes hard to remember if I should use \myf{x}{C} or \myf{C}{x}, so I would like to use something like keyword arguments from Python. In my example, this means I would like to write \myf{set=C,at=x} or \myf{at=x,set=C} and get f(x;C) as the result for both commands. For that, I am using pgfkeys; see below.

I am running into an error when pdflatexing the code below:

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{mathtools}
\usepackage{mathrsfs}
\pgfkeys{
  /myf/set/.estore in = \myfset,
  /myf/at/.estore in = \myfat,
}
\newcommand*{\myf}[1]{%
  \begingroup
  \pgfkeys{/myf/.cd, #1}
  f(\myfat;\myfset)
  \endgroup
}
\begin{document}
\(\myf{set=\mathscr{C},at=a}\)
\end{document}

I get an error

<inserted text> 
                \inaccessible 
l.16 \(\myf{set=\mathscr{C},at=a}
                                 \)
?

This error goes away if I replace \mathscr{C} with C or if I drop the package mathtools (which I need for other features).

Could anybody please help me fix this? Thanks!

2
  • 4
    Is there a reason you are using .estore in rather than just .sotre in? The former does expansion which may (and here does) go wrong with non-expandable input.
    – Joseph Wright
    Jan 20, 2015 at 10:08
  • @JosephWright: the only reason is my ignorance. Thank you so much. This solved my problem.
    – mk1
    Jan 20, 2015 at 11:19

1 Answer 1

3

Just for fun a solution using l3keys. Here, the keys set defaults to \mathscr{A} and at to c, so you can use \myf{} with an empty argument.

\documentclass{article}
\usepackage{xparse}
\usepackage{mathrsfs}
\ExplSyntaxOn

\keys_define:nn { mk1 }
 {
  set .tl_set:N  = \l_mk_set_tl   ,
  at  .tl_set:N  = \l_mk_at_tl    ,
  set .default:n = { \mathscr{A} },
  at  .default:n = { c }
 }

\NewDocumentCommand { \myf } { m }
 {
  \group_begin:
  \keys_set:nn { mk1 } { set,at,#1 }
  f( \tl_use:N \l_mk_at_tl ; \tl_use:N \l_mk_set_tl )
  \group_end:
 }
\ExplSyntaxOff
\begin{document}
\(\myf{set=\mathscr{C},at=a}\)
\(\myf{}\)
\end{document}

enter image description here

You must log in to answer this question.

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