# When writing a user defined package, how can I decide whether the \documentclass using it is book or article?

I want to write my own package as a collection of other packages I often use.

Some declarations in my package only work for book, some for article, and the remaining work for both.

In (La)TeX, how can we make a conditional declaration ?

``````\ProvidesPackage{MyPackage}

\newcommand\ContentsDir{Contents/}

\newcommand\ChapterDir{\ContentsDir}

%==============================================
%== It will be defined for book document class
%==============================================
\newcommand\IncludeChapter[1]{%
\renewcommand\ChapterDir{\ContentsDir#1/}%
\include{\ContentsDir#1}%
}

\newcommand\IncludeOnlyChapter[1]{%
\includeonly{\ContentsDir#1}%
}

\newcommand\Chapter[1]%
{%
\chapter{#1}%
}%
%==============================================

%==============================================
%== Common commands
%==============================================
\newcommand\SectionDir{\ChapterDir}

\newcommand\InputSection[1]{%
\renewcommand\SectionDir{\ChapterDir#1/}
\input{\ChapterDir#1}%
}
%----------------------------------------------
\newcommand\SubSectionDir{\SectionDir}

\newcommand\InputSubSection[1]{%
\renewcommand\SubSectionDir{\SectionDir#1/}
\input{\SectionDir#1}%
}
%----------------------------------------------
\newcommand\InputSubSubSection[1]{%
\input{\SubSectionDir#1}%
}
%----------------------------------------------
\newcommand\Section[1]%
{%
\section{#1}%
}%
%----------------------------------------------
\newcommand\SubSection[1]%
{%
\subsection{#1}%
}%
%----------------------------------------------
\newcommand\SubSubSection[1]%
{%
\subsubsection{#1}%
}%
%----------------------------------------------
``````
-

To answer the question in your title,

``````\@ifclassloaded{article}{%
% code for article
}{%
% code for book
}{%
% else clause
}%
}
``````

For more generic information about conditionals, see this question. For some more information about writing your own package, see the standard documentation file `clsguide`.

Update: Ulrich makes a good suggestion; for your particular case, it's probably sufficient to write instead

``````\@ifundefined{chapter}{%
% code for article
}{%
% code for book
}
``````
-
@xport: For completeness: that tests against the class directly, for example, neither case would turn out true for scrartcl or scrbook. It might be appropriate to test whether the \chapter command is defined, instead. – Ulrich Schwarz Dec 9 '10 at 6:26
thank you for the quick response. I will try now. – xport Dec 9 '10 at 6:30
@Ulrich, temporarily I just work with article and book. Since I am also interested in PSTricks manual layout written using koma-script, I will consider using scrbook later. Anyway, thank for your comment. – xport Dec 9 '10 at 7:03
where can I get the information on (La)TeX conditional, branching, looping commands or keywords? I want to learn them, sometimes I need make a loop inside a table to do something. :-) – xport Dec 9 '10 at 7:27
@xport — ask here `:)` and read, say, `source2e`, `etoolbox`, `expl3`, ... – Will Robertson Dec 9 '10 at 7:30