TransWikia.com

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

TeX - LaTeX Asked on June 4, 2021

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.

enter image description here

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}nullvfill%

begin{SingleSpacing}   
    begin{center}
        begin{minipage}[c][][c]{13.5cm}
            begin{center}
                {small Ficha gerada por meio do SIGAA/Biblioteca 
                com dados fornecidos pelo
                    autor.}%
                {small N'ucleo Integrado de Bibliotecas/UFMA}%
            end{center}
        end{minipage}
        %      
        begin{tabular}
            [c]{|c|}hline
            begin{minipage}[c][7.8cm][c]{13.1cm}%
                begin{adjustwidth}{0.5cm}{0.0cm}%
                    texttt{Mota, Alexsandro 
                    Lucena}newlinetexttt{hspace*{0.55cm}thetitle/
                        theauthor. -- thedate} 
                        newlinetexttt{hspace*{0.55cm}pageref{LastPage}
                        p.} 
                        newlinenewlinetexttt{hspace*{0.55cm}Orientador:
                         Prof. Dr. Fulando
                        Beltrano Silva Sauro.} 
                        newlinehspace{0.55cm}texttt{Tese
                        (doutorado)~--~Programa de Pós-Graduação em 
                        Física/ccet, Universidade Federal
                        do Maranhão, São Luís, 
                        thedate.}newlinenewlinetexttt{hspace*{0.55cm}1.
                        Ficha Catalográfica.~2. Exemplo Mínimo.~3. 
                        LaTeX.~4. PDFLaTeX~I. Sauro, Fulando 
                        Beltrano 
                        Silva.~II. Título}
                end{adjustwidth}%
            end{minipage}%
            hline
        end{tabular}        
    end{center}
end{SingleSpacing}

end{document}

The code above produces

enter image description here

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

2 Answers

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.

enter image description here

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
NewDocumentCommandAuthor{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 }

NewDocumentCommandfirstname{}{tl_use:Ng_firstname_tl}
NewDocumentCommandmiddlename{}{tl_use:Ng_middlename_tl}
NewDocumentCommandlastname{}{tl_use:Ng_lastname_tl}
NewDocumentCommandfullauthor{}{lastname,spacefirstnamespacemiddlename}
ExplSyntaxOff

newcommandNames[1]{Author{#1}textsf{Author:} #1.newline textit{First}: firstname, textit{Middle}: middlename, textit{Last}: lastname.parmedskip}

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
letrealAuthorauthor

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
renewcommandauthor[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 }

NewDocumentCommandfirstname{}{tl_use:Ng_firstname_tl}
NewDocumentCommandmiddlename{}{tl_use:Ng_middlename_tl}
NewDocumentCommandlastname{}{tl_use:Ng_lastname_tl}
NewDocumentCommandfullauthor{}{lastname,spacefirstnamespacemiddlename}
ExplSyntaxOff

newcommandNames[1]{author{#1}textsf{Author:} #1.newline textit{First}: firstname, textit{Middle}: middlename, textit{Last}: lastname.parmedskip}

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}

Correct answer by user30471 on June 4, 2021

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}

letrealAuthorauthor

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
renewcommandauthor[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 }

NewDocumentCommandfirstname{}{tl_use:Ng_firstname_tl}
NewDocumentCommandmiddlename{}{tl_use:Ng_middlename_tl}
NewDocumentCommandlastname{}{tl_use:Ng_lastname_tl}
NewDocumentCommandtheauthor{}{ 
firstnamespacemiddlenamespacelastname}
NewDocumentCommandfullauthor{}{lastname, 
spacefirstnamespacemiddlename}
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}nullvfill%
    theauthorparfullauthor
    begin{SingleSpacing}   
        begin{center}
            begin{minipage}[c][][c]{13.5cm}
                begin{center}
                    {small Ficha gerada por meio do 
                    SIGAA/Biblioteca  
                        com dados fornecidos pelo autor.par% 
                        N'ucleo Integrado de Bibliotecas/UFMA}%
                end{center}
            end{minipage}
            %      
            begin{tabular}
                [c]{|c|}hline
                begin{minipage}[c][7.8cm][c]{13.1cm}%
                    begin{adjustwidth}{0.5cm}{0.0cm}%
                        texttt{fullauthor}newlinetexttt{hspace*{0.55cm}thetitle/
                            theauthor. -- thedate} 
                        newlinetexttt{hspace*{0.55cm}pageref{LastPage}
                            p.} 
                        newlinenewlinetexttt{hspace*{0.55cm}Orientador:
                            Prof. Dr. Fulando
                            Beltrano Silva Sauro.} 
                        newlinehspace{0.55cm}texttt{Tese
                            (doutorado)~--~Programa de 
                            Pós-Graduação em 
                            Física/ccet, Universidade Federal
                            do Maranhão, São Luís, 
                            thedate.}newlinenewlinetexttt{hspace*{0.55cm}1.
                            Ficha Catalográfica.~2. Exemplo 
                            Mínimo.~3. 
                            LaTeX.~4. PDFLaTeX~I. Sauro, Fulando 
                            Beltrano 
                            Silva.~II. Título}
                    end{adjustwidth}%
                end{minipage}%
                hline
            end{tabular}        
        end{center}
    end{SingleSpacing}
    
end{document}

This code produces exactly the hoped answer.

enter image description here

Answered by lucenalex on June 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP