3

With xparse it's pretty easy to split a list at each occurrence of a token. In my MWE I set up a new command \mylist in which I passes two names separated by \and. The xparse script splits the list at the \and token and outputs the two names under each other.

Is there a possibility to use this script with the \@author macro. I'd like to print the authors passed to \author{} one after the other in the \maketitle command. To make sure that LaTeX can normally use the author-command where I used to, I don't want to use the example code (underneath the MWE).


MWE

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\mylist{>{\SplitList{\and}}m}
    {
    \ProcessList{#1}{\insertitem}
    }
\newcommand\insertitem[1]{#1\\}

\begin{document}

\noindent \emph{Authors:}\\
\mylist{John Doe \and Jane Doe}

\end{document}

Author Example

...
\RenewDocumentCommand\author{>{\SplitList{\and}}m}
...
\author{John Doe \and Jane Doe}
...

Count

I have set up a counter and add 1 for every item, so at the end I know how many authors were passed to the list. I then check with a conditional if the value of the counter is greater than 1. With this I could decide if Author or Authors should be output before the names. This does not work at the moment because I need the result before the array is defined. If I could use the default LaTeX \author command, this problem would not occur, because the \author command is populated in the preamble. Then the \maketitle shoulr prints out the authors as shown in the MWE.

\documentclass{article}
\usepackage{xparse}
\RenewDocumentCommand\author{>{\SplitList{\and}}m}
    {
    \ProcessList{#1}{\insertitem}
    }

\newcounter{numberOfAuthors}

\newcommand\insertitem[1]{%
    #1%
    \newline%
    \addtocounter{numberOfAuthors}{1}
}

\makeatletter
\newcommand\checkIfGreatherThan{%
    \ifnum\c@numberOfAuthors>1
        \noindent There are \thenumberOfAuthors\ authors given. The output should be \emph{Authors:}
    \else 
        \noindent There is only one author given. The output should be \emph{Author:}
  \fi}
\makeatother

\begin{document}

\noindent \emph{Authors:}\\
% uncomment one of the two commands to test
% \author{John Doe \and Jane Doe}
% \author{John Doe}

\checkIfGreatherThan

\end{document}
3

Since both \author and \@author are macros that are 'volatile', i.e. \let to \relax after usage of \maketitle, it is better to store the content of the argument of \author to another macro, which is evaluated later on.

The \and macro is 'complicated' in the sense that it is actually closing a tabular environment and starting another one. I decided to redefine \and to expand to , and use the expl3 macro \seq_set_from_clist:Nx in order to split the list into the individual names.

\documentclass{article}
\usepackage{xparse}



\ExplSyntaxOn

\cs_generate_variant:Nn \seq_set_from_clist:Nn {No,Nx}

\NewDocumentCommand{\myinternalsplitter}{m}{%
  \group_begin:
  \seq_set_from_clist:Nx \l_tmpa_seq {#1}% Split the expanded list, i.e. `\and` will place `,` 
  \int_compare:nNnTF { \seq_count:N \l_tmpa_seq } = {1} {% Count the number of items
    \displayauthorhead
  }{
    \displayauthorshead
  }
  \seq_map_inline:Nn \l_tmpa_seq {\insertitem{##1}}% Display the list with `\insertitem
  \group_end:
}

\ExplSyntaxOff


\makeatletter

\let\latex@@author\author

\RenewDocumentCommand{\author}{m}{%
  \latex@@author{#1}% Use the old command
  \gdef\@@authorlist{#1}%
}

\NewDocumentCommand{\displayauthorhead}{}{%
  Author: 

}

\NewDocumentCommand{\displayauthorshead}{}{%
  Authors:

}


\NewDocumentCommand\mylist{}{%
  \begingroup
  \def\and{,}
  \edef\@authorlist{\@@authorlist}
  \myinternalsplitter{\@authorlist}
  \endgroup
}
\makeatother

\newcommand\insertitem[1]{%
  #1

}


\author{John Doe \and Jane Doe \and John Hannibal Smith \and B.A. Baracus}

\begin{document}
\title{Foo}

\mylist%


\maketitle


\end{document}

enter image description here

  • Wow, that's awesome! Thank you very much Christian! I still have a little question. How can I align the output to the right? I tried with \begin{flushright} and with a right-aligned tabularx environment, but neither of them worked. Thank you very much in advance! PS: I need your code for the \author command and for a self defined \supervisor command. The authors should be left aligned and the supervisors right aligned, so the alignment shouldn't be hard coded to the script. It would be better if the output would respect the flushright environment. – Sam Sep 17 '17 at 20:13
  • @Sam: I'll check later on... I have no time right now ... – user31729 Sep 18 '17 at 14:04
  • No problem, take your time :) – Sam Sep 19 '17 at 13:43
  • I solved the issue myself. While cleaning up my code I removed your empty lines in \NewDocumentCommand{\displayauthorhead}, \NewDocumentCommand{\displayauthorshead} and \newcommand\insertitem and replaced it by \newline. I now reverted it back to the empty line and it works fine. Is there a more clean solution than an empty line? I tried with `\` which resulted in over 100 errors in my case... – Sam Sep 19 '17 at 14:44
  • All fine, \linebreak did the job. – Sam Sep 19 '17 at 15:01
0

As you linked this question in another one of yours with a similar scope you could use the same technique that I employed there to solve it (delimited arguments) as an alternative to xparse:

\documentclass{article}
\usepackage{xpatch}

\makeatletter
\newcounter{authors}
\stepcounter{authors}
\def\@author{\textit{Author\ifnum\theauthors>1s\fi:}\\}
\def\split@and#1\and#2{%
  \g@addto@macro\@author{#1\\}%
  \ifx#2\@nil\else
    \stepcounter{authors}%
    \expandafter\split@and\expandafter#2\fi
}
\def\author#1{\split@and#1\and\@nil}
\makeatother

\title{title}
\author{John Doe}%\and Jane Doe}

\begin{document}
\maketitle
\end{document}

example1_compiled

example2_compiled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.