[Tex/LaTex] the proper way to implement language options in a multilanguage package

babelpackage-options

I'm writing a multilingual LaTeX2e package to be used with pdflatex containing several macro and environment definitions. The main language of the document–a thesis–is English, Finnish or Swedish. One page of the document contains text in one secondary language, again English, Finnish or Swedish. My problem is in some ways similar to the one here.

I have implemented the choice of the main language as an option of my package, which I then pass on to the babel package using \PassOptionsToPackage. I then process the package options with english as the default. However, specifying a language other than english globally as say \documentclass[finnish]{article} doesn't seem to pass on the chosen language to babel while \usepackage[finnish]{mypackage} does. This is evident from the caption in tables and figures, which are in English and not Finnish, as well as the title of the list of references, which is also in English in the first case.

After digging around I found a solution to my problem (a more elegant one to using flags and then loading babel with the appropriate options) here.

My question is why doesn't \PassOptionsToPackage pass on the option to babel when it is specified in \documentclass? The option isn't passed on when finnish is specified to both \documentclass and \usepackage{mypackage} either. Why? A minimum working package code for mypackage.sty is:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}[2013/09/26 Version 0.01]

\DeclareOption{english}{%
  \PassOptionsToPackage{english}{babel}
%  \AtEndOfClass{\main@language{english}}
}
\DeclareOption{finnish}{%
  \PassOptionsToPackage{finnish}{babel}%
%  \AtEndOfClass{\main@language{finnish}}
}
\DeclareOption{swedish}{%
  \PassOptionsToPackage{swedish}{babel}%
  %\AtEndOfClass{\main@language{swedish}
}
\ExecuteOptions{english}% Default package options
\ProcessOptions \relax

\RequirePackage{babel}%
\RequirePackage[latin1]{inputenc}%
\RequirePackage[T1]{fontenc}%

\endinput

A not-so-minimal LaTeX file that shows the caption and reference list heading in the wrong language follows:

% This works when \AtEndOfClass{\main@language{...} is uncommented
% but not with \PassOptionsToPackage{finnish}{babel}.
\documentclass[finnish]{article}
\usepackage{mypackage}

% This works with \PassOptionsToPackage{finnish}{babel} uncommented.
% This causes an error with \AtEndOfClass{\main@language{
%\documentclass{article}
%\usepackage[finnish]{mypackage}

\begin{document}

\section{Johdanto}

Tämän tekstin lähteenä oleva tiedosto on opinnäytteen pohja...

\begin{table}[htb]
\caption{\label{taulukko1} Taulukon kirjaintyypin...}
\vspace{1em}
\centering
\begin{tabular}{|c|l|r|}
\hline \textbf{A} & 1 & $e^{j \omega t}$ \\ \hline
\end{tabular}
\end{table}

\subsection*{Lähdeluettelo} 

Viite \cite{bcs} on esimerkki artikkelin esittämisestä lähdeluettelossa.

\begin{thebibliography}{99}

\bibitem{bcs} Bardeen,\ J., Cooper,\ L.\ N. ja Schrieffer,\ J.\ R.
  Theory of Superconductivity. \textit{Physical Review,} 1957, vol.      108, nro~5, s.\ 1175--1204.

\end{thebibliography}

\end{document}

Best Answer

My question is why doesn't \PassOptionsToPackage pass on the option to babel when it is specified in \documentclass?

The thinking was that there was no need to as it was a global option so the included package already sees it. Of course babel is a bit special as it matters there about the order or the options, the last one being default, that isn't really handled in the general code.

Related Question