1

I have the following class

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{democlass}[2022/11/15]
\RequirePackage{kvoptions}
\DeclareStringOption[\the\year{}]{year}
\ProcessOptions\relax
\LoadClass{article}
\def\foo{\democlass@year}
\endinput

and the following document

\documentclass[year=1999]{democlass}

\begin{document} \foo \end{document}

I would expect it to display 1999. But it shows the current year. What did I do wrong?

1 Answers1

3

You must process the options with \ProcessKeyvalOptions:

\begin{filecontents}[overwrite]{democlass.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{democlass}[2022/11/15]
\RequirePackage{kvoptions}
\DeclareStringOption[\the\year{}]{year}
\ProcessKeyvalOptions*\relax
\LoadClass{article}
\def\foo{\democlass@year}
\endinput
\end{filecontents}

\documentclass[year=1999]{democlass}

\begin{document} \foo \end{document}

As alternative with a current LaTeX you can use the inbuilt keyval system:

\begin{filecontents}[overwrite]{democlass.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{democlass}[2022/11/15]
\DeclareKeys
 { 
   year .tl_set:N  = \democlass@year,
   year .default:n = \the\year,
   year .initial:n = 2000,
 }   
\ProcessKeyOptions

\LoadClass{article} \def\foo{\democlass@year} \endinput \end{filecontents}

\documentclass[year=1999]{democlass}

\begin{document} \foo \end{document}

Ulrike Fischer
  • 327,261