\@ifclassloaded{}와 함께 \if \then \else를 어떻게 사용할 수 있나요?

\@ifclassloaded{}와 함께 \if \then \else를 어떻게 사용할 수 있나요?

\@ifclassloaded{}나는 문서 클래스를 처리하기 위해 명령을 사용하는 패키지를 작성했습니다 .

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

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

다음과 같이 동일한 조건으로 클래스를 사용하여 report또는 memoir클래스를 처리하고 싶습니다 .book

\makeatletter%
\@ifclassloaded{book}
\else  \@ifclassloaded{report}
\else  \@ifclassloaded{memoir} 
{%
<code block>
}
\makeatother%

하지만 작동하지 않습니다. 어떻게 이를 달성할 수 있나요?

답변1

etoolbox및 해당 명령을 사용하여 \ifboolexpr양식의 여러 조건문을 \<conditional [possibly with additional arguments]>{<true>}{<false>}하나로 결합할 수 있습니다.

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여기서는 from 을 사용했는데 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클래스가 책이 아닌 경우에만 실행한다는 것입니다.

어쨌든 패키지 코드에는 \makeatletteror 가 있어서는 안 되므로 조각이 다음과 같아야 합니다.\makeatother

\RequirePackage{pgffor}

\@ifclassloaded{book}
{%
<code block>
}{%
 else code
}

\@ifclassloaded{article}
{%
<code block>
}{%
    else code
}

덧붙여서 이름으로 클래스를 확인하는 것은 실제로 권장되지 않습니다. 수천 개의 클래스가 있으며 누군가가 book.cls몇 가지를 복사하고 변경하여 클래스를 만든다면 패키지는 이를 책과 같은 클래스로 인식하지 못할 것입니다. 일반적 \chapter으로 클래스가 호출되는지 확인하는 것보다 정의와 같은 특정 기능을 테스트하는 것이 더 좋습니다 book.

관련 정보