3

I'm wondering if it's possible to count the number of authors set in the \author{} macro. I would need this to modify the output of my titlepage. I'm currently hardcoding the following:

\emph{Author:}\\
\begin{tabular}[t]{c}%
    \@author
\end{tabular}\par%

This prints "Author" no matter if there are set multiple authors or not. This should be changed by counting the authors passed to the macro and then modifying the output based on the results.

I would say that the key in counting the authors is in the \and statement, but unfortunately I don't know how to split the string at this keyword.

6
  • 2
    It depends on how you populate \@author
    – egreg
    Sep 12, 2017 at 16:24
  • \author{John Doe \and Jane Doe}
    – Sam
    Sep 12, 2017 at 16:25
  • If it is, as I suppose, for your previous post, please take a look at a solution I've just posted: it doesn't require counting whatever.
    – Bernard
    Sep 12, 2017 at 17:04
  • @Bernard Yes, it if for that previous post, but I need the counting to modify the title. It should say "Author:" if there is only one author and "Authors:" if there are more. That's why I need to count them.
    – Sam
    Sep 13, 2017 at 15:34
  • Why don't you define a \authors command which you use when there are several authors?
    – Bernard
    Sep 13, 2017 at 15:42

2 Answers 2

2

In the comments you confirmed that you gather the authors of your document in the standard way. In this case you could use delimited arguments to get the actual number of authors (as a direct answer to your question):

\newcounter{authors}
\stepcounter{authors}
\def\count@and#1\and#2{%
  \ifx#2\@nil\else
    \stepcounter{authors}\expandafter\count@and\fi
}

Now, if you use \count@and...\and...\and...\and\@nil, \theauthors will hold the number of elements seperated by \and. You can include this to count the authors defined by \author with the following patch.

\let\latex@author\author
\def\author#1{%
  \count@and#1\and\@nil
  \latex@author{#1}
}

In a similar fashion to @egreg's answer you could say:

\xpatchcmd{\@maketitle}
  {\begin{tabular}}
  {\author@or@authors\begin{tabular}}
  {}{}
\def\author@or@authors{\textit{Author\ifnum\theauthors>1s\fi:}\\}

And here is the complete code:

\documentclass{article}
\usepackage{xpatch}

\makeatletter
\newcounter{authors}
\stepcounter{authors}
\def\count@and#1\and#2{%
  \ifx#2\@nil\else
    \stepcounter{authors}\expandafter\count@and\fi
}
\let\latex@author\author
\def\author#1{%
  \count@and#1\and\@nil
  \latex@author{#1}
}
\xpatchcmd{\@maketitle}
  {\begin{tabular}}
  {\author@or@authors\begin{tabular}}
  {}{}
\def\author@or@authors{\textit{Author\ifnum\theauthors>1s\fi:}\\}
\makeatother

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

\begin{document}
\maketitle\theauthors
\end{document}
1
  • 1
    That's a really cool idea. My problem was however solved by this answer to another question of mine. I not only wanted to calculate the authors (even if that was what I was asking for here), but I also wanted to change their displaying. That said I'll mark your answer as accepted, because I think it's a great method for people who only want to count the authors.
    – Sam
    Sep 21, 2017 at 16:13
2

I don't know how you are modifying \@maketitle, so I make just the change you tell about; the idea is to add \author@or@authors instead of \emph{Author:}\\, with the following definition for the auxiliary macro:

\documentclass{article}
\usepackage{xpatch,xstring}

\makeatletter
\xpatchcmd{\@maketitle}
 {\begin{tabular}}
 {\author@or@authors\begin{tabular}}
 {}{}

\newcommand\author@or@authors{%
  \emph{Author%
    \expandafter\IfSubStr\expandafter{\@author}{\and}{s}{}%
  :}\\%
}
\noexpandarg
\makeatother

\begin{document}

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

\maketitle

\end{document}

enter image description here

2
  • How can I integrate this in my new definition of \@maketitle (without the \xpatchcmd) and have the multiple authors be displayed one below the other. (See here)
    – Sam
    Sep 13, 2017 at 15:44
  • PS: I redefined the \@maketitle macro as seen here
    – Sam
    Sep 13, 2017 at 20:31

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.