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}
2
  • kvoptions allow to use DeclareOption. Sep 30, 2011 at 11:08
  • But how to use kvoptions' DeclareOption then to pass the languages keyval options to babel?
    – Sebastian
    Sep 30, 2011 at 11:17

2 Answers 2

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}
1
  • 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 3, 2011 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@}
5
  • I get errors: "missing \endscname inserted \ProcessOptions*"
    – Sebastian
    Oct 1, 2011 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 1, 2011 at 19:54
  • @mhp: please compare the \DeclareStringOption in the mwe and in your code. Oct 1, 2011 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 2, 2011 at 17:39
  • @mhp: You are right. Oct 2, 2011 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.