Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
    
possible duplicate of Loading a package conditionally –  Grigory M 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. –  Loop Space Sep 16 '10 at 11:32
add comment

2 Answers

up vote 7 down vote accepted

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

in your preamble.

share|improve this answer
    
Perfect; thanks! –  Jason Sep 15 '10 at 4:05
add comment

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, http://tex.stackexchange.com/questions/1482/how-can-one-undefine-an-already-processed-class-option is a slightly related question that may contain some information that may help)

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.