# How do I conditionally execute something based on a documentclass option?

I'd like to conditionally do some things (like input a file) depending on if a specific class option was passed. For example, I'd like to have an "instructor" option to the class that would input a file of instructor notes. Something like:

\documentclass[instructor]{book}

\begin{document}
\if@instructor
\input{instructor-notes}
\fi
Rest of stuff
\end{document}


Is there some code I can put in the preamble to set something (maybe an ifthen boolean?) based on if the instructor option is passed to the class?

My goal in doing this is to make it trivial for someone to change the file to include the instructor portions of the book. If what I'm trying to do above is an ugly way to do it, I'd love to hear a more elegant way!

• possible duplicate of Loading a package conditionally Sep 15 '10 at 10:15
• Definitely related so it's certainly worth noting the link, but in this case I suspect that the package (if such is loaded) should be loaded in both cases but do different things depending on a class option. Sep 16 '10 at 11:32

Package and class options generally have meaning just for that particular class or package. The book class doesn't have an instructor option. If you're making your own class, based on book, then it is quite simple. It'd be something like

\newif\if@instructor
\@instructorfalse
\DeclareOption{instructor}{\@instructortrue}
\ProcessOptions\relax


Then you could use \if@instructor in the document (although you'd need to have used \makeatletter first, so maybe it's better to omit the @).

If you're not making your own class, then the easiest way would be to just have

\newif\if@instructor
\@instructortrue % or \@instructorfalse depending on which you want


• Why the @? What does it mean? Jun 14 '17 at 6:32
• @thiagowfx, It's just convention for macros or other control sequences that aren't meant for use outside the package/class. Normally, @ cannot be used as part of a multi-character control sequence. In .sty and .cls files, (of if you use \makeatletter), it can be. As for why I used it here? I suspect it's because it was used in the question.
– TH.
Jun 14 '17 at 7:42
• Thanks for your kind, fast and complete reply in a 7-year-old answer of yours. I am starting to like this TeX SE community! :) Jun 16 '17 at 0:07

More as an aside, class options get passed on to packages so if you don't want to modify the class but want to simulate the situation that you describe, you could write a new package, say instructor.sty, and have it do something with the instructor option. (You'd get a warning that the class didn't know how to deal with the instructor option.)

If your goal is to make this as easy as possible for the end user, I would make a lot of commands in the instructor.sty package that did one thing if given the instructor option but did something else if not, rather than have lots of \if ... \fis in your document. So, for example, you could have a command \instructorinput{filename} which input the filename if the instructor option was given but did nothing if not. Something like:

\ProvidesPackage{instructor}

\DeclareOption{instructor}{
\renewcommand{\instructorinput}[1]{\input{#1}}
}

\newcommand{\instructorinput}[1]{}

\ProcessOptions


I should say that TH's answer is probably the simplest way to do it in terms of code, though.

(Also, How can one undefine an already processed class option? is a slightly related question that may contain some information that may help)

• I think your way is better if there's a reason to actually make a style file, which it sounds like there might be.
– TH.
Sep 16 '10 at 3:23