0

My case is very similar to this problem: I've got a list of pairs (first name, last name), and I want to process it to make last name in \scshape. It works fine with @egreg's solution. But when I want to first store the list in a macro, it is not processed any more… The aim is to put the \Bar command in the preamble and process the list later in the document.

I've seen codes with expl3 syntax (for example here, still by @egreg!), but I really don't understand it :(

Any help appreciated!

To go further is it possible to handle the case where the first name is not provided?

\documentclass{article}
\usepackage{xparse}

\newcommand{\Bar}[1]{\def\bar{#1}}

\NewDocumentCommand{\foo}{>{\SplitList{;}}m}{% \def\respdelim{\def\respdelim{, }}% \def\aze##1{\respdelim\fooaux{##1}}% \ProcessList{#1}{\aze}% } \NewDocumentCommand{\fooaux}{>{\SplitArgument{1}{,}}m}{\fooauxa#1} \NewDocumentCommand{\fooauxa}{>{\TrimSpaces}m>{\TrimSpaces}m}{\IfValueTF{#1}{\IfValueTF{#2}{#1\space{\scshape #2}}{#1}}{\IfValueTF{#2}{{\scshape#2}}}}

\begin{document} \foo{x,1;y;z,2}

\foo{First Name1, Last Name1; First Name2, Last Name2; First Name3; ,Last Name4}

\Bar{First Name1, Last Name1; First Name2, Last Name2; First Name3; ,Last Name4} % store list in \bar
\foo{\bar} % process \bar

\end{document}

NBur
  • 4,326
  • 10
  • 27

1 Answers1

0

I'd take a different approach, defining a \usenames command that takes as argument a template.

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\IfNotEmpty}{mm} { \tl_if_empty:nF {#1} { #2 } }

\NewDocumentCommand{\addnames}{m} { \nbur_names:n { #1 } }

\NewDocumentCommand{\usenames}{+m} { \cs_set:Nn __nbur_names_process:nn { #1 } __nbur_names_process: }

\seq_new:N \g_nbur_names_seq \seq_new:N \l__nbur_names_temp_seq \cs_new:Nn __nbur_names_process:nn { } \cs_generate_variant:Nn __nbur_names_process:nn { ee }

\cs_new_protected:Nn \nbur_names:n { \seq_set_split:Nnn \l__nbur_names_temp_seq { ; } { #1 } \seq_map_inline:Nn \l__nbur_names_temp_seq { \seq_gput_right:Nn \g_nbur_names_seq { ##1 } } }

\cs_new_protected:Nn __nbur_names_process: { \seq_map_inline:Nn \g_nbur_names_seq { \seq_set_split:Nnn \l__nbur_names_temp_seq { , } { ##1 } __nbur_names_process:ee { \seq_item:Nn \l__nbur_names_temp_seq { 1 } }% first name { \seq_item:Nn \l__nbur_names_temp_seq { 2 } }% last name } }

\ExplSyntaxOff

\begin{document}

\addnames{First Name1, Last Name1; First Name2, Last Name2; First Name3; ,Last Name4}

\usenames{\IfNotEmpty{#1}{#1 }\IfNotEmpty{#2}{\textsc{#2}}\par}

\end{document}

enter image description here

In the template, #1 stands for the first name and #2 for the last name.

egreg
  • 1,121,712
  • 1
    expl3 remains weird to me… I'm going to play with this code, trying to understand it. – NBur Mar 04 '22 at 17:01