[Tex/LaTex] Default options in custom memoir class

class-optionsdocumentclass-writingmemoir

I can't get default arguments to work with my custom class based on memoir. I want to be able to define some default options (a4paper,danish) but I want to be able to override them when I call \documentclass in the tex document. How can I resolve this?

PS: I deleted a similar earlier question because the reason for the problem was a trivial syntax error. This is not the case here, though.

testclass.cls

% identification
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{testclass}[2013/12/02 testclass]

% declaration of options
\DeclareOption{danish}{%
  \PassOptionsToPackage{danish}{babel}}

\DeclareOption{english}{%
  \PassOptionsToPackage{english}{babel}}

\DeclareOption*{%
  \PassOptionsToClass{\CurrentOption}{memoir}}

\ExecuteOptions{a4paper,danish}

% execution of options
\ProcessOptions

% package loading
\LoadClass{memoir}
\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}
\RequirePackage{babel}
\RequirePackage{lmodern}
\RequirePackage{amsmath,amssymb,amsthm}
\RequirePackage{siunitx}
\RequirePackage{tikz}
\RequirePackage{graphicx}

% main code
\@ifpackagewith{babel}{danish}{%
  \renewcommand\danishhyphenmins{22}}

%\setlrmarginsandblock{4cm}{*}{1}
%\setulmarginsandblock{3cm}{*}{1}
%\checkandfixthelayout[nearest]

\makepagestyle{testclass}
\makeoddhead{testclass}{\@author}{}{\today}
\makeheadrule{testclass}{\textwidth}{\normalrulethickness}
\makeoddfoot{testclass}{}{\thepage / \pageref{Lastpage}}{}
\pagestyle{testclass}

test.tex

\documentclass{testclass}
\author{name}
\begin{document}
\section*{Test}
\subsection*{a}
\end{document}

Best Answer

It's never a good idea to load babel in the class, because this has the consequence that no other language can be used unless one has the awkward code

\PassOptionsToPackage{lsorbian}{babel}
\documentclass{testclass}

However, the problem is that global options are always processed first when a package is loaded. So, if you say

\documentclass[english]{testclass}

the default danish option will be processed after english and so the main document language will be Danish in any case.

Instead of executing danish as default option, you can do in a different way:

% identification
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{testclass}[2013/12/02 testclass]

% declaration of options
\newif\ifdext@languagechosen

\DeclareOption{english}{%
  \PassOptionsToPackage{english}{babel}
  \dext@languagechosentrue
}
\DeclareOption{danish}{%
  \PassOptionsToPackage{danish}{babel}
  \dext@languagechosentrue
}

\DeclareOption*{%
  \PassOptionsToClass{\CurrentOption}{memoir}}

\PassOptionsToClass{a4paper}{memoir}

% execution of options
\ProcessOptions

% package loading
\LoadClass{memoir}
\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}
\ifdext@languagechosen
  \RequirePackage{babel}
\else
  \RequirePackage[danish]{babel}
\fi
\RequirePackage{lmodern}
\RequirePackage{amsmath,amssymb,amsthm}
\RequirePackage{siunitx}
\RequirePackage{tikz}
\RequirePackage{graphicx}

% main code
\@ifpackagewith{babel}{danish}{%
  \renewcommand\danishhyphenmins{22}}

%\setlrmarginsandblock{4cm}{*}{1}
%\setulmarginsandblock{3cm}{*}{1}
%\checkandfixthelayout[nearest]

\makepagestyle{testclass}
\makeoddhead{testclass}{\@author}{}{\today}
\makeheadrule{testclass}{\textwidth}{\normalrulethickness}
\makeoddfoot{testclass}{}{\thepage / \pageref{Lastpage}}{}
\pagestyle{testclass}

So if you load testclass with no language option, Danish will be the main (and only available) language. Here are the other possibilities:

  • \documentclass[danish]{testclass}: main (and only) language Danish
  • \documentclass[english]{testclass}: main (and only) language English
  • \documentclass[english,danish]{testclass}: main language Danish
  • \documentclass[danish,english]{testclass}: main language English

But, again, I'd not force users to such a limited choice, preferring to warn them if babel has not been loaded in the document. The setting of the hyphenation minima can be done with

\AtBeginDocument{%
  \@ifpackagewith{babel}{danish}{%
    \renewcommand\danishhyphenmins{22}}%
}

However this is the default for Danish, according to danish.ldf, so this setting is redundant.

Note also the different way to set A4 paper, because the default letterpaper option in memoir would override it.