5

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?

5
  • See tex.stackexchange.com/questions/1492/…
    – Caramdir
    Commented Aug 8, 2011 at 20:52
  • @xport: Are you seriously chastising Peteris for bad behavior?
    – Caramdir
    Commented Aug 8, 2011 at 20:53
  • @Caramdir: I support Peteris! Commented Aug 8, 2011 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
    Commented Aug 8, 2011 at 21:05
  • @Caramdir: I just want to let him know there is a good article that he might have not read yet. Commented Aug 8, 2011 at 21:10

1 Answer 1

5

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
\LoadClass{book}
\RequirePackage{...}
...

in your document you can use:

\documentclass[EPUB,11pt,a4paper,...]{myClass}
\usepackage{...}
...
\begin{document}
...
\ifEPUB
  ...
\else
  ...
\fi
...
\end{document}
3
  • 1
    +1 Why did you not use * for the first \DeclareOption? Commented Aug 8, 2011 at 20:11
  • @xport: ctan.org/pkg/clsguide Commented Aug 9, 2011 at 7:27
  • there is a different syntax for the starred version!
    – user2478
    Commented Aug 9, 2011 at 8:44

You must log in to answer this question.