4

First a warning. This is a highly specific and idiosyncratic problem I have that will only interest a few people who are very invested in biblatex and/or regular expressions.

From the answer to Placement of Jr. and Sr. with biblatex, I'm using the following code:

\renewbibmacro{name:last-first}[4]{% put name affixes like 'Jr.' after the name
    \usebibmacro{name:delim}{#3#1}%
    \ifblank{#3}{#1}{#3 #1}\addcomma\addspace
    #2\isdot
    \ifblank{#4}{}{\addcomma\addspace #4}\isdot
}

And then from the answer to Abbreviating a name in biblatex when first name is missing, I'm using this code:

\DeclareStyleSourcemap{
    \maps[datatype = bibtex]{
        \map{%% Make sure a field like [Adam John Smith] comes out as [Smith, Adam John]
            \step[fieldsource = author,
                match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z},
                replace = {[$2, $1]}]
    }
        \map{%% Abbreviate an entry such as Adam J[ohn] Smith to Smith, Adam J.
            \step[fieldsource = author,
                match = \regexp{(\w+)\[(.+?)\]},
                replace ={$1.}]
    }
        \map{%% Abbreviate an entry such as [Adam John] Smith to Smith
            \step[fieldsource = author,
                notmatch = \regexp{\A\[(.+)\]\Z},
                final]
            \step[fieldsource = author,
                match = \regexp{(\A|\,\s)\[(.+?)\]},
                replace = {$1}]
    }
  }
}

These two code snippets don't work perfectly together, as the first code snippet causes the second code snippet to retain or include a comma after a bracketed first name as in [John] Lennon has been removed and replaced by Lennon. In the MWE below, instead of printing Lennon (1974), it prints Lennon, (1974). If you comment out the code that deals with name affixes like Jr., then the desired output Lennon (1974) appears. So the question is, how should the code be modified so that it puts name affixes where I want them while at the same time it doesn't add the comma when I remove bracketed first names?

\documentclass{article}
\usepackage[style = authoryear-comp]{biblatex}
\renewbibmacro{name:last-first}[4]{% put name affixes like 'Jr.' after the name
    \usebibmacro{name:delim}{#3#1}%
    \ifblank{#3}{#1}{#3 #1}\addcomma\addspace
    #2\isdot
    \ifblank{#4}{}{\addcomma\addspace #4}\isdot
}
\DeclareStyleSourcemap{
    \maps[datatype = bibtex]{
        \map{%% Make sure a field like [Adam John Smith] comes out as [Smith, Adam John]
            \step[fieldsource = author,
                match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z},
                replace = {[$2, $1]}]
    }
        \map{%% Abbreviate an entry such as Adam J[ohn] Smith to Smith, Adam J.
            \step[fieldsource = author,
                match = \regexp{(\w+)\[(.+?)\]},
                replace ={$1.}]
    }
        \map{%% Abbreviate an entry such as [Adam John] Smith to Smith
            \step[fieldsource = author,
                notmatch = \regexp{\A\[(.+)\]\Z},
                final]
            \step[fieldsource = author,
                match = \regexp{(\A|\,\s)\[(.+?)\]},
                replace = {$1}]
    }
  }
}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@BOOK{lennon1970,
    AUTHOR = "J[ohn] Lennon",
    TITLE = "My life with the Beatles",
    YEAR = "1970",
    SORTNAME = "John Lennon"}
@BOOK{lennon1971,
    AUTHOR = "[John Lennon]",
    TITLE = "Moving on",
    YEAR = "1971",
    SORTNAME = "John Lennon"}
@BOOK{lennon1972,
    AUTHOR = "[J. John Lennon]",
    TITLE = "Moving further on",
    YEAR = "1972",
    SORTNAME = "John Lennon"}
@BOOK{lennon1973,
    AUTHOR = "John Lennon",
    TITLE = "Still moving on",
    YEAR = "1973",
    SORTNAME = "John Lennon"}
@BOOK{lennon1974,
    AUTHOR = "[John] Lennon",
    TITLE = "I'm out of here",
    YEAR = "1974",
    SORTNAME = "John Lennon"}
@BOOK{lennon1975,
    AUTHOR = "Lennon, [John]",
    TITLE = "I'm out of here",
    YEAR = "1975",
    SORTNAME = "John Lennon"}
@BOOK{beatles1970,
    AUTHOR = "John W[inston] Lennon and J[ames] Paul McCartney",
    TITLE = "Let it be",
    YEAR = "1970"}
@BOOK{gauch2012,
    AUTHOR = "Gauch, Jr., Hugh G.",
    TITLE = "Scientific method in brief",
    YEAR = "2012",
    LOCATION = "Cambridge",
    PUBLISHER = "Cambridge University Press"}
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}

\nocite{*}
\printbibliography
\end{document}

enter image description here

3

When writing name macros, I think it always safer to take the standard definition (from biblatex.def) and apply one's modifications carefully. The standard definitions think of so many things I, for one, might not have considered.

In this case here, name:last-first did not fare well with names only consisting of one component such as "Aristotle", "Plato", or after our treatment "Lennon". In those cases it still added a comma in joyful anticipation of a first name that did not follow.

The macro below is just name:last-first from biblatex.def with a few lines shuffled around. The \ifblank{#4}{}{...} lines went to the end. Furthermore those were enhanced with a further \revsdnamepunct, to set off the Jr part. (Maybe it would have been more fitting to create a new \bibnameaffixpunct and use that instead)

\renewbibmacro*{name:last-first}[4]{%
  \ifuseprefix
    {\usebibmacro{name:delim}{#3#1}%
     \usebibmacro{name:hook}{#3#1}%
     \ifblank{#3}{}{%
       \ifcapital
         {\mkbibnameprefix{\MakeCapital{#3}}\isdot}
         {\mkbibnameprefix{#3}\isdot}%
       \ifpunctmark{'}{}{\bibnamedelimc}}%
     \mkbibnamelast{#1}\isdot
     \ifblank{#2}{}{\revsdnamepunct\bibnamedelimd\mkbibnamefirst{#2}\isdot}%
     \ifblank{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnameaffix{#4}\isdot}}
    {\usebibmacro{name:delim}{#1}%
     \usebibmacro{name:hook}{#1}%
     \mkbibnamelast{#1}\isdot
     \ifblank{#2#3}{}{\revsdnamepunct}%
     \ifblank{#2}{}{\bibnamedelimd\mkbibnamefirst{#2}\isdot}%
     \ifblank{#3}{}{\bibnamedelimd\mkbibnameprefix{#3}\isdot}%
     \ifblank{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnameaffix{#4}\isdot}}}

MWE

\documentclass{article}
\usepackage[style = authoryear-comp]{biblatex}


\renewbibmacro*{name:last-first}[4]{%
  \ifuseprefix
    {\usebibmacro{name:delim}{#3#1}%
     \usebibmacro{name:hook}{#3#1}%
     \ifblank{#3}{}{%
       \ifcapital
         {\mkbibnameprefix{\MakeCapital{#3}}\isdot}
         {\mkbibnameprefix{#3}\isdot}%
       \ifpunctmark{'}{}{\bibnamedelimc}}%
     \mkbibnamelast{#1}\isdot
     \ifblank{#2}{}{\revsdnamepunct\bibnamedelimd\mkbibnamefirst{#2}\isdot}%
     \ifblank{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnameaffix{#4}\isdot}}
    {\usebibmacro{name:delim}{#1}%
     \usebibmacro{name:hook}{#1}%
     \mkbibnamelast{#1}\isdot
     \ifblank{#2#3}{}{\revsdnamepunct}%
     \ifblank{#2}{}{\bibnamedelimd\mkbibnamefirst{#2}\isdot}%
     \ifblank{#3}{}{\bibnamedelimd\mkbibnameprefix{#3}\isdot}%
     \ifblank{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnameaffix{#4}\isdot}}}

\DeclareStyleSourcemap{
    \maps[datatype = bibtex]{
        \map{%% Make sure a field like [Adam John Smith] comes out as [Smith, Adam John]
            \step[fieldsource = author,
                match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z},
                replace = {[$2, $1]}]
    }
        \map{%% Abbreviate an entry such as Adam J[ohn] Smith to Smith, Adam J.
            \step[fieldsource = author,
                match = \regexp{(\w+)\[(.+?)\]},
                replace ={$1.}]
    }
        \map{%% Abbreviate an entry such as [Adam John] Smith to Smith
            \step[fieldsource = author,
                notmatch = \regexp{\A\[(.+)\]\Z},
                final]
            \step[fieldsource = author,
                match = \regexp{(\A|\,\s)\[(.+?)\]},
                replace = {$1}]
    }
  }
}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@BOOK{lennon1970,
    AUTHOR = "J[ohn] Lennon",
    TITLE = "My life with the Beatles",
    YEAR = "1970",
    SORTNAME = "John Lennon"}
@BOOK{lennon1971,
    AUTHOR = "[John Lennon]",
    TITLE = "Moving on",
    YEAR = "1971",
    SORTNAME = "John Lennon"}
@BOOK{lennon1972,
    AUTHOR = "[J. John Lennon]",
    TITLE = "Moving further on",
    YEAR = "1972",
    SORTNAME = "John Lennon"}
@BOOK{lennon1973,
    AUTHOR = "John Lennon",
    TITLE = "Still moving on",
    YEAR = "1973",
    SORTNAME = "John Lennon"}
@BOOK{lennon1974,
    AUTHOR = "[John] Lennon",
    TITLE = "I'm out of here",
    YEAR = "1974",
    SORTNAME = "John Lennon"}
@BOOK{lennon1975,
    AUTHOR = "Lennon, [John]",
    TITLE = "I'm out of here",
    YEAR = "1975",
    SORTNAME = "John Lennon"}
@BOOK{beatles1970,
    AUTHOR = "John W[inston] Lennon and J[ames] Paul McCartney",
    TITLE = "Let it be",
    YEAR = "1970"}
@BOOK{gauch2012,
    AUTHOR = "Gauch, Jr., Hugh G.",
    TITLE = "Scientific method in brief",
    YEAR = "2012",
    LOCATION = "Cambridge",
    PUBLISHER = "Cambridge University Press"}
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}

\nocite{*}
\printbibliography
\end{document}

Gauch, Hugh G., Jr.

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.