
後で使用するために、コンマ区切りのリストをキー値プロパティに保存しようとしていますが、正しく動作しません。
LaTeX が keyval パラメータを解析する方法はよくわかりませんが、パラメータをプレーンテキストとして処理しているのではないかと思います。
これは私が現在持っているコードです:
\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand { \getvmeta } { m } { \prop_item:cn { g_rvnlatex_doc_prop } { #1 } }
\NewDocumentCommand { \setvmeta } { m m } { \prop_gput:cnV { g_rvnlatex_doc_prop } { #1 } #2 }
\keys_define:nn { ravenhill/latex/meta } {
author .clist_set:N = \l_rvnlatex_author_clist ,
}
\NewDocumentCommand { \setup } { m } {
\group_begin:
\prop_new:c { g_rvnlatex_doc_prop }
\keys_set:nn { ravenhill/latex/meta } { #1 }
\setvmeta { author } { \l_rvnlatex_author_clist }
\group_end:
}
\NewDocumentCommand { \authorblock } { } {
\clist_new:N \l__authblock_authcopy_clist
\clist_set:Nn \l__authblock_authcopy_clist { \getvmeta { author } }
\clist_use:Nnnn \l__authblock_authcopy_clist { ~and~ } { ,~ } { ,~and~ }
}
\ExplSyntaxOff
\setup {
author = {John, Bob},
}
\begin{document}
\authorblock
\end{document}
得られた結果John,Bob
は の代わりにですJohn and Bob
。
何か見落としている点はありますか?
答え1
あなたの推測は正しいです。
\clist_set:Nn \l__authblock_authcopy_clist { \getvmeta { author } }
clist パーサーは、にはコンマが含まれていないため\getvmeta { author }
、 に関する限り、 は と何ら変わらないと認識します。getvmeta { author }
キーに渡したコンマ リストを公開するにはauthor
、その内容を展開する必要があります\getvmeta{author}
。そのためには、\clist_set:Nx
(x
は網羅的な展開を意味します) を使用します。 は展開によって機能するため、展開コンテキストで項目を返すことができます (で宣言して が展開できるよう\prop_item:Nn
にする必要もあります)。\getvmeta
\NewExpandableDocumentCommand
また、変数宣言をユーザー レベルのコマンドの外部に移動します。誰かが、たとえば 2 回使用しようとするかどうかはわかりません。\setup
そのような場合、既に存在する変数を宣言しようとすると、コードでエラーが発生します。
\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand { \getvmeta } { m }
{ \prop_item:Nn \g_rvnlatex_doc_prop {#1} }
\NewDocumentCommand { \setvmeta } { m m }
{ \prop_gput:NnV \g_rvnlatex_doc_prop {#1} #2 }
\keys_define:nn { ravenhill/latex/meta }
{ author .clist_set:N = \l_rvnlatex_author_clist }
\prop_new:N \g_rvnlatex_doc_prop
\clist_new:N \l__authblock_authcopy_clist
\NewDocumentCommand \setup { m }
{
\group_begin:
\keys_set:nn { ravenhill/latex/meta } {#1}
\setvmeta { author } { \l_rvnlatex_author_clist }
\group_end:
}
\NewDocumentCommand \authorblock { }
{
\clist_set:Nx \l__authblock_authcopy_clist { \getvmeta { author } }
\clist_use:Nnnn \l__authblock_authcopy_clist { ~and~ } { ,~ } { ,~and~ }
}
\ExplSyntaxOff
\setup{author = {John, Bob}}
\begin{document}
\authorblock
\end{document}