3

My scenario is to let the user override the default options specified in myclass.cls.

Minimal Working Example

\RequirePackage{filecontents}

\begin{filecontents*}{myclass.cls}
\ProvidesClass{myclass}[2017/05/23 version 0.0.0.1]
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}} 
\ProcessOptions\relax
\LoadClass[]{article}

\RequirePackage[paperwidth=5cm,paperheight=3cm,hmargin=2mm,vmargin=2mm]{geometry}
\RequirePackage[many]{tcolorbox}

\AtBeginDocument{\begin{tcbitemize}[raster height=\textheight,raster columns=1,raster rows=1]}
\AtEndDocument{\end{tcbitemize}}
\pagestyle{empty}
\endinput
\end{filecontents*}

%\documentclass[paperwidth=4cm,paperheight=5cm]{myclass}% override the defaults
\documentclass{myclass}

\begin{document}
\tcbitem{A}
\tcbitem{B}
\tcbitem{C}
\end{document}

Question

How to override the default options that are specified in user defined class?

For example,

\documentclass[paperwidth=4cm,paperheight=5cm,]{myclass}

must override the default options but

\documentclass{myclass}

uses the default options.

3

You can use pgfopts to allow myclass.cls to accept/process key-value options. To do this you need to set up some pgfkeys to accept the options that you want to support, which is straightforward:

\pgfkeys{/myclass/.is family, /myclass,
  paperheight/.initial=3cm,
  paperwidth/.initial=5cm,
  hmargin/.initial=2mm,
  vmargin/.initial=2mm,
}

This defines options paperheight, paperwidth, hmargin and vmargin with the defaults that you listed. You can then access the key values using \pgfkeysvalueof{/myclass/paperheight} etc. Below I have defined a "helper" macro \Myclass to do this.

Following Passing (just) other options, when using pgfopts, you can pass all unrecognised options to the article class by using an "unknown key handler" to recursively build up the "extra" options that are passed to the class. Below these "extra options" are stored in the macro \extra@options.

Here is your updated MWE:

\RequirePackage{filecontents}

\begin{filecontents*}{myclass.cls}
\ProvidesClass{myclass}[2017/05/23 version 0.0.0.1]
\RequirePackage{pgfopts}

\def\extra@options{}% will hold "extra" options to pass to article
% set up supported options with default values
\pgfkeys{/myclass/.is family, /myclass,
  paperheight/.initial=3cm,
  paperwidth/.initial=5cm,
  hmargin/.initial=2mm,
  vmargin/.initial=2mm,
  .unknown/.code={\edef\extra@options{\extra@options,\pgfkeyscurrentname}}
}
% helper for accessing values of options
\newcommand\Myclass[1]{\pgfkeysvalueof{/myclass/#1}}

\ProcessPgfOptions{/myclass}% process options
\LoadClass[\extra@options]{article}% load article class with options

% give settings to geometry
\RequirePackage[paperwidth=\Myclass{paperwidth},%
                paperheight=\Myclass{paperheight},%
                hmargin=\Myclass{hmargin},%
                vmargin=\Myclass{vmargin},
               ]{geometry}

\RequirePackage[many]{tcolorbox}

\AtBeginDocument{\begin{tcbitemize}[raster height=\textheight,raster columns=1,raster rows=1]}
\AtEndDocument{\end{tcbitemize}}
\pagestyle{empty}
\endinput
\end{filecontents*}

%\documentclass[paperwidth=4cm,paperheight=5cm]{myclass}% override the defaults
\documentclass{myclass}

\begin{document}
\tcbitem{A}
\tcbitem{B}
\tcbitem{C}
\end{document}

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.