I want to write a class which loads babel and other language aware packages and pass to all of them the option ngerman
as default. But if another language is used in the options of the \documentclass
then this language should be used. The only way I found to get this behaviour is this code
\ProvidesClass{mytest}[2013/11/27]
\RequirePackage{etoolbox}
\preto\@classoptionslist{ngerman,}
\LoadClass{article}
\RequirePackage{babel}
\endinput
which then can be used like this and \documentclass[english]{mytest}
works as expected.
\documentclass{mytest}
\begin{document}
\bibname
\end{document}
But the \preto\@classoptionslist{ngerman,}
looks a bit like a hack. Is there really no other way to pass a default option in a .cls
to all (or some) packages?
Best Answer
(I know this question is old, and maybe you've found a solution, but it's still lingering on the unanswered list.)
Your assumption here is that all language-aware packages use the same option list as
babel
, which is unfortunately not the case. It may be that you have a specific list of language-aware packages that your class will load, where you can verify whether this is true, but this doesn't take into account any additional packages the user might want to load after they've loaded your class that may also require language support.The other problem is, what happens if the user wants to use XeLaTeX? In this case, they'll more likely want
polyglossia
, and here there's another problem. From thepolyglossia
manual:It also doesn't accept
ngerman
as a language name. It must instead begerman
with the optionspelling=new
.So even if you do find an easy way to add
ngerman
to the class option list, given that it doesn't contain any other language names, you still won't achieve your desired effect if a user of your class wantspolyglossia
instead ofbabel
.What of other language-aware packages? I've come across at least one class where the language is set using a key=value option in the form
language=enGB
. Given the increasing number of packages with a key=value option list, there may well be some packages that require the language set in this way as well. (I can't recall any off the top of my head, but I did consider it for one of my packages with a key=value option list, although in the end I decided it wasn't a good idea.)I think that most packages that provide multilingual support tend to check for
babel
orpolyglossia
so that they can integrate with their caption mechanism (especially given the complications it can cause if they don't in multilingual documents). However, for those that don't, there's no guarantee at all that they will recognisengerman
as an option.Therefore, it seems that the only practical solution to your question is to just concentrate on getting
babel
orpolyglossia
to set German as the default language if no language has been supplied by the user in the document class, and then assume that any other packages the user wants to load that has multilingual support will have the sense to check forbabel
orpolyglossia
and pick up the language lists from them.The simplest method that I can think of to do this is:
This way,
ngerman
is only automatically added if the user hasn't specified any language options in the document class, so it's not added if they don't want it.