10

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

1 Answer 1

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
  • 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. Commented Dec 9, 2010 at 6: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. Commented Dec 9, 2010 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. :-) Commented Dec 9, 2010 at 7:27
  • 1
    @xport — ask here :) and read, say, source2e, etoolbox, expl3, ... Commented Dec 9, 2010 at 7:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.