4

This is now a follow-up to Prevent abbreviation of names for anonymous work with biblatex, itself a follow-up to Abbreviate resolved abbreviations in authors' first names in biblatex.

In short, I need to print the names of the authors as they appear in their own original work. In my .bib file, on the other hand, I always supply the missing information about names within brackets. The trick is therefore to delete this extra information in appropriate ways.

Now, using the code from Andrew Swann's answer to my question, I notice that when I have an entry in my .bib file in which the author's first name is missing, yet supplied in brackets, the output prints the author's last name followed by a comma plus white space plus a period, because the code has abbreviated a first name into an empty white space character:

Lennon, . (1974).

The appropriate output would be one that just prints the last name: Lennon (1974). The other names should appear as they do in this MWE.

Any suggestions?

\documentclass{article}

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

\DeclareSourcemap{\maps[datatype = bibtex]{
\map{
  \step[fieldsource = author, notmatch = \regexp{\A\[.+\]\Z}, final]
  \step[fieldsource = author, match = \regexp{\[[^]]+\]}, replace = .]
}
\map{
 \step[fieldsource = author, match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z},
 replace = {[$2, $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"}
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}

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

enter image description here


EDIT

Applying moewe's suggestion below, something goes wrong when there is more than one author with brackets in the AUTHOR field.

\documentclass{article}

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

\DeclareSourcemap{
  \maps[datatype = bibtex]{
    \map{
      \step[fieldsource = author, match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z}, replace = {[$2, $1]}]
    }
    \map{
      \step[fieldsource = author, match = \regexp{(\w+)\[(.+)\]}, replace ={$1.}]
    }
    \map{
      \step[fieldsource = author, notmatch = \regexp{\A\[(.+)\]\Z}, final]
      \step[fieldsource = author, match = \regexp{(\A|\,\s)\[(.+)\]}, replace = {}]
    }
  }
}

\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"}
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}

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

enter image description here

Sverre
  • 20,729

1 Answers1

2

These regex trickeries can be quite complicated and the solution here might well have its drawbacks, but here goes ...

\DeclareSourcemap{
  \maps[datatype = bibtex]{
    \map{
      \step[fieldsource = author, match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z}, replace = {[$2, $1]}]
    }
    \map{
      \step[fieldsource = author, match = \regexp{(\w+)\[(.+?)\]}, replace ={$1.}]
    }
    \map{
      \step[fieldsource = author, notmatch = \regexp{\A\[(.+)\]\Z}, final]
      \step[fieldsource = author, match = \regexp{(\A|\,\s)\[(.+?)\]}, replace = {$1}]
    }
  }
}

The first map is taken verbosely from the MWE and hence Andrew Swann's answer. The second map transforms pairs of brackets immediately following a word character (i.e. a letter) into an abbreviation dot. Finally, the third part deletes an entire first name if wrapped in brackets ([John] Lennon and Lennon, [John] both match and are transformed to Lennon).

MWE

\documentclass{article}

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

\DeclareSourcemap{
  \maps[datatype = bibtex]{
    \map{
      \step[fieldsource = author, match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z}, replace = {[$2, $1]}]
    }
    \map{
      \step[fieldsource = author, match = \regexp{(\w+)\[(.+?)\]}, replace ={$1.}]
    }
    \map{
      \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"}
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}

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

enter image description here

moewe
  • 175,683
  • Thanks! But I think you misunderstood parts of my question. For anonymous work, in which the entire name is put between brackets in my .bib file, I do not want the brackets removed. In my example, then, I only want to fix the Lennon, . (1974). entry to Lennon (1974). Everything else should stay the way it is. (If you manage to find a solution to this, please just add it to your existing answer, which I think migth be useful on a later occasion as well :)) – Sverre Aug 12 '15 at 16:03
  • @Sverre Indeed, I had misunderstood the question... Try the edited version then, if you will. – moewe Aug 12 '15 at 16:11
  • Would you mind adding a short comment on what part of the code does what? – Sverre Aug 12 '15 at 16:24
  • Actually, something goes wrong when there's more than one author with brackets in their name - see my updated question (in a minute or two). – Sverre Aug 12 '15 at 16:46
  • @Sverre Of course it does, but that is probably far above my pay grade. The regex is going to be much more involved, I would think. – moewe Aug 12 '15 at 16:47
  • I didn't know there were biblatex problems you couldn't solve :) – Sverre Aug 12 '15 at 16:58
  • @Sverre OK, that was just a matter of greediness and laziness, have a look at the new version, please. – moewe Aug 12 '15 at 16:58
  • @Sverre I found another corner case where a small modification was necessary (add $1 in the third map). You may want to try the new solution. – moewe Aug 12 '15 at 17:12
  • Sweet! In my actual document, though, I'm left with a comma in the case where the entire first name is within brackets. I'll do some diagnostics to figure out what this code interfers with and open a new question (I'll consider this question solved now). – Sverre Aug 12 '15 at 17:17
  • @Sverre Yes, I got Biber warnings about something like this as well, but those were safe to ignore since Biber just drops that unnecessary comma. – moewe Aug 12 '15 at 17:23
  • I found the culprit. Your code interfers with this code. I'll open a new question about it. – Sverre Aug 12 '15 at 17:35
  • Follow-up question in http://tex.stackexchange.com/questions/261105/commas-in-biblatex-entries – Sverre Aug 12 '15 at 17:46