# How to create a macro that manipulates the author's name?

I would like to create a macro that could take the information of the name author, usually declared in the macro \author, and break it down into three parts: name, middle name, and last name. For that, internal macros of the type \firstname, \midlename, and \lastname could be created. The macro would then be able to take this information and print it in the format last name, first name, and middle name. For example, I could declare the name \author{Alexsandro Lucena Mota} and macro, say, \newauthorname, would print in pdf document the name in the form Mota, Alexsandro Lucena.

Note: the \newauthorname macro was just a suggestion that, perhaps, was not the best. Here, the intention is to refer to the author's name in the bibliography citations, but in the form of the example provided above, that is, Mota, Alexsandro Lucena. Perhaps a better name for this macro would be \authornameinbibliographycitation, however, it is certainly very long. Feel you free to make your suggestions.

Are there any known packages or macros that are capable of doing this?

It is possible that someone still asks: "Why do you need this?"

In Brazil, in theses and dissertations, behind the cover page, we must inform the catalog data as in the example below.

So, I would like LaTeX to print the author's name in the format of the example in the figure above.

But someone might still ask: Why don't you write by hand?

And the answer is that I would like to create a package so that the user would only worry about typing his name in the macro \author and LaTeX would take care of the rest, generating the file automatically.

And finally, someone will ask for a minimal example and, for that reason, follow a code for the tasting.

\documentclass[12pt,a4paper]{memoir}
\usepackage{lastpage}
\title{Ficha catalográfica: um exemplo mínimo.}
\author{Alexsandro Lucena Mota}
\date{2020}
\begin{document}
\thispagestyle{empty}\null\vfill%

\begin{SingleSpacing}
\begin{center}
\begin{minipage}[c][][c]{13.5cm}
\begin{center}
{\small Ficha gerada por meio do SIGAA/Biblioteca
autor.}%
\end{center}
\end{minipage}
%
\begin{tabular}
[c]{|c|}\hline
\begin{minipage}[c][7.8cm][c]{13.1cm}%
\texttt{Mota, Alexsandro
Lucena}\newline\texttt{\hspace*{0.55cm}\thetitle/
\theauthor. -- \thedate}
\newline\texttt{\hspace*{0.55cm}\pageref{LastPage}
p.}
Prof. Dr. Fulando
Beltrano Silva Sauro.}
\newline\hspace{0.55cm}\texttt{Tese
do Maranhão, São Luís,
\thedate.}\newline\newline\texttt{\hspace*{0.55cm}1.
Ficha Catalográfica.~2. Exemplo Mínimo.~3.
LaTeX.~4. PDFLaTeX~I. Sauro, Fulando
Beltrano
Silva.~II. Título}
\end{minipage}%
\\\hline
\end{tabular}
\end{center}
\end{SingleSpacing}

\end{document}


The code above produces

I hope, therefore, that I have provided enough information to guide possible responses.

• Or you could use \author{\firsname \middlename \lastname} given the other macros. Aug 31 '20 at 14:54
• That was the first alternative that I thought, but, for some reason, the author's name is not printed on the PDF document. Aug 31 '20 at 20:58

The problem that I see in providing commands that extract implicit information from the \author is that there are too many corner cases to get right. Ideally, it should cope correctly with each of the following use-cases:

• \author{Alexsandro Lucena Mota}
• \author{Doe}
• \author{Jane Doe}
• \author{Jane Maria Doe}
• \author{Jane Maria Ellen Doe}
• \author{Jane Maria de Lucca}
• \author{Mota, Alexsandro Lucena}

Handling the first five cases is fairly straightforward. Case 6 requires building in knowledge of all multi-word surnames in all languages. Coping with Case 7 would be possible, but tedious. Of course, there are further problems because some languages, such as Mandarin, write names in the reverse order, with the surname first.

Ignoring all of these issues the code below defines an \Author command that sets \firstname, \middlename and \lastname and also passes its argument along to the \author command. As the image below shows, it caters for Cases 1-6, with Case 6 requiring a small amount of extra help through the addition of a ~. The \Author command does not play well with Case 7.

Here is the code:

\documentclass[12pt,a4paper]{memoir}
\parindent0pt

\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \g_firstname_tl
\tl_new:N \g_middlename_tl
\tl_new:N \g_lastname_tl
\seq_new:N \l_author_names_seq
\NewDocumentCommand\Author{m}{
\author{#1}
% clear the  first, middle and last token lists
\tl_clear:N \g_firstname_tl
\tl_clear:N \g_middlename_tl
\tl_clear:N \g_lastname_tl
% trim spaces and split the author name(s) on surrounding spaces
\regex_split:nxN {\s+} { \tl_trim_spaces:n {#1} } \l_author_names_seq
\int_case:nnF {\seq_count:N \l_author_names_seq }
{
{0} {}
{1} {\seq_pop_left:NN \l_author_names_seq \g_lastname_tl }
{2} {
\seq_pop_left:NN \l_author_names_seq \g_firstname_tl
\seq_pop_left:NN \l_author_names_seq \g_lastname_tl
}
}
{
\seq_pop_left:NN \l_author_names_seq \g_firstname_tl
\seq_pop_right:NN \l_author_names_seq \g_lastname_tl
\tl_gset:Nx \g_middlename_tl { \seq_use:Nn \l_author_names_seq {\space} }
}
}
\cs_generate_variant:Nn \regex_split:nnN { nxN }

\NewDocumentCommand\firstname{}{\tl_use:N\g_firstname_tl}
\NewDocumentCommand\middlename{}{\tl_use:N\g_middlename_tl}
\NewDocumentCommand\lastname{}{\tl_use:N\g_lastname_tl}
\NewDocumentCommand\fullauthor{}{\lastname,\space\firstname\space\middlename}
\ExplSyntaxOff

\newcommand\Names[1]{\Author{#1}\textsf{Author:} #1.\newline \textit{First}: \firstname, \textit{Middle}: \middlename, \textit{Last}: \lastname.\par\medskip}

\begin{document}

\Names{Alexsandro Lucena Mota}
\Names{Doe}
\Names{Jane Doe}
\Names{Jane Maria Doe}
\Names{Jane Maria Ellen Doe}
\Names{Jane Maria de~Lucca}

\textbf{Fails}:

\Names{Mota, Alexsandro Lucena}

\end{document}


The idea is simple enough: use LaTeX3 regular expressions to split the author names on the spaces and then set the last "name" to be the last name, any remaining first name to be the first name, after which anything left over becomes the middle name. The \Names macro is just a helper macro to show what \Author does.

The code provides a \fullauthor command that prints last name, first name middle name. I did not calls this \newauthorname because, to me, this sounds like the command that you would use to define the author, not to print it.

EDIT

As requested in the comments, here is a small variation that redefines the \author command instead of defining a new \Author command. If this is going to be used often the code in the preamble could be put into a style file, say fullauthor.sty and then included with \usepackage{fullauthor}.

\documentclass[12pt,a4paper]{article}
\parindent0pt
\let\realAuthor\author

\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \g_firstname_tl
\tl_new:N \g_middlename_tl
\tl_new:N \g_lastname_tl
\seq_new:N \l_author_names_seq
\renewcommand\author[1]
{
\realAuthor{#1}
% clear the  first, middle and last token lists
\tl_clear:N \g_firstname_tl
\tl_clear:N \g_middlename_tl
\tl_clear:N \g_lastname_tl
% trim spaces and split the author name(s) on surrounding spaces
\regex_split:nxN {\s+} { \tl_trim_spaces:n {#1} } \l_author_names_seq
\int_case:nnF {\seq_count:N \l_author_names_seq }
{
{0} {}
{1} {\seq_pop_left:NN \l_author_names_seq \g_lastname_tl }
{2} {
\seq_pop_left:NN \l_author_names_seq \g_firstname_tl
\seq_pop_left:NN \l_author_names_seq \g_lastname_tl
}
}
{
\seq_pop_left:NN \l_author_names_seq \g_firstname_tl
\seq_pop_right:NN \l_author_names_seq \g_lastname_tl
\tl_gset:Nx \g_middlename_tl { \seq_use:Nn \l_author_names_seq {\space} }
}
}
\cs_generate_variant:Nn \regex_split:nnN { nxN }

\NewDocumentCommand\firstname{}{\tl_use:N\g_firstname_tl}
\NewDocumentCommand\middlename{}{\tl_use:N\g_middlename_tl}
\NewDocumentCommand\lastname{}{\tl_use:N\g_lastname_tl}
\NewDocumentCommand\fullauthor{}{\lastname,\space\firstname\space\middlename}
\ExplSyntaxOff

\newcommand\Names[1]{\author{#1}\textsf{Author:} #1.\newline \textit{First}: \firstname, \textit{Middle}: \middlename, \textit{Last}: \lastname.\par\medskip}

\author{Alexsandro Lucena Mota}     % using the new \author command in the preamble

\begin{document}

\fullauthor

\Names{Alexsandro Lucena Mota}
\Names{Doe}
\Names{Jane Doe}
\Names{Jane Maria Doe}
\Names{Jane Maria Ellen Doe}
\Names{Jane Maria de~Lucca}

\textbf{Fails}:

\Names{Mota, Alexsandro Lucena}

\end{document}

• Your solution answers well, at least at a first moment, to the proposed problem. However, two things still bother. The first, regard to the \Author{} command. I would like to continue using the \author{} command for the sake of universal compatibility with other classes. So, what I wanted was for the \Author{} command to extract the information provided by the user in \author{}, without having to enter the information in \Author{}. Sep 1 '20 at 13:24
• The second point, and correct me if I am mistaken, is that the \Author{} command only works without errors in the document environment. I would like, instead, to use it in the preamble of the document. Sep 1 '20 at 13:24
• @lucenalex Both these points are easy to address. See my edit.
– user30471
Sep 1 '20 at 14:44
• Perfect! I will post an example answer based on your code. Sep 1 '20 at 15:20
• @lucenalex I'm note sure that I understand the rationale for that. If my code is "Perfect" the usual response is to upvote (by clicking on the arrows next to the score) and consider marking it as the accepted answer (by clicking on the checkmark ✓).
– user30471
Sep 1 '20 at 15:26

Here, I provide an answer--example based on the code from the above answer given by @Andrew.

\documentclass[12pt,a4paper]{memoir}
\parindent0pt

\usepackage{lastpage}
\usepackage{xparse}

\let\realAuthor\author

\usepackage{xparse}
\ExplSyntaxOn
%\let\__real_author:n \author
\tl_new:N \g_firstname_tl
\tl_new:N \g_middlename_tl
\tl_new:N \g_lastname_tl
\seq_new:N \l_author_names_seq
\renewcommand\author[1]
{
%\realAuthor{#1}
% clear the  first, middle and last token lists
\tl_clear:N \g_firstname_tl
\tl_clear:N \g_middlename_tl
\tl_clear:N \g_lastname_tl
% trim spaces and split the author name(s) on surrounding spaces
\regex_split:nxN {\s+} { \tl_trim_spaces:n {#1} }
\l_author_names_seq
\int_case:nnF {\seq_count:N \l_author_names_seq }
{
{0} {}
{1} {\seq_pop_left:NN \l_author_names_seq \g_lastname_tl }
{2} {
\seq_pop_left:NN \l_author_names_seq \g_firstname_tl
\seq_pop_left:NN \l_author_names_seq \g_lastname_tl
}
}
{
\seq_pop_left:NN \l_author_names_seq \g_firstname_tl
\seq_pop_right:NN \l_author_names_seq \g_lastname_tl
\tl_gset:Nx \g_middlename_tl { \seq_use:Nn
\l_author_names_seq {\space} }
}
}
\cs_generate_variant:Nn \regex_split:nnN { nxN }

\NewDocumentCommand\firstname{}{\tl_use:N\g_firstname_tl}
\NewDocumentCommand\middlename{}{\tl_use:N\g_middlename_tl}
\NewDocumentCommand\lastname{}{\tl_use:N\g_lastname_tl}
\NewDocumentCommand\theauthor{}{
\firstname\space\middlename\space\lastname}
\NewDocumentCommand\fullauthor{}{\lastname,
\space\firstname\space\middlename}
\ExplSyntaxOff

\title{Ficha catalográfica: um exemplo mínimo.}
\author{Alexsandro Lucena Mota}    % using the new \author command
%in the preamble
\date{2020}
\begin{document}
\thispagestyle{empty}\null\vfill%
\theauthor\par\fullauthor
\begin{SingleSpacing}
\begin{center}
\begin{minipage}[c][][c]{13.5cm}
\begin{center}
{\small Ficha gerada por meio do
SIGAA/Biblioteca
\end{center}
\end{minipage}
%
\begin{tabular}
[c]{|c|}\hline
\begin{minipage}[c][7.8cm][c]{13.1cm}%
\texttt{\fullauthor}\newline\texttt{\hspace*{0.55cm}\thetitle/
\theauthor. -- \thedate}
\newline\texttt{\hspace*{0.55cm}\pageref{LastPage}
p.}
Prof. Dr. Fulando
Beltrano Silva Sauro.}
\newline\hspace{0.55cm}\texttt{Tese
do Maranhão, São Luís,
\thedate.}\newline\newline\texttt{\hspace*{0.55cm}1.
Ficha Catalográfica.~2. Exemplo
Mínimo.~3.
LaTeX.~4. PDFLaTeX~I. Sauro, Fulando
Beltrano
Silva.~II. Título}