Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}%
}%
%----------------------------------------------
share|improve this question

1 Answer 1

up vote 8 down vote accepted

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
}
share|improve this answer
3  
@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
1  
@xport — ask here :) and read, say, source2e, etoolbox, expl3, ... –  Will Robertson Dec 9 '10 at 7:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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