列出作者的技巧

列出作者的技巧

我写了以下代码:

\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

也就是说,我可以用命令添加作者\authorAdd{},然后用命令\printauthor列出所有作者,用逗号分隔。我想用单词“and”替换最后一个逗号。我该怎么做?谢谢

答案1

具有分割功能的“传统”方式\@elt

计数器重置列表使用\@elt分割,即应按计数器重置的计数器foo按以下方式存储:

foobar\@elt otherfoobar\@elt. 该列表被命名cl@foo并在每次执行步进命令时被调用,而是\@elt一个“易失性”宏,被定义为执行某些操作。

这里可以应用相同的方法,使用\@elt而不是,作为名称分隔符。

\printauthor命令只是使用\the\author@toks并定义一个特殊的\@elt(在一个组中),最终显示正确,and分隔符。

\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- 分裂方式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}

在此处输入图片描述

相关内容