# Using Babel inside my own package produces option-clash error

I want to write my own package which provides an \affidavit command which produces some standard text in two languages (ngerman and english).

Minimal working example of this package:

\def\fileversion{0.1}
\def\filedate{2014/12/10}
\NeedsTeXFormat{LaTeX2e}
\RequirePackage[ngerman,english]{babel}
\ProvidesPackage{myaffidavit}
[\filedate\space\fileversion\space
My affidavit package]

\newcommand{\affidavit}{
\begin{otherlanguage}{ngerman}
This text will be in german stating some standard information (juristic)...
It should use correct hyphenation for german texts.
\end{otherlanguage}
\begin{otherlanguage}{english}
This text will be in english stating the same information as the german one above...
Of course this should automatically use correct hyphenation for english texts.
\end{otherlanguage}
}


As I don't control how my users are working with my package some user could try to use the following minimal working example of a latex document:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{myaffidavit}

\begin{document}
Hallo Welt!
\affidavit
\end{document}


Which results in this error: LaTeX Error: Option clash for package babel

# My question is:

How can I write a package which outputs text in different languages (using correct hyphenation) without restricting the user of my package to the languages loaded in my package.

And of course, if my user loads babel and my package the other way round I would not be able to use ngerman or english in my package, if the user didn't specify this languages in his latex document, too. (And there would be an option clash if my package tried to load additional languages after babel was already loaded by the user in the main document)

To be more precise: The point is, when I specify the languages my package is going to use inside my package using the package options for babel like this \RequirePackage[ngerman,english]{babel}, my package is most likely producing an option clash when the user of my package loads the babel package as well.

But: If the user of my package specifies the language he wants to use as a class option (as suggested in the comments), then there would still be an "option clash" (sort of), because the first load of babel inside the document still specifies which language definitions are being loaded (the ones specified as class options). And using the \myaffidavit command then produces errors, because it tries to use languages which are not defined/loaded.

I am writing a package and I don't want to force the user to specify the languages, used inside my package, as well when he is loading babel. I want it to be transparent. Say my user wants to write in italian, so he is loading babel with the italian language option. If he specifies it as an option when loading babel with \usepackage[italian]{babel} there is an option clash. When he specifies italian as a class option, there is no option clash, but the default language won't be italian anymore, when he is loading my affidavit package as well. That's not transparent! And I don't want my package to be that intrusive.

• Load ngerman in your document as a class option (as should be, Ithink, since version 3.9). The option clash comes from the fact that you load babel twice, with different options. Or don't load babel at all. – Bernard Dec 10 '14 at 20:29
• Welcome to TeX.SX! – cfr Dec 10 '14 at 22:08
• @Bernard Well, I want to have the correct hyphenation for the text, my package will output when using the \affidavit command in my document. I updated my question to make more clear what I want. – Thilo Dec 13 '14 at 0:17
• A package shouldn't make predictions about what languages will be used if there is any expectation that users will be using different languages. To do so is intrusive. If you want to ensure proper hyphenation of those words, provide explicit hyphenation break-points: it is not language-specific. Or: (on the intrusive side) you could make your package issue an error if the package babel isn't loaded, and state in the error message that this package unfortunately requires (1) ngerman and english or (2) a redefinition of \affadavit (which could be implemented as a package option). – jon Dec 13 '14 at 2:37
• To be honest, i agree with the others. But if you want to keep this track, just put it in the documentation of your package. ... Or load babel at the end of the preamble (which could conflict with hyperref). You could test if hyperref is loaded, but what about glossaries? You cannot get every possible instance, you aren't psychic. And after all, it is not your responsibility to protect the user from making mistakes. – Johannes_B Dec 13 '14 at 12:41

Here's something you can play with:

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[paperwidth=9cm, paperheight=10cm, showframe]{geometry}
\parindent 0pt%
%\usepackage[american, british]{babel}
%\usepackage{datetime}

\def\germtoday{% Example format: Date Month Year
\number\day\space
\ifcase\month\or
Januar%
\or Februar%
\or März%
\or April%
\or Mai%
\or Juni%
\or Juli%
\or August%
\or September%
\or Oktober%
\or November%
\or Dezember%
\fi
\space\number\year}

\newcommand\germanaffidavit{
\begingroup
\language=31%
The language is \the\language. {\germtoday}

%  \showhyphens{% Useful to test how words are being hyphenated
This text will be in Language stating the same information as the
Language one above.  Of course this should automatically use
correct hyphenation for Language texts.
%  }
\endgroup}

\newcommand\englishaffidavit{%
\begingroup
\language=0
The language is \the\language. \today

%  \showhyphens{%
This text will be in Language stating the same information as the
Language one above.  Of course this should automatically use
correct hyphenation for Language texts.
% }
\endgroup}

\newcommand\affidavit{\germanaffidavit\newpage\englishaffidavit}

\begin{document}
The language is \the\language

This text will be in Language stating the same information as the
Language one above.  Of course this should automatically use
correct hyphenation for Language texts.

\newpage
\affidavit
\newpage
The language is \the\language

This text will be in Language stating the same information as the
Language one above.  Of course this should automatically use
correct hyphenation for Language texts.

\end{document}


Remarks

• When switching babel off, compile twice to get rid of the error message.

• I'd be inclined to use a custom date command to avoid dependency on other packages for getting it 'right'; what I suggested here is meant to be illustrative of the possibilities, not definitive or even standards-compliant.

• Uncomment the \showhyphens command to verify that TeX is hyphenating things correctly (note that the output gets printed in the .log)

• Well, that's actually pretty cool and exactly what I wanted to do. Thanks a lot!! – Thilo Jan 19 '15 at 1:05
• @Thilo -- Glad it works for you! – jon Jan 19 '15 at 1:13

I stumbled on this question recently because I had the exact same problem, but the solution given here was not satisfactory for me, so I will try to explain the solution I found with the hope it will be useful to some other people looking for the same thing.

The point is that we want to specialize the babel translation for one language but not forbid to load all the other languages that we did not specialized. This could be done if and only if you redefine the translation after the babel package is loaded and the language(s) have been selected. But, the problem is that you are redefining it from a .cls or a .sty file. So, I used the \AtBeginDocument{} macro combined with a test on the presence (or not) of the babel package. I found this trick in the akletter class. Here is the final code (redefining the name of the table of contents section for french translation):

\AtBeginDocument{%
%%% French
\def\frenchcontentsname{Sommaire}%
}
}{}
}


Note also that I did not use \captionsfrench but \extrasfrench to work around the language selector if one is chosen and the document is multilingual.

Well, I hope this help! Any comments are welcome if something could be improved in this code.

If you load babel with particular options, anyone who wants to pass babel different options will need to call a commmand like

\PassOptionsToPackage{italian}{babel}


before calling your package. You can put this in the manual and then it won’t be your fault when they don’t read it.

Alternatively, you can require the package without particular options, and add your languages with \babelprovide. This is not the recommended way to load German or English, but it ought to work, and it will avoid an option clash if the user loads babel before your package.

If you need your package to support loading babel either before or after it, you would need to defer loading babel and all commands that depend on it until at least \AtEndPreamble, then do all your configuration without package options.