선택 옵션을 패키지에 전달할 수 없습니다.

선택 옵션을 패키지에 전달할 수 없습니다.

모든 개인 매크로와 개인 스타일에 맞는 사용자 정의 클래스를 포함하는 사용자 정의 패키지를 만들려고 합니다. 나는 내가 수강하는 다양한 과정에 대한 표기법을 변경하기 위해 또는 notation값을 취할 수 있는 키워드 인수를 전달하고 싶었습니다 (예를 들어 수학에서는 복소 공액은 종종 막대로 표시되고 물리학에서는 별표로 표시됩니다). 이리저리 놀면서 얻은 답변을 따른 후mathphysics여기. 저는 이 3개의 파일을 얻었습니다.

% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]

\RequirePackage{xstring}

\DeclareKeys[demo]
{
    notation.choices:nn =
        { physics, math }
        {\ExpandArgs{Nc}\let\@demo@notation{l_keys_choice_tl}},
    notation.usage = load,
    notation.initial:n = physics
}

\ProcessKeyOptions[demo]

\IfStrEqCase{\@demo@notation}{
    {physics}{}%
    {math}{}%
}[
    \PackageError{demo-pkg}{Invalid notation value: \@demo@notation}
    {Choose value of: physics, math}
]

\RequirePackage{xparse}

\IfStrEqCase{\@demo@notation}{%
{physics}{%
\NewDocumentCommand{\Conjugate}{ m }{{##1}^{\ast}}%
}%
{math}{%
\NewDocumentCommand{\Conjugate}{ m }{\overline{##1}}%
}%
}%

% demo-cls.cls
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{demo-cls}[Demo class]

\DeclareKeys[demo-cls]
{
    notation.choices:nn =
        { physics, math }
        {\PassOptionsToPackage{notation=l_keys_choice_tl}{demo-pkg}}, % <- This seems to cause issues
    notation.usage = load,
    notation.initial:n = physics
}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}

\ProcessKeyOptions[demo-cls]
\ProcessOptions\relax

\LoadClass[a4paper, 12pt]{article}
\RequirePackage{demo-pkg}

% main.tex
\documentclass[notation=math]{demo-cls}

\begin{document}
\[ z = x + iy \Longleftrightarrow \Conjugate{z} = x - iy \]
\end{document}

이 코드를 컴파일하려고 하면 오류가 발생합니다 Key 'demo/notation' accepts only a fixed set of choices.. 내가 직접 사용할 때 demo-pkg:

\documentclass{article}

\usepackage[notation=math]{demo-pkg}

\begin{document}
\[ z = x + iy \Longleftrightarrow \Conjugate{z} = x - iy \]
\end{document}

잘 컴파일되고 패키지 매개변수를 사용하여 표기법을 전환할 수 있습니다. 클래스에서 패키지로 매개변수를 전달하면 문제가 발생하는 것 같습니다. 나는 이 코드 블록을 / 로 둘러싸서 사용해 \l_keys_choice_tl보았습니다 . 아무것도 작동하지 않습니다. 같은 오류가 발생합니다. 클래스 에서 패키지 로 매개변수 를 어떻게 전달할 수 있나요 ?\tl_use:N \l_keys_choice_tl\ExplSyntaxOn\ExplSyntaxOffnotationdemo-clsdemo-pkg

답변1

notation=mathfrom을 \documentclass패키지로 전달할 필요는 없습니다 . 패키지는 를 사용하므로 \ProcessKeyOptions해당 옵션이 포함된 전역 옵션 목록을 구문 분석합니다.

\ProcessKeyOptions그리고 클래스의 메커니즘을 사용하여 알 수 없는 옵션을 article. 따라서 변경이 필요한 유일한 파일은 클래스입니다.

demo-cls.cls:

% demo-cls.cls
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{demo-cls}[Demo class]

\DeclareKeys[demo-cls]
  {
    unknown .code = \PassOptionsToClass{\CurrentOption}{article}
  }

\ProcessKeyOptions[demo-cls]

\LoadClass[a4paper, 12pt]{article}
\RequirePackage{demo-pkg}

demo-pkg.sty:

% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]

\RequirePackage{xstring}

\DeclareKeys[demo]
{
    notation.choices:nn =
        { physics, math }
        {\ExpandArgs{Nc}\let\@demo@notation{l_keys_choice_tl}},
    notation.usage = load,
    notation.initial:n = physics
}

\ProcessKeyOptions[demo]

\IfStrEqCase{\@demo@notation}{
    {physics}{}%
    {math}{}%
}[
    \PackageError{demo-pkg}{Invalid notation value: \@demo@notation}
    {Choose value of: physics, math}
]

\RequirePackage{xparse}

\IfStrEqCase{\@demo@notation}{%
{physics}{%
\NewDocumentCommand{\Conjugate}{ m }{{##1}^{\ast}}%
}%
{math}{%
\NewDocumentCommand{\Conjugate}{ m }{\overline{##1}}%
}%
}%

main.tex:

% main.tex
\documentclass[notation=math]{demo-cls}

\begin{document}
\[ z = x + iy \Longleftrightarrow \Conjugate{z} = x - iy \]
\end{document}

약간의 설명이 필요하다고 생각합니다 (이전 답변을 되돌아 보면).

\ExpandArgs인수 확장을 미세하게 제어할 수 있는 고급 인터페이스를 제공하는 매크로입니다. L3 언어를 직접 사용하지 않고( 또는 없이 ) 패키지 내부를 \@demo@notationL3 변수 값으로 설정하는 데 사용했습니다. 이는 를 사용하여 수행되었습니다 . 즉, 두 개의 인수를 사용하기 전에 첫 번째는 변경되지 않은 단일 토큰( )이고 두 번째는 에 의해 매크로를 형성해야 하는 중괄호 그룹입니다 . 이 저수준 구성의 더 읽기 쉬운 변형은 다음과 같습니다 .\ExplSyntaxOn\ProvidesExplPackage\ExpandArgs{Nc}\let\@demo@notation{l_keys_choice_tl}\letN\csname\ExpandArgs{Nc}\let\@demo@notation{l_keys_choice_tl}

\expandafter\let\expandafter\@demo@notation\csname l_keys_choice_tl\endcsname

이 설명이 귀하의 이유를 설명하는 데 도움이 되기를 바랍니다.

\DeclareKeys[demo-cls]
{
    notation.choices:nn =
        { physics, math }
        {\PassOptionsToPackage{notation=l_keys_choice_tl}{demo-pkg}}, % <- This seems to cause issues
    notation.usage = load,
    notation.initial:n = physics
}

작동하지 않았습니다. l_keys_choice_tl마술처럼 실제 선택 값이 되지 않기 때문에 이는 에 의해 수행되었으며 \ExpandArgs여기에서도 비슷한 수단으로 수행되었을 것입니다(내 의견이 제안함).

관련 정보