How do I execute some commands conditionally?

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?

-
– Caramdir Aug 8 '11 at 20:52
@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:

\newif\ifEPUB\EPUBfalse
\DeclareOption{EPUB}{\EPUBtrue}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{book}}
\ProcessOptions\relax
\RequirePackage{...}
...


in your document you can use:

\documentclass[EPUB,11pt,a4paper,...]{myClass}
\usepackage{...}
...
\begin{document}
...
\ifEPUB
...
\else
...
\fi
...
\end{document}

-
+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