[Tex/LaTex] Using Babel inside the own package produces option-clash error

babellanguagesoption-clashpackage-optionspackage-writing

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.

Best Answer

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)