9

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}%
%\addcontentsline{toc}{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}%
%\addcontentsline{toc}{section}{#1}%
}%
%----------------------------------------------
\newcommand\SubSection[1]%
{%
\subsection{#1}%
%\addcontentsline{toc}{chapter}{#1}%
}%
%----------------------------------------------
\newcommand\SubSubSection[1]%
{%
\subsubsection{#1}%
%\addcontentsline{toc}{chapter}{#1}%
}%
%----------------------------------------------
Display Name
  • 46,933

1 Answers1

10

To answer the question in your title,

\@ifclassloaded{article}{%
  % code for article
}{%
  \@ifclassloaded{book}{%
    % 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
}
  • 4
    @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 09 '10 at 06:26
  • @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. – Display Name Dec 09 '10 at 07: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. :-) – Display Name Dec 09 '10 at 07:27
  • 1
    @xport — ask here :) and read, say, source2e, etoolbox, expl3, ... – Will Robertson Dec 09 '10 at 07:30