在 LaTeX3 中查找并存储作者名字、姓名首字母和姓氏值

在 LaTeX3 中查找并存储作者名字、姓名首字母和姓氏值

我想将每个作者和值存储firstnameinitialsnewcommandslastname中。我的 MWE 是:

\documentclass{article}
\usepackage{xparse}
\makeatletter
\def\author#1{\gdef\@author{#1}
\getfirstpart\firstname{#1}
%\getinitial\initials{#1}
\getlastpart\lastname{#1}}
\makeatother

\ExplSyntaxOn
\NewDocumentCommand{\getfirstpart}{mm}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
  \seq_pop_left:NN \l_tmpa_seq #1
 }

\NewDocumentCommand{\getlastpart}{mm}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #2 }
  \seq_pop_right:NN \l_tmpa_seq #1
 }
\ExplSyntaxOff

\begin{document}
\author[[email protected], web="http://www.tex.stackexchange.com", address=Department of Mathematics, UK]{John Smith} %% mail value should store \mail{[email protected]}, web value should store in \web{http://www.tex.stackexchange.com} and address value should store \address{Department of Mathematics, UK}

\firstname and \lastname

\author{John X. Smith}
\firstname and \lastname %%how do store initial value?

\author{Smith}

\author{Brian H.-K. Lly}

\end{document}

答案1

对于标准的美国名字,“Firstname I. Surname”自动化是可能的。以下是使用此功能的一些代码,但可以针对不同情况提供帮助:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\keys_define:nn { balaji/author }
 {
  mail .tl_set:N = \l_balaji_mail_tl,
  mail .initial:n = {},
  web .tl_set:N = \l_balaji_web_tl,
  web .initial:n = {},
  address .tl_set:N = \l_balaji_address_tl,
  address .initial:n = {},
  firstname .tl_set:N = \l_balaji_firstname_tl,
  firstname .initial:n = {},
  initials .tl_set:N = \l_balaji_initials_tl,
  initials .initial:n = {},
  lastname .tl_set:N = \l_balaji_lastname_tl,
  lastname .initial:n = {},
  fullname .tl_set:N = \l_balaji_fullname_tl,
  key .tl_set:N = \l_balaji_key_tl,
  key .initial:n = {},
 }
\NewDocumentCommand\xauthor{ O{} m }
 {
  \group_begin:
  \keys_set:nn { balaji/author } { #1 }
  \keys_set:nn { balaji/author } { fullname = #2 }
  \balaji_define_author:n { #2 }
  \group_end:
 }

\cs_new_protected:Npn \balaji_define_author:n #1
 {
  \tl_if_empty:NT \l_balaji_lastname_tl
   {
    \seq_set_split:Nnn \l_balaji_author_seq { ~ } { #1 }
    \seq_pop_right:NN \l_balaji_author_seq \l_balaji_lastname_tl
    \seq_pop_left:NN \l_balaji_author_seq \l_balaji_firstname_tl
    \tl_set:Nx \l_balaji_initials_tl { \seq_use:Nn \l_balaji_author_seq { ~ } }
   }
  \tl_if_empty:NT \l_balaji_key_tl
   { \tl_set_eq:NN \l_balaji_key_tl \l_balaji_lastname_tl }
  \seq_gput_right:NV \g_balaji_author_list_seq \l_balaji_key_tl
  \prop_new:c { g_balaji_author_ \l_balaji_key_tl _prop }
  \balaji_populate:n { mail, web, address, firstname, initials, lastname, fullname }
 }

\cs_new_protected:Npn \balaji_populate:n #1
 {
  \clist_map_inline:nn { #1 }
   {
    \prop_gput:cnv { g_balaji_author_ \l_balaji_key_tl _prop } 
     { ##1 }
     { l_balaji_##1_tl }
   }
  %\prop_show:c { g_balaji_author_ \l_balaji_key_tl _prop } % for debugging
 }

\cs_generate_variant:Nn \prop_gput:Nnn { cnv }
\seq_new:N \l_balaji_author_seq
\seq_new:N \g_balaji_author_list_seq

\NewDocumentCommand{\getauthorfield} { m m }
 {
  \prop_item:cn { g_balaji_author_#1_prop } { #2 }
 }

\ExplSyntaxOff

\begin{document}

\xauthor{John X. Smith}

\xauthor[
  key=HK,
  web=http://x.y.z,
  firstname=Brian,
  lastname=Hamilton Kelly
]{Brian Hamilton Kelly}

\xauthor[
  key=VP,
  firstname=Charles,
  initials={L. X. J.},
  lastname={de la Vall\'ee Poussin},
]{Charles Louis Xavier Joseph de la Vall\'ee Poussin}

\getauthorfield{Smith}{firstname} \getauthorfield{Smith}{lastname}

\getauthorfield{HK}{web}

\end{document}

如果名字“复杂”,则可以且应该添加一个键;如果同一个姓氏出现多次,则添加键。对于简单的名字,键设置为姓氏。

我设置了一个包含键的全局序列,因此可以映射它以检索信息。当然还有很多事情要做。

KEY属性列表 中包含了作者的信息,其键为\g_balaji_author_KEY_prop。可以通过 检索单个字段\getauthorfield{KEY}{<field name>}。可以轻松扩展字段集。

相关内容