13

I'm currently using \PassOptionsToClass, as suggested in Conditional Compiling and \documentclass to compile a document in several languages. However, if I use language-options in \PassOptionsToClass, they will not be passed to babel.

MWEs:

\documentclass[english]{article}

\usepackage{babel}

\begin{document}
\end{document}

compiles (without complaints) an empty document. But

\PassOptionsToClass{english}{article}
\documentclass{article}

\usepackage{babel}

\begin{document}
\end{document}

yields

! Package babel Error: You haven't specified a language option.

since babel does not get the english option.

So here is my actual question: Is there any (simple) possibility to add a global option (which is passed to subsequent packages) before the actual \documentclass?

gerw
  • 606

1 Answers1

19

The mechanism used by \PassOptionsToClass (or indeed \PassOptionsToPackage) targets just that file: it's therefore equivalent to

\LoadClass[<options>]{<name>}

and very similar to

\RequirePackage[<options>]{<name>}
\usepackage[<options>]{<name>}

In contrast, the options given as part of the \documentclass line are globally available to all packages and classes loaded (yes, you can have only one class using \documentclass but that can load a series of ones to build on). As such, the global options are stored in another place. There's no pre-defined mechanism to access this 'publicly' other than via the optional argument to \documentclass. Of course, one can access it using the internal name

\makeatletter
\def\@classoptionslist{english}
\makeatother
\documentclass{article}

\usepackage{babel}

\begin{document}
\end{document}

I'm not really sure why you would choose to do this: a class shouldn't really be messing about with this type of thing.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 1
    Thank you for your answer. In my understanding, we have three types of options: package options, class options and global options (passed to all classes and packages). In the standard way (\documentclass[]) one can set global options and with \PassOptionsToClass we can set class options. In a standard document however, one does not distinguish between class and global options. – gerw Jan 16 '15 at 07:03
  • It’s 2023. Have there been any developments towards a command like \PassGlobalOptions or something similar which can pass options to the class and all packages? – Gaussler Oct 31 '23 at 08:55
  • @Gaussler Those are the options the user gives to \documentclass – Joseph Wright Oct 31 '23 at 08:57
  • Yes, I understand, but it would allow for a much cleaner way to make conditional global options. I would like to be able to do stuff like \ifdanish \PassGlobalOptions{danish} \else \PassGlobalOptions{english} \fi. As discussed in the chat yesterday, I can work around this via solutions like \ifdanish \def\mylanguageoptions{danish}\else\def\mylanguageoptions{english}\fi \expandafter\documentclass\expandafter[\mylanguageoption,⟨other options⟩]{article}, but this doesn’t look pretty. – Gaussler Oct 31 '23 at 09:01
  • @Gaussler The entire point is that global options are things the user sets – Joseph Wright Oct 31 '23 at 09:44
  • @JosephWright Yes, I get that, and that’s what I’m trying to do. I define a boolean \ifdanish at the top of my preamble and execute different options in my document, depending on whether I set it to true or false. – Gaussler Oct 31 '23 at 09:47