2

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.

  • 3
    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
2

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

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.