[Tex/LaTex] LaTeX3: how to access package options

latex3package-options

In my package I intend to write in expl3 syntax, how can I get access to the list of options passed to it on the document level via \usepackage[<option list>]{my-l3-pkg}?

I know more or less how to define and set keys using the functions provided by the l3keys package. I don't know yet, which variable holds the contents of <option list> I could pass to \keys_set:...

Is there some replacement for the LaTeX2e functions \DeclareOption and \ProcessOptions?

Best Answer

First, bear in mind that what we are talking about is LaTeX3 syntax on top of LaTeX2e. That means that LaTeX2e conventions apply to some extent. In particular, LaTeX2e expands options and removes spaces. So you have to be a little wary in what you set up as load-time package options.

Now, as LaTeX2e is still actually doing the processing, the main macros remain: \DeclareOption and \ProcessOptions. However, if you want to create key-value options you are best off loading the support package l3keys2e. This enables you you process LaTeX2e package or class options using the key-value system provided by expl3. What you then need to do is a two-step process:

  1. Declare keys for your package using \keys_define:nn
  2. Process the package options using \ProcessKeysOptions

You might therefore have something like

\RequirePackage{l3keys2e}
\ProvidesExplPackage{mypkg}{2011/04/14}{0.0}{A demo}
\keys_define:nn { mypkg }
  {
    option-1 .code:n = some-code-here ,
    option-2 .code:n = some-other-code-here
  }
\ProcessKeysOptions { mypkg }
...

Any key which is declared before \ProcessKeysOptions is treated as a potential package option (this is true for other key-value approaches to package options).

Related Question