10

How to pass a keyval string option given to my package to babel?

I load my package with

\usepackage[languages={ngerman,english}]{mylang}

In package mylang I want to handle this optional languages parameter and hand it over to babel. If the option is missing I want to define a default value.

My attempt:

\usepackage{kvoptions}
\DeclareStringOption{languages}[english,ngerman]

But how to go on from there? I think the default value is set correctly but how to hand it over to babel now? Do I need to run the default process option routine?

For a non keyval value I would use

\DeclareOption{languages}{\PassOptionsToPackage{\CurrentOption}{babel}}
\DeclareOption*{\OptionNotUsed}
Sebastian
  • 1,907

2 Answers2

8

It is not really an answer but I want to show a minimal working example in relation to mhp answer.

DeclareStringOption (important is the order)

\RequirePackage{filecontents}
\begin{filecontents}{mylang.sty}
\ProvidesPackage{mylang}
\RequirePackage{kvoptions}
 \SetupKeyvalOptions{%
   family=ML,
   prefix=ML@,
   }

\DeclareStringOption[english,ngerman]{languages}
\ProcessKeyvalOptions*
\PassOptionsToPackage{\ML@languages}{babel}
\RequirePackage{babel}



\end{filecontents}
\documentclass{article}
\usepackage[languages=frenchb]{mylang}
\usepackage{blindtext}
\begin{document}
\blindtext
\end{document}
Marco Daniel
  • 95,681
  • Works like a charm. Hint: my package name contained numbers and therefore I had to set the prefix myself since the macro \L10n@langiages didn't work with numbers. – Sebastian Oct 03 '11 at 13:02
3

The value of an option declared with \DeclareStringOption is stored in the macro \<package>@<option>. Hence, try

\ProvidesPackage{mylang}

...

\usepackage{kvoptions}

\DeclareStringOption{languages}[english,ngerman]

\ProcessKeyvalOptions*

\PassOptionsToPackage{\mylang@languages}{babel}

If you prefer a shorter prefix than mylang@ use \SetupKeyvalOptions before declaring options, e.g.:

\SetupKeyvalOptions{family=ML, prefix=ML@}
mhp
  • 8,692
  • I get errors: "missing \endscname inserted \ProcessOptions*" – Sebastian Oct 01 '11 at 10:31
  • @Sebastian: Look at the answer of Marco, he's built a minimal working example around my code. It should definitely work. – mhp Oct 01 '11 at 19:54
  • @mhp: please compare the \DeclareStringOption in the mwe and in your code. – Marco Daniel Oct 01 '11 at 21:23
  • @Marco: OK, I see. In contrast to Sebastian, you don't specify a default value, but an initial value for the languages option. In fact, this is a better approach since it allows the babel package to be loaded without options at all. But I don't think the error reported by Sebastian is related thereto. – mhp Oct 02 '11 at 17:39
  • @mhp: You are right. – Marco Daniel Oct 02 '11 at 18:59