23

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]{myclass} 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?

  • \PassOptionsToPackage{ngerman}{babel}? – egreg Nov 27 '13 at 17:06
  • @egreg: No this doesn't work, then \documentclass[english]{mytest} doesn't gives english but german words as the document options are processed before the package options. – Ulrike Fischer Nov 27 '13 at 17:14
  • 3
    @egreg: classes are not language agnostic: you get "table of contents" as default and english hyphenation. I need a class with german defaults. – Ulrike Fischer Nov 27 '13 at 18:31
  • 5
    If you define all the \*name macros and \today in your class file, ending with \language\l@ngerman, you will have ngerman defaults in exactly the same way that article.cls has usenglish defaults. – Dan Nov 28 '13 at 4:40
  • 1
    @UlrikeFischer A possible trouble with your hack, coming more form babel, is if the passed class option is english,ngerman: in such a case, "Last declared language option is 'ngerman', but the last processed one was 'english'" as a babel's warning says. – Denis Bitouzé Apr 8 '14 at 20:10
4

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

  • As I wrote my (rather special) class already loads babel, so polyglossia is not an issue (and even with xelatex I would prefer babel if the needed language files are adapted to xelatex). My main problem was not the various possible option names, but how to put such an option at the very begin of the option list. And imho the answer is that there is currently no better solution than my code (which works exactly as I want it). Beside this there is imho a need for some language interface on the kernel level. – Ulrike Fischer Dec 5 '15 at 22:32
  • @UlrikeFischer I agree that there is definitely a need for some language interface in the kernel. Perhaps you should just post your comment as an answer given that no one else can come up with a better solution. – Nicola Talbot Dec 5 '15 at 22:48
3

To get the question from the unanswered list I will answer it myself:

Imho there is currently no better solution than the one proposed in the question.

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.