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}

