以同样的方式拆分嵌套和简单的逗号列表

以同样的方式拆分嵌套和简单的逗号列表
\documentclass{article}
\usepackage{kvsetkeys,kvdefinekeys}
\makeatletter

\def\Process@Split#1{%
  \if@tempswa
    \edef\ST@LastName{\comma@entry}% 
    \typeout{* LastName:\ST@LastName}%
    \@tempswafalse   
  \else
    \edef\ST@FirstName{\comma@entry}%
    \typeout{* FirstName:\ST@FirstName}%
  \fi
}
\newcommand*\Split@Name[1]{
  \begingroup
    \@tempswatrue
    \expandafter\comma@parse\expandafter{#1}{\Process@Split}
  \endgroup
}

\newcommand*\Process@Authors[1]{%
  \typeout{>>>>>\comma@entry<<<<<}
  \Split@Name{\comma@entry} 
}

\kv@define@key{ST}{author}{%
  \def\ST@author{#1}%
}
\kv@define@key{ST}{foo}{%
  % ...
}

\newcommand\Test[1][]{
  \kvsetkeys{ST}{#1}%
  \expandafter\comma@parse\expandafter{\ST@author}{\Process@Authors}
}

\begin{document}         
\Test[%
  author={{LastName1A, FirstName1A},{LastName2B,FirstName2B}},
  foo=bla
]
\Test[%
  author={LastName1C, FirstName1C},
  foo=bla
]
\end{document}

在上面的例子中,我想使用 author key 来表示一个或多个作者,并将每个作者拆分为名字和姓氏。该示例对于只有一个作者的情况不正确。有没有解决方案可以解决我的问题

author={{LastName1C, FirstName1C}}

需要写吗?我想保留这个kvsetkeys包裹。提前谢谢。

罗尔夫

答案1

下面使用两个expl3函数(\clist_count:n\clist_item:nn)实现我在评论中概述的检查。

\documentclass{article}
\usepackage{kvsetkeys,kvdefinekeys}
\makeatletter

\def\Process@Split#1{%
  \if@tempswa
    \edef\ST@LastName{\comma@entry}% 
    \typeout{* LastName:\ST@LastName}%
    \@tempswafalse   
  \else
    \edef\ST@FirstName{\comma@entry}%
    \typeout{* FirstName:\ST@FirstName}%
  \fi
}
\newcommand*\Split@Name[1]{
  \begingroup
    \@tempswatrue
    \expandafter\comma@parse\expandafter{#1}{\Process@Split}
  \endgroup
}

\newcommand*\Process@Authors[1]{%
  \typeout{>>>>>\comma@entry<<<<<}
  \Split@Name{\comma@entry} 
}

\ExplSyntaxOn
\cs_new_eq:NN \ST@count@authors \clist_count:n
\cs_new_eq:NN \ST@authors@access \clist_item:nn
\ExplSyntaxOff

\kv@define@key{ST}{author}{%
  \ifnum\ST@count@authors{#1}=2
    \ifnum\expandafter\ST@count@authors\expanded{{\ST@authors@access{#1}{1}}}=2
      \def\ST@author{#1}%
    \else
      \ifnum\expandafter\ST@count@authors\expanded{{\ST@authors@access{#1}{2}}}=2
        \def\ST@author{#1}%
      \else
        \def\ST@author{{#1}}%
      \fi
    \fi
  \else
    \def\ST@author{#1}%
  \fi
}
\kv@define@key{ST}{foo}{%
  % ...
}

\newcommand\Test[1][]{
  \kvsetkeys{ST}{#1}%
  \expandafter\comma@parse\expandafter{\ST@author}{\Process@Authors}
}

\begin{document}         
\Test[%
  author={{LastName1A, FirstName1A},{LastName2B,FirstName2B}},
  foo=bla
]
\Test[%
  author={LastName1C, FirstName1C},
  foo=bla
]
\end{document}

相关内容