5

I wrote the following code:

\makeatletter
 \newtoks\author@toks\newcounter{author@counter}%
 \setcounter{author@counter}{0}%
 \newcommand{\authorAdd}[1]{%
 \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

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

1 Answer 1

4

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}%
 \newcommand{\authorAdd}[1]{%
 \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}
\authorAdd{Shakespeare}
\authorAdd{Tolkien}
\authorAdd{Groucho Marx}
\authorAdd{Harpo Marx}
\authorAdd{Gummo Marx}
\authorAdd{Zeppo Marx}
\authorAdd{Chico Marx}

\printauthor
\end{document}

A \clist - splitting way with expl3.

\documentclass{article}

\usepackage{xparse}




\makeatletter
 \newtoks\author@toks\newcounter{author@counter}%
 \setcounter{author@counter}{0}%
 \newcommand{\authorAdd}[1]{%
 \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}
\authorAdd{Shakespeare}
\authorAdd{Tolkien}
\authorAdd{Groucho Marx}

\printauthors
\end{document}

enter image description here

5
  • 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
    Commented 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
    Commented 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
    Commented 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
    Commented May 4, 2016 at 23:15
  • The new question is tex.stackexchange.com/questions/307943/…
    – MaPo
    Commented May 4, 2016 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .