# Choose documentclass according to condition

I would like to choose the documentclass according to a value. For instance, something like this

\newcommand{\mode}{0}

if (mode is 0)
then \documentclass{...}
else if (mode is 1)
then \documentclass{---}
fi


Is there a way to do such a thing?

-
Yes of course but this really doesn't make a lot of sense: the document class defines what may and may not be in the content ... – Joseph Wright Feb 12 at 13:52
Yes you're right, actually it's not to choose the documentclass but rather the options and some package that have to be included or not. – minomic Feb 12 at 13:53
@JosephWright I disagree. I have a customized class for producing our organizational reports. But if I want to convert it into a journal manuscript, I replace the customized documentclass with article PLUS a package that does the conversion of the customized macros into something reasonable. In this way, by this simple change, I can totally revamp the total look of the document in a consistent way. – Steven B. Segletes Feb 12 at 13:54
beamer versus beamerarticle would be another example. – JPi Feb 12 at 15:08

I revised my MWE to answer some of the criticisms about the difficulties encountered by having a selectable document class. In the MWE, if I choose the article class, I also redefine other things (in this case \chapter) to make it compatible with the alternate class, which is book in this case.

With such an extended approach, one can quickly alternate between custom and standard classes, if one takes the time to redefine a logical way to handle the custom macros.

\def\mode{1}
\if 0\mode
\documentclass{article}
\let\chapter\section
\else
\documentclass{book}
\fi
\usepackage{lipsum}
\begin{document}
\chapter{This is my chapter}
\lipsum[1-10]
\end{document}


If one wants a more descriptive multi-character mode name, then one could do this:

\def\mode{ArticleMode}
\def\ArticleMode{%
\documentclass{article}
\let\chapter\section
}
\def\BookMode{%
\documentclass{book}
}
\csname\mode\endcsname
\usepackage{lipsum}
\begin{document}
\chapter{This is my chapter}
\lipsum[1-10]
\end{document}

-
Thank you! I did not know this syntax of if to compare two values... – minomic Feb 12 at 13:55
@minomic The use of \if compares a two successive bytes. If you wanted to compare two-digit modes (e.g., \def\mode{11}), you would have to use \ifnum as shown in Christian's answer. But with \if, the modes could be called A, B, C, etc. instead of numbers. – Steven B. Segletes Feb 12 at 14:06
I see... And what if I want to compare strings? Like \newcommand{\mode}{firstMode} and then if (mode is firstMode) – minomic Feb 12 at 14:14
@StevenB.Segletes: \RequirePackage{ifthen} should work even before \documentclass. The LaTeX core is active at that time of loading already. \usepackage{ifthen} does not work (as far as I know) – Christian Hupfer Feb 12 at 15:00
@ChristianHupfer Thanks. That is good to know. – Steven B. Segletes Feb 12 at 15:04

Possible: yes, but it's necessary to change it most times by hand anyway.

The main culprit is that different classes provide different features and some of them are contradictory to each other,i.e. one has to make conditionals to choose the correct settings afterwards anyway.

\newcommand{\mode}{1}

\ifnum\mode=0
\documentclass{article}
\else
\documentclass{book}
\fi

\begin{document}

Foo

\ifnum\mode>0
\chapter{Foo}
\fi
\end{document}


Here's a way with \ifcase

\newcommand{\mode}{3}

\ifcase\mode
\documentclass{article}
\or \documentclass{report} % 1
\else
\documentclass{book} % Default
\fi

\begin{document}

\tableofcontents

\ifnum\mode>0
\chapter{Foo}
\else
\section{Foo}
\fi
\end{document}


Update -- with etoolbox

It is possible to use \RequirePackage{...} even before \documentclass but not \usepackage{...} -- there are cases where this might be really necessary, but in general I don't recommend this procedure.

\RequirePackage{etoolbox}
\def\mode{artmode}

\ifstrequal{\mode}{artmode}{%
\documentclass{article}
}{%
\documentclass{report}
}

\begin{document}

\tableofcontents

\section{Foo}
\end{document}

-
Thank you: this works as well – minomic Feb 12 at 13:56
I'd go with the \ifcase model, that's much more flexible. I'd also use \providecommand{\mode}{<number>} in the top line, so the value can be also set in the command line: pdflatex '\newcommand\mode{2}\input{filename}' (so without changing the file, which is also possible, of course). – egreg Feb 12 at 15:13