xkeyval 키를 \thetitle 또는 \theauthor로 사전 설정

xkeyval 키를 \thetitle 또는 \theauthor로 사전 설정

xkeyvalLaTeX 템플릿을 작성 중이고 거의 완료되었지만(!!!) 의 동작을 이해하는 데 매우 어려움을 겪고 있습니다 \presetkeys. 몇 가지 기본값을 설정할 수 있었지만 이를 설정하면 \theauthor및 명령 \thetitle뒤에 설정되었더라도 키가 빈 문자열로 설정됩니다 .\author{}\title{}

다음은 mwe입니다.

my_mwe.cls

\RequirePackage{expl3}

\ProvidesExplClass{my_mwe}
                  {2020/09/08}
                  {1.0}
                  {Minimal working example}

\LoadClass{report}

\RequirePackage{xkeyval}
\RequirePackage{xparse}

\AtBeginDocument{%
    \define@key{my_mwe} {author} [] {\def\my_mwe@author{#1}}
    \define@key{my_mwe} {title}  [] {\def\my_mwe@title{#1}}
    \presetkeys{my_mwe} {author}    {author={\theauthor}}
    \presetkeys{my_mwe} {title}     {title=\thetitle}
}

\NewDocumentCommand{\MWECommand}{O{}}{%
    \setkeys{my_mwe}{author, title, #1}%

    Here's~the~output:\\
    \my_mwe@title \\
    \my_mwe@author
}

mwe.tex

\documentclass[10pt, a4paper]{my_mwe}

\usepackage{titling}
    \author{The poor crying author}
    \title{A sad mwe}

\begin{document}

\MWECommand%

\MWECommand[title=\thetitle, author=\theauthor]

\end{document}

내가 기대하는 것은 에 대한 기본 호출이 MWECommand인수된 호출과 동일하지만 대신 \theauthor\thetitle문자열이 두 번째 호출에서만 인쇄된다는 것입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변1

author값 없이 또는 키를 전달하는 경우 사용되는 기본값을 title비어 있도록 명시적으로 설정합니다.

\define@key{my_mwe}{author}[]{\def\my_mwe@author{#1}}

대괄호 안에 지정된 값은 기본값입니다(이 경우 비어 있음). 매크로 \presetkeys는 키가 명시적으로 전혀 설정되지 않은 경우 사용되는 값인 초기 값을 설정합니다.

이제 매크로에 author, title, #1을 입력 \setkeys하면 초기 값이 아닌 기본값이 사용됩니다. 그리고 기본값은 비어 있습니다.

따라서 가장 쉬운 방법은 정의에서 제거하는 것입니다 author, title. (또한 정의에서 빈 줄을 제거했습니다. \par결과가 될 것입니다.)

\RequirePackage{expl3}

\ProvidesExplClass{my_mwe}
                  {2020/09/08}
                  {1.0}
                  {Minimal working example}

\LoadClass{report}

\RequirePackage{xkeyval}
\RequirePackage{xparse}

\AtBeginDocument{%
    \define@key{my_mwe} {author} [] {\def\my_mwe@author{#1}}
    \define@key{my_mwe} {title}  [] {\def\my_mwe@title{#1}}
    \presetkeys{my_mwe} {author}    {author={\theauthor}}
    \presetkeys{my_mwe} {title}     {title=\thetitle}
}

\NewDocumentCommand{\MWECommand}{O{}}{%
    \setkeys{my_mwe}{#1}%
    Here's~the~output:\\
    \my_mwe@title \\
    \my_mwe@author
}

관련 정보