
\@ifclassloaded{}
私は、ドキュメントクラスを処理するコマンドを使用するパッケージを作成しました。
\RequirePackage{pgffor}
\makeatletter%
\@ifclassloaded{book}
{%
<code block>
}
\makeatother%
\makeatletter%
\@ifclassloaded{article}
{%
<code block>
}
\makeatother%
report
次のように、 ormemoir
クラスとbook
class を同じ条件で処理したいと思います。
\makeatletter%
\@ifclassloaded{book}
\else \@ifclassloaded{report}
\else \@ifclassloaded{memoir}
{%
<code block>
}
\makeatother%
しかし、動作しません。どうすればこれを実現できますか?
答え1
etoolbox
およびそのコマンドを使用して\ifboolexpr
、フォームの複数の条件を\<conditional [possibly with additional arguments]>{<true>}{<false>}
1 つに結合できます。
test
各条件文の前のキーワードとそれを囲む中括弧に注意してください。
\documentclass{article}
\usepackage{etoolbox}
\newcommand{\test}{} % just to make sure \test is undefined
\makeatletter
\ifboolexpr{ test {\@ifclassloaded{book}}
or test {\@ifclassloaded{report}}
or test {\@ifclassloaded{memoir}}}
{\def\test{lorem}}
{\def\test{ipsum}}
\makeatother
\begin{document}
\test
\end{document}
この例では、コマンドの存在を直接テストすることもできます\chapter
。何をしようとしているかに応じて、実際にはそれがより良いアイデアである可能性があります。
ここでは\ifundef
を使用しましたがetoolbox
、これは最初の例のロジックを逆転させます。( もありますが\ifdef
、これは定義されたとおりのコマンドを扱うため\relax
、このアプリケーションではそれほど役に立ちません。)
\documentclass{article}
\usepackage{etoolbox}
\newcommand{\test}{} % just to make sure \test is undefined
\ifundef\chapter
{\def\test{ipsum}}
{\def\test{lorem}}
\begin{document}
\test
\end{document}
答え2
の構文\@ifclassloaded
は
\@ifclassloaded{class}{yes code}{no code}
したがって、 は必要ありません\else
。両方のブランチが構文に組み込まれています。
あなたが持っている
\@ifclassloaded{book}
{%
<code block>
}
\makeatother%
つまり、「コードなし」引数は、\makeatother
クラスがブックでない場合にのみ実行されることになります。
\makeatletter
パッケージコードにはまたはは含まれていないはずな\makeatother
ので、フラグメントは次のようになります。
\RequirePackage{pgffor}
\@ifclassloaded{book}
{%
<code block>
}{%
else code
}
\@ifclassloaded{article}
{%
<code block>
}{%
else code
}
ちなみに、クラスを名前でチェックすることはあまりお勧めできません。何千ものクラスがあり、誰かがコピーしていくつかの変更を加えてクラスを作成した場合、パッケージはそれをブックのようなクラスとして認識しません。通常、クラスが と呼ばれるかどうかを確認するよりも、定義されているbook.cls
などの特定の機能をテストする方が適切です。\chapter
book