\@ifclassloaded{} で \if \then \else を使用するにはどうすればよいでしょうか?

\@ifclassloaded{} で \if \then \else を使用するにはどうすればよいでしょうか?

\@ifclassloaded{}私は、ドキュメントクラスを処理するコマンドを使用するパッケージを作成しました。

\RequirePackage{pgffor}
\makeatletter%
\@ifclassloaded{book}
{%
<code block>
}
\makeatother%

\makeatletter%
\@ifclassloaded{article}
{%
<code block>
}
\makeatother%

report次のように、 ormemoirクラスとbookclass を同じ条件で処理したいと思います。

\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などの特定の機能をテストする方が適切です。\chapterbook

関連情報