[Tex/LaTex] A class with default language settings

class-optionslanguages

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.)

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.

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 the polyglossia manual:

polyglossia no longer supports loading language definition files as package options!

It also doesn't accept ngerman as a language name. It must instead be german with the option spelling=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 wants polyglossia instead of babel.

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 or polyglossia 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 recognise ngerman as an option.

Therefore, it seems that the only practical solution to your question is to just concentrate on getting babel or polyglossia 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 for babel or polyglossia and pick up the language lists from them.

The simplest method that I can think of to do this is:

\ProvidesClass{mytest}

\LoadClass{article}

\RequirePackage{tracklang}
\RequirePackage{afterpackage}
\RequirePackage{ifxetex}

\ifxetex
 \newcommand{\@mytest@add@dialect}[1]{%
   \AfterPackage{polyglossia}{\setotherlanguage{\TrackedLanguageFromDialect{#1}}}%
 }
\else
 \newcommand{\@mytest@add@dialect}[1]{%
   \PassOptionsToPackage{#1}{babel}%
 }
\fi

\AnyTrackedLanguages
{
  \ForEachTrackedDialect{\this@dialect}{%
    \expandafter\@mytest@add@dialect\expandafter{\this@dialect}}

}
{
  \ifxetex
    \AfterPackage{polyglossia}{\setmainlanguage[spelling=new]{german}}
  \else
    \PassOptionsToPackage{ngerman}{babel}
  \fi
}

\ifxetex
 \RequirePackage{polyglossia}
\else
 \RequirePackage{babel}
\fi

\endinput

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.