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.

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?

  • Welcome to LaTeX! Welcome to TeX.sx! You can enclose words in backticks ` to mark them as code, as can be seen in my edit. (You can also highlight the code and click the "code" button (with "{}" on it) or hit Ctrl + K.) – Torbjørn T. Apr 18 '12 at 19:55
  • 4
    Why not just let \doctype be the document class itself? So then you can say \documentclass{\doctype}, and afterwards load the appropriate preamble. – Peter Grill Apr 18 '12 at 19:56
  • You can use \ifx. See here for an example: What does \ifx\\#1\\ stand for? – Marco Daniel Apr 18 '12 at 19:57
  • Thank you for the tip Torb. Peter, because I use the same documentclass for several styles and I also use that doctype variable to set the rest of the template. Marco Daniel, wow, what can I say? I am completely lost. Do you mean I can do something like this? \ifx\doctype\letter <code for #1 is empty> \else \ifx\doctype\book <code for #1 is _not_ empty> \else \fi \fi – tuxtlequino Apr 18 '12 at 22:07

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

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


    \def\@clsopt@select{#1}%-------------------- default class
    \edef\@clsopt@list{\zap@space#2 \@empty}%--- list of classes
    \@for\CurrentOption:=\@currclass@opt\do{%--- Go through documents options



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


It can then be used as follows:


%---- [ Put this in template for \input{} ] -------------
\documentclass[\DocType,\FontSize, \PaperSize]{generalclass}
%% Usage
%---- [ End template ] ----------------------------------

    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
  • Wow Daniel, this is perfect. Thank you very much for providing the code. This is WAY beyond my knowledge of LaTex. I read and re-read until I figured out what was going on. If I had any reputation I would give you points for this answer. I am know scratching my head thinking how to use something like memoir (so I can use the document class article and memoir and the article option. How would you go about accomplishing this? Should I create another question for that? – tuxtlequino Apr 19 '12 at 20:02
  • @tuxtlequino: I have changed the code a little bit to make it more general – Danie Els Apr 20 '12 at 5:37
  • Daniel, I spent hours trying to find documentation for some of the things that you used in the code you provided without any meaningful results. Could you please point me somewhere I can figure out your code, or even better, could you document every line explaining what is happening? – tuxtlequino Apr 20 '12 at 6:02
  • I think I did not made myself clear on my second question. The improvements to the code are appreciated but I still cannot do something like this... \documentclass{10pt,oneside,article}{memoir} or this \documentclass{10pt,oneside,book}{memoir} – tuxtlequino Apr 20 '12 at 15:46
  • @tuxtlequino: Regarding the code, it is mostly for the source of the LaTeX kernel (see source2e.pdf) taken from the \ProcessOptions commands. For the second part the class type is now alway an optional argument for generalclass e.g. \documentclass[<options>, article]{generalclass} or \documentclass[<options>, memoir]{generalclass}. Please look at the example! – Danie Els Apr 21 '12 at 3:39

You can define macros which hold the different values (letter, book, etc.) and then compare it with \doctype using \ifx\firstmacro\secondmacro <equal> \else <not equal> \fi:

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

  % doctype is 'letter'
  % doctype is 'book'

Note that it is also possible (but not always recommendable) to load a package before the class using \RequirePackage instead of \usepackage.

You could also make your own class files which load the real class using \LoadClassWithOptions{<real class>} and then include your required settings for this class.

  • Arg, just seeing now that tuxtlequino posted basically the same as comment 11 hours ago! Well I keep it for now. – Martin Scharrer Apr 19 '12 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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