xkeyval キーを \thetitle または \theauthor にプリセットする

xkeyval キーを \thetitle または \theauthor にプリセットする

xkeyval私は LaTeX テンプレートを書いていて、ほぼ完了 (!!!) していますが、の動作を理解するのに非常に苦労しています\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文字列は 2 番目の呼び出しでのみ印刷されるということです。何が間違っているのでしょうか?

答え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
}

関連情報