TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What would be the correct way to execute commands conditionally in LaTeX?

Here is the problem I am having. I have a book_pdf.tex where I wish to define the style of the book as it would look in a PDF. Then I have book_epub.tex where I wish to define the style of the book as it would look in an EPUB format.

Then I also have chapter1.tex, chapter2.tex, etc. In each of these chapters I have to do micro adjustments for each format. So I am thinking that I need to define a variable in either ebook_pdf.tex (something like var pdf = true in pseudosyntax), and then in chapter1.tex, I want to check if it's the pdf template that is used (something like if pdf then insert \newline). Or if it's the epub format, then I want to define epub variable and do stuff like if epub then \begin{lstlistings}[basicstyle=\smaller] else insert \begin{lstlistings}[basicstyle=\small].

Any ideas how to accomplish this?

share|improve this question
@xport: Are you seriously chastising Peteris for bad behavior? – Caramdir Aug 8 '11 at 20:53
@Caramdir: I support Peteris! – xport Aug 8 '11 at 21:02
@xport: Then you should know that Peteris wouldn't be affected by that rule (as he has posted less than 50 questions in total so far). And they seem pretty substantial (though this one has been asked in several variations before). – Caramdir Aug 8 '11 at 21:05
@Caramdir: I just want to let him know there is a good article that he might have not read yet. – xport Aug 8 '11 at 21:10

Define your own document class with an optional argument, eg for myClass.cls:


in your document you can use:

share|improve this answer
+1 Why did you not use * for the first \DeclareOption? – xport Aug 8 '11 at 20:11
@xport: ctan.org/pkg/clsguide – Andrey Vihrov Aug 9 '11 at 7:27
there is a different syntax for the starred version! – Herbert Aug 9 '11 at 8:44

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.