# Trick for listing authors

I wrote the following code:

\makeatletter
\newtoks\author@toks\newcounter{author@counter}%
\setcounter{author@counter}{0}%
\ifnum\theauthor@counter>0\author@toks=\expandafter{\the\author@toks,    #1}%
\else\author@toks=\expandafter{\the\author@toks\@author{#1}}%
\fi\stepcounter{author@counter}%
}
\newcommand{\printauthor}{{\sc Authors:}\the\authors@toks}
\makeatother


that is, I can add an author with the command \authorAdd{} and then the command \printauthor list all the author, separating them with a comma. I would like tu replace the last comma with the word "and". How can I do? Thanks

• You could make an optional argument, or starred variant, for \authorAdd to specify the last in the list. May 4, 2016 at 16:28
• Please do not use \sc -- that command is deprecated. Use \scshape or \textsc
– user31729
May 4, 2016 at 16:32

A 'traditional' way with the \@elt splitting feature:

Counter reset lists use the \@elt splitting, i.e. the counters that should be reset by foo counter are stored this way:

foobar\@elt otherfoobar\@elt. The list is named cl@foo and is called each time in stepping command, whereas \@elt is a 'volatile' macro, being defined to do some action.

The same approach can be applied here, using \@elt instead of , as name separator.

The \printauthor command just uses \the\author@toks and defines a special \@elt (in a group) that displays the correct , or and separator finally.

\documentclass{article}

\makeatletter
\newtoks\author@toks\newcounter{author@counter}%
\setcounter{author@counter}{0}%
\ifnum\theauthor@counter>0\author@toks=\expandafter{\the\author@toks\@elt\relax #1}%
\else\author@toks=\expandafter{\the\author@toks\@author{#1}}%
\fi\stepcounter{author@counter}%
}

\newcounter{dummycounter}
\newcommand{\printauthor}{%
\textsc{Authors:}%
\begingroup
\setcounter{dummycounter}{0}%
\def\@elt##1{%
\ifnum\c@dummycounter < \numexpr\c@author@counter - 2\relax%
,
\else
{ and }%
\fi
\stepcounter{dummycounter}%
}
\the\author@toks%
\endgroup
}

\makeatother

\begin{document}

\printauthor
\end{document}


A \clist - splitting way with expl3.

\documentclass{article}

\usepackage{xparse}

\makeatletter
\newtoks\author@toks\newcounter{author@counter}%
\setcounter{author@counter}{0}%
\ifnum\theauthor@counter>0\author@toks=\expandafter{\the\author@toks,    #1}%
\else\author@toks=\expandafter{\the\author@toks\@author{#1}}%
\fi\stepcounter{author@counter}%
}
\newcommand{\printauthor}{{\sc Authors:}\the\author@toks}

\ExplSyntaxOn

\newcommand{\splitauthorlist}[1]{%
\clist_set:Nx \l_tmpa_clist {#1}
\clist_use:Nnnn \l_tmpa_clist {,\space} {,\space} {\space and\space}
}
\ExplSyntaxOff

\newcommand{\printauthors}{%
{\scshape Authors}:
\splitauthorlist{\the\author@toks}
}
\makeatother

\begin{document}

\printauthors
\end{document}


• I'm trying to do a thing similar to the 'traditional' way around. The point is that I don't have to print the name of the author, but I have to store the list (the would-be-printed list: names, separated with commas and the word "end") in a token. How can I do?
– MaPo
May 4, 2016 at 23:05
• @MaPo: I am not sure that I understand you in the full range. What's the purpose of \printauthor then? What should this list do then?
– user31729
May 4, 2016 at 23:08
• I'm trying to merge this issue to tex.stackexchange.com/questions/307916/… but I'm not able to find how. I'll add a post here with my example code
– MaPo
May 4, 2016 at 23:13
• @MaPo: That's another question again -- neither the tricky expansion nor this question should be edited. Use a new one and provide links to your questions
– user31729
May 4, 2016 at 23:15
• The new question is tex.stackexchange.com/questions/307943/…
– MaPo
May 4, 2016 at 23:21