[OS X TeX] Test whether an option has loaded

Josep Maria Font jmfont at ub.edu
Fri Mar 2 12:47:33 CET 2012


On 02/03/2012, at 03:48, David Craig wrote:

> How does one test whether a particular documentclass option has been loaded?  


I know how to do this for a selfdefined option in a selfdefined class file (say, "myarticle"). I don't know whether it would work in a package file (.sty). In this case it loads the standard article class. It would have at least this contents:

%%%%%%%%%%%%%%%%%%%%%%%  file starts here  %%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myarticle}[2012/03/02 My article class]     

%%%  Establish the "mypreprint" option and its contrary as switches for a conditional:

\newif\ifmypreprint
\DeclareOption{mypreprint}{\global\mypreprinttrue}
\DeclareOption{mynopreprint}{\global\mypreprintfalse}

%%  Besides this, your class admits all other options of the "article" class:

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}} 

%%%  You make "mynopreprint" the default (so you put just nothing) and 
%%%  "article" the real class, here with some prechosen options:

\ExecuteOptions{mynopreprint}
\ProcessOptions
\LoadClass[a4paper,10pt,twoside,onecolumn]{article}

%%%  Now your conditional definitions:

\ifmypreprint                                     
  %%%  code for the mypreprint case
\else
  %%%  code for the mynopreprint, default case
\fi

%%%  And finally all your favorite permanent, non-conditional macros, etc.:


\endinput
%%%%%%%%%%%%%%%%%%%%%%%  file ends here  %%%%%%%%%%%%%%%%%%%%%%

In this way you can even use the conditonal \ifmypreprinttrue in the normal body. If you want a conditional that can only be used in the class file, then call it \ifmypr at print, for instance.

This may not be 100% accurate. The experts will speak.

To do this with the standard options of some existing class, what I would do is to read the class definition file and see whether there is a switch like the one above for the wanted option (e.g., "titlepage" in "article" has it). If the switch contains an @, then you can use it directly in packages files but only inside a \makeatletter ... \makeatother group in normal files.

Finally, if there is no such switch, you must duplicate the entire class file, rename it to "myclass.cls" and *add* the switch inside the definition of the wanted option.

Hope (some of) this helps,


JMaF




More information about the macostex-archives mailing list