clist를 keyval prop으로 어떻게 구문 분석할 수 있나요?

clist를 keyval prop으로 어떻게 구문 분석할 수 있나요?

나중에 사용하기 위해 쉼표로 구분된 목록을 키-값 속성에 저장하려고 하는데 제대로 작동하지 않습니다.

나는 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,BobJohn 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

또한 변수 선언을 사용자 수준 명령 외부로 이동하십시오. 누군가가 두 번 사용하려고 시도할지 알 수 없으며 \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}

관련 정보