5

I have listed dvipsnames (or other options) in \LoadClass with the hope I don't need to specify dvipsnames (or other options) in \documentclass.

Apparently this does not work as what I expect. It means, I need specify dvipsnames in \documentclass even though this option has been specified by default in \LoadClass. It looks like the options defined in \LoadClass get ignored.

MWE

\begin{filecontents}{dummy.cls}
%\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesClass{dummy}[2015/10/21 v 0.01 description]
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass[% the default options
    dvipsnames,
    12pt,
]{article}
\RequirePackage{xcolor}

\def\test{\textcolor{Red}{It is a test.}}

\endinput
\end{filecontents}


% try remove dvipsnames 
\documentclass[dvipsnames]{dummy}
\begin{document}
\test
\end{document}

Questions

What is the correct way to define default options when making user defined class?

1
  • article does not know what to do with dvipsnames this way. I think you have to specify dvipsnames to be an option of dummy.cls two and pass the parameter explicitly to the package with \PassOptionsToPackage{dvipsnams}{xcolor}
    – user31729
    Oct 20, 2015 at 16:51

1 Answer 1

7

The options to \LoadClass don't become global options, so they're not passed to every loaded package; so you have to add dvipsnames manually, if you want it to become global.

The option dvipsnames is not defined in article.cls, so it does nothing, but it doesn't raise an error either, because of how options are dealt with in class files.

\ProvidesClass{dummy}[2015/10/21 v 0.01 description]
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass[% the default options
    12pt,
]{article}
\@expandtwoargs\in@{dvipsnames}{\@classoptionslist}
\ifin@
  % dvipsnames has been used as a global option by the user, do nothing
\else
  % append dvipsnames to the list of global options
  \ifx\@classoptionslist\@empty
    \g@addto@macro\@classoptionslist{dvipsnames}
  \else
    % with a comma if the list is not empty
    \g@addto@macro\@classoptionslist{,dvipsnames}
  \fi
\fi
\RequirePackage{xcolor}

\def\test{\textcolor{Red}{It is a test.}}

\endinput

Now calling either \documentclass{dummy} or \documentclass[dvipsnames]{dummy} will result in \@classoptionslist to contain dvipsnames, so upon loading xcolor the dvipsnames option will be passed to it.

However, I'm not sure this is a good thing to do.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .