[Tex/LaTex] Using string conditionals to determine document class

conditionals

I am trying to set up a set of templates for most of the styles I have created or collected. Right now I am using a bunch of LaTeX files with the \input{} and setting variables with \def. What I am looking to accomplish is to have the template setup execute a series of steps depending on the variable sent. For example.

My main document will have something like this on the first couple of lines.

\def\doctype{letter} % this could also be a book, article, beamer presentation, etc.
\input{template-setup}

Based on the doctype I want to get my template setup to select the right document class. I know of the xstring or xifthen packages, but since I want to use the doctype variable to select the right document class, this is not possible (since you cannot use a package before \documentclass). Can somebody please help me?

Best Answer

I will use a general class file that can load the specific class as an option and create a boolean type test for all the different classes. The following is just a first try. It can be enhanced with keyvalues, etc.

Edit 1: Add command \ClassList to provide your own class list.

%%%% generalclass.cls %%%%

\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesClass{generalclass}[2012/04/19 v0.1 General class (DNJ ELS)]

\RequirePackage{etoolbox}

\newcommand*\DeclareClassList[2]{%
    \def\@clsopt@select{#1}%-------------------- default class
    \edef\@clsopt@list{\zap@space#2 \@empty}%--- list of classes
    \@for\@clsopt@item:=\@clsopt@list\do{%
        \newbool{@clsopt@test@\@clsopt@item}%
        \csedef{@clsopt@def@\@clsopt@item}{%
            \def\noexpand\@clsopt@select{\@clsopt@item}}}
    \edef\@currclass@opt{\@ptionlist{\@currname.\@currext}}%
    \@for\CurrentOption:=\@currclass@opt\do{%--- Go through documents options
        \ifcsdef{@clsopt@def@\CurrentOption}{%
            \csuse{@clsopt@def@\CurrentOption}%
            \csdef{ds@\CurrentOption}{}%
            \@use@ption}{}}}

\newcommand*\ThisClass{}
\newcommand\SetClassOption{%
    \edef\@clsopt@temp{%
        \noexpand\booltrue{@clsopt@test@\@clsopt@select}%
        \def\noexpand\ThisClass{\@clsopt@select}}%
    \@clsopt@temp}

\newcommand\IfClass[3]{%
    \ifbool{@clsopt@test@#1}{#2}{#3}}

%----------------------------------------------------------------------
\providecommand*\ClassDefault{article}                   % Edit 1
\providecommand*\ClassList{article,report,book,letter}   % Edit 1
\DeclareClassList{\ClassDefault}{\ClassList}             % Edit 1 
\SetClassOption

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\ThisClass}}
\ProcessOptions\relax
\LoadClass{\ThisClass}
\endinput

It can then be used as follows:

\newcommand*\DocType{memoir}
\newcommand*\FontSize{10pt}
\newcommand*\PaperSize{a4paper}

%---- [ Put this in template for \input{} ] -------------
\newcommand*\ClassList{article,report,book,letter,memoir}
\documentclass[\DocType,\FontSize, \PaperSize]{generalclass}
%% Usage
%\IfClass{article}{..<true>..}{..<false..}
%\IfClass{report}{..<true>..}{..<false..}
%\IfClass{book}{..<true>..}{..<false..}
%\IfClass{letter}{..<true>..}{..<false..}
%\IfClass{memoir}{..<true>..}{..<false..}
%---- [ End template ] ----------------------------------

\begin{document}
    This class is: \ThisClass \par
    This is \IfClass{article}{an}{not an} article class document\par
    This is \IfClass{report}{a}{not a} report class document\par
    This is \IfClass{book}{a}{not a} book class document\par
    This is \IfClass{letter}{a}{not a} letter class document\par
    This is \IfClass{memoir}{a}{not a} memoir class document\par
\end{document}
Related Question