3

I would like to write a class that loads the package babel with a default language but that this language could be overridden by a language specified in the class options. MWE:

\RequirePackage{filecontents}
\begin{filecontents}{test.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{test}[2015/09/22 v0 testclass]

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass{article}

\RequirePackage{babel}
%% or
%% \RequirePackage[english]{babel}

\end{filecontents}

\documentclass[a4paper, english]{test}
%% or 
%% \documentclass{test}

\begin{document}
\Huge We are typing in \languagename. 
\end{document}

As written, babel is loaded (as expected) with english. But if I omit english in the option, I have an error (logically) from babel.

The problem is that if I use \RequirePackage[english]{babel} in the class I can't override it --- for example:

\RequirePackage{filecontents}
\begin{filecontents}{test.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{test}[2015/09/22 v0 testclass]

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass{article}
\RequirePackage[english]{babel}
\end{filecontents}

\documentclass[a4paper, spanish]{test}
\begin{document}
\Huge We are typing in \languagename. 
\end{document}

...is still printing out "english".

Is there a general way to solve this generically, or I have the only option to use \DeclareOption each language that I am interested in and create a big \if loading babel in different ways?

To explain better --- I know how to provide with \DeclareOption a way to explicitily set babel defaults, like in jon's answer. But suppose I want that the class' users could do:

\documentclass[a4paper, whatever]{test}  %% babel default to english

or

\documentclass[a4paper, whatever, turkish, serbian, italian]{test}
%% ...and now babel is called with [turkish, serbian, italian] options 

The way I have it now is requiring the package babel with no option and letting it fail if the user do not specify at least a language on the class options... and documenting it. But it would be nice to have a default...

2 Answers 2

3

I'm not sure if this is what you are after, but what about something like this:

\RequirePackage{filecontents}
\begin{filecontents}{langtest.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{langtest}[2015/09/22 v0 testclass]

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\DeclareOption{spanish}{\PassOptionsToPackage{english,main=spanish}{babel}}
\DeclareOption{english}{\PassOptionsToPackage{english}{babel}}
\ExecuteOptions{english} % set default value as "english"


\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass{article}
\RequirePackage{babel}

\end{filecontents}

\documentclass[a4paper, ngerman, spanish]{langtest}
\begin{document}
\Huge We are typing in \languagename.
\end{document}

In my emacs output buffer, I see:

(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size10.clo))                 
(/usr/local/texlive/2015/texmf-dist/tex/generic/babel/babel.sty                 
(/usr/local/texlive/2015/texmf-dist/tex/generic/babel-german/ngerman.ldf        
(/usr/local/texlive/2015/texmf-dist/tex/generic/babel-german/ngermanb.ldf       
(/usr/local/texlive/2015/texmf-dist/tex/generic/babel/babel.def)))              
(/usr/local/texlive/2015/texmf-dist/tex/generic/babel-english/english.ldf)      
(/usr/local/texlive/2015/texmf-dist/tex/generic/babel-english/english.ldf)      
(/usr/local/texlive/2015/texmf-dist/tex/generic/babel-spanish/spanish.ldf)      
(/usr/local/texlive/2015/texmf-dist/tex/generic/babel-spanish/spanish.ldf)))
5
  • In fact, while we're creating shortcuts and so on, I might be inclined to name my options span and eng, and then use them in the class options....
    – jon
    Sep 23, 2015 at 3:38
  • Yes, this is what I mentioned at the end of my question --- thanks anyway for a clean way to do it. The problem is that I should do that for each and everyone of the languages I expect the class users can choose to use, and every combination of... I'll try to explain better in the questoon.
    – Rmano
    Sep 23, 2015 at 7:18
  • Rmano -- You want it to pick english if nothing is said, or only the languages given in the class options? Why not create an option called overridelang and explain in the docs that if you want to override the english setting, you must use overridelang and specify the languages the user wants to use? I.e., \documentclass[overridelangs, turkish, greek, spanish]{myclass}. Then you simply need one if-statement: either babel+english or babel+nothing.
    – jon
    Sep 23, 2015 at 20:36
  • yes, this is a good idea. Not so "clean" as I'd like, but better than letting babel bailing out in error. It's a pity that a try... catch thing is not possible in TeX!
    – Rmano
    Sep 23, 2015 at 20:40
  • Rmano -- Yeah, not very clean, I know. babel is a hard thing to wrap, and TeX error handling is pretty rigid....
    – jon
    Sep 23, 2015 at 20:43
0

I landed here and the solution for my problem was this:

The last language specified in the RequirePackage command for babel is the default. With this command it will load spanish and english and have english as the default:

\RequirePackage[spanish,english]{babel}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .