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

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

  • 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

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.