If \printauthors is the macro I suggested in On sorting a string with multiple entries, then it's easy to add the functionality. With this version,
\printauthors
will just print the names while
\printauthors[filename.ext]
will output the same data on the specified file:
\documentclass{article}
\usepackage{xparse,l3sort,pdftexcmds}
\ExplSyntaxOn
% a macro from pdftexcmds that works with all engines
\cs_set_eq:Nc \konewka_strcmp:nn { pdf@strcmp }
\NewDocumentCommand{\addauthor}{ o m m }
{
\IfNoValueTF{#1}
{
\konewka_add_author:nnn { #3 } { #2 } { #3 }
}
{
\konewka_add_author:nnn { #1 } { #2 } { #3 }
}
}
\NewDocumentCommand{\printauthors}{ o }
{
\IfNoValueTF { #1 }
{
\konewka_print_authors:
}
{
\konewka_print_authors_to_file:n { #1 }
}
}
\seq_new:N \g_konewka_authors_id_seq
\seq_new:N \l__konewka_authors_full_seq
\iow_new:N \g_konewka_write_authors_stream
\msg_new:nnn { konewka/authors } { author~exists }
{
The ~ author ~ #1 ~ already ~ exists; ~ it ~ won't ~ be ~ added ~ again
}
\cs_new_protected:Npn \konewka_add_author:nnn #1 #2 #3
{
\prop_if_exist:cTF { g_konewka_author_#1_prop }
{
\msg_warning:nnn { konewka/authors } { author~exists } { #1 }
}
{
\seq_gput_right:Nn \g_konewka_authors_id_seq { #1 }
\prop_new:c { g_konewka_author_#1_prop }
\prop_gput:cnn { g_konewka_author_#1_prop } { fname } { #2 }
\prop_gput:cnn { g_konewka_author_#1_prop } { lname } { #3 }
}
}
\cs_new_protected:Npn \konewka_print_authors:
{
\__konewka_sort_authors:
\seq_use:Nn \l__konewka_authors_full_seq { ,~ }
}
\cs_new_protected:Npn \konewka_print_authors_to_file:n #1
{
\iow_open:Nn \g_konewka_write_authors_stream { #1 }
\__konewka_sort_authors:
\iow_now:Nx \g_konewka_write_authors_stream
{
\seq_use:Nn \l__konewka_authors_full_seq { ,~ }
}
\iow_close:N \g_konewka_write_authors_stream
}
\cs_new_protected:Npn \__konewka_sort_authors:
{
\seq_gsort:Nn \g_konewka_authors_id_seq
{
\string_compare:nnnTF {##1} {>} {##2} {\sort_reversed:} {\sort_ordered:}
}
\seq_clear:N \l__konewka_authors_full_seq
\seq_map_inline:Nn \g_konewka_authors_id_seq
{
\seq_put_right:Nx \l__konewka_authors_full_seq
{
\prop_item:cn { g_konewka_author_##1_prop } { fname }
\c_space_tl
\prop_item:cn { g_konewka_author_##1_prop } { lname }
}
}
}
\prg_new_conditional:Npnn \string_compare:nnn #1 #2 #3 {TF}
{
\if_int_compare:w \konewka_strcmp:nn {#1}{#3} #2 \c_zero
\prg_return_true:
\else:
\prg_return_false:
\fi
}
\ExplSyntaxOff
\begin{document}
\addauthor{John}{Doe}
\addauthor{Harry}{Potter}
\addauthor[Uthor]{Archibald}{\"Uthor}
\addauthor{John}{Doe}
\addauthor{Bill}{Clinton}
\addauthor{Barack}{Obama}
\printauthors
\printauthors[\jobname.txt]
\end{document}
The contents of \jobname.txt is
Bill Clinton, John Doe, Barack Obama, Harry Potter, Archibald \"Uthor
Note that I used \jobname just not to clobber any of my files.
\printauthorsthe macro I suggested? – egreg Dec 12 '14 at 13:21