13

I have used \DeclareSourcemap in biblatex for three things, cf. Ordering references with bracketed years in biblatex 2.7, Abbreviate resolved abbreviations in authors' first names in biblatex, and Change US postal codes (CA) to AP stylebook abbreviations (Calif.) in bibliography (biblatex).

As pointed out by moewe in a comment (https://tex.stackexchange.com/a/142665/9077), however, \DeclareSourcemap cannot be used more than once. I have tried to combine the three things I've used \DeclareSourcemap for into one command, as seen in the MWE below. The result, however, is that no. 1 does not work in combination with either no. 2 or 3. No. 2 and 3 work fine together, though. In the output below, where all three are used together, only no. 1 has any effect.

What do I need to do in order to take advantage of all three?

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

\DeclareSourcemap{\maps[datatype = bibtex]{\map{
%% 1. Find year in brackets and sort them as if they were without brackets
    \step[fieldsource = year, 
        match = \regexp{\A\[(.+)\]\z}, final]
    \step[fieldset = sortyear, fieldvalue = {$1}]
%% 2. Abbreviate an entry such as Adam J[ohn] Smith to Smith, Adam J.
    \step[fieldsource = author,
        match = \regexp{\[[^]]+\]},
        replace = .]
%% 3. Change US postal codes such as 'MA' to 'StateMA'
    \step[fieldsource = location,
        match = \regexp{,\s*(MA|CA|NY)},
        replace = \regexp{,~\\State\{$1\}}]
    }}}
% then change 'StateMA' etc. to AP style abbreviations 'Mass.' etc.
\def\State#1{\csname State#1\endcsname}
\def\StateMA{Mass.}
\def\StateCA{Calif.}
\def\StateNY{N.Y.}

\begin{filecontents}{\jobname.bib}
@ARTICLE{western1889,
    AUTHOR = "Aug[ust] Western",
    TITLE = "Kurze darstellung des norwegischen lautsystems",
    JOURNALTITLE = "Phonetische Studien",
    YEAR = "1889",
    PAGES = "259--282",
    VOLUME = "2"}
@BOOK{lehiste1970,
    AUTHOR = "Ilse Lehiste",
    TITLE = "Suprasegmentals",
    YEAR = "1970",
    LOCATION = "Cambridge, MA",
    PUBLISHER = "The M.I.T. Press"}
@BOOK{hoff1946,
    AUTHOR = "Ingeborg Hoff",
    TITLE = "Skjetvem{\aa}let",
    YEAR = "1946",
    LOCATION = "Oslo",
    NUMBER = "1",
    PUBLISHER = "Jacob Dybwad",
    SERIES = "Skrifter utgitt av Det Norske Videnskaps-Akademi i Oslo. II. Historisk-filosofisk klasse. 1946"}
@INCOLLECTION{hoff1968,
    AUTHOR = "Ingeborg Hoff",
    BOOKTITLE = "Aurskog og Blaker",
    EDITOR = "[Eyvind Lillevold]",
    TITLE = "M{\aa}let i Aurskog og Blaker",
    YEAR = "[1968]",
    LOCATION = "[Aurskog og Blaker]",
    PAGES = "685--752",
    PUBLISHER = "Bygdeboknemnda for Aurskog og Blaker",
    VOLUME = "3"}
\end{filecontents}
\addbibresource{\jobname.bib}

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

enter image description here

Sverre
  • 20,729

1 Answers1

11

The best practice for Biber sourcemapping is as follows.

  • Use \DeclareSourcemap only once. Starting from version 3.11 of biblatex you can actually use \DeclareSourcemap multiple times without bad side-effects. For compatibility reasons you probably want to stick to the one-sourcemap approach for now. I also think using only one can sourcemap help you keep track of all your manipulations in one place, so I still recommend it, but there are situations where multiple \DeclareSourcemap are a better solution.
  • Put only those \steps forming a logical unit, i.e. those 'belonging' together - those doing the same thing, into the same \map. (In this example the final in one of the mapping commands prevents further execution of the following commands.)
  • Put the \maps for the same datatype into the same \maps environment.

So here we get

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

\DeclareSourcemap{
  \maps[datatype = bibtex]{
    \map{%% 1. Find year in brackets and sort them as if they were without brackets
      \step[fieldsource = year, 
          match = \regexp{\A\[(.+)\]\z}, final]
      \step[fieldset = sortyear, fieldvalue = {$1}]
    }
    \map{%% 2. Abbreviate an entry such as Adam J[ohn] Smith to Smith, Adam J.
      \step[fieldsource = author,
          match = \regexp{\[[^]]+\]},
          replace = .]
    }
    \map{%% 3. Change US postal codes such as 'MA' to 'StateMA'
      \step[fieldsource = location,
          match = \regexp{,\s*(MA|CA|NY)},
          replace = \regexp{,~\\State\{$1\}}]
    }
  }
}
% then change 'StateMA' etc. to AP style abbreviations 'Mass.' etc.
\newcommand{\State}[1]{\csname State#1\endcsname}
\newcommand{\StateMA}{Mass.}
\newcommand{\StateCA}{Calif.}
\newcommand{\StateNY}{N.Y.}

\begin{filecontents}{\jobname.bib}
@ARTICLE{western1889,
    AUTHOR = "Aug[ust] Western",
    TITLE = "Kurze darstellung des norwegischen lautsystems",
    JOURNALTITLE = "Phonetische Studien",
    YEAR = "1889",
    PAGES = "259--282",
    VOLUME = "2"}
@BOOK{lehiste1970,
    AUTHOR = "Ilse Lehiste",
    TITLE = "Suprasegmentals",
    YEAR = "1970",
    LOCATION = "Cambridge, MA",
    PUBLISHER = "The M.I.T. Press"}
@BOOK{hoff1946,
    AUTHOR = "Ingeborg Hoff",
    TITLE = "Skjetvem{\aa}let",
    YEAR = "1946",
    LOCATION = "Oslo",
    NUMBER = "1",
    PUBLISHER = "Jacob Dybwad",
    SERIES = "Skrifter utgitt av Det Norske Videnskaps-Akademi i Oslo. II. Historisk-filosofisk klasse. 1946"}
@INCOLLECTION{hoff1968,
    AUTHOR = "Ingeborg Hoff",
    BOOKTITLE = "Aurskog og Blaker",
    EDITOR = "[Eyvind Lillevold]",
    TITLE = "M{\aa}let i Aurskog og Blaker",
    YEAR = "[1968]",
    LOCATION = "[Aurskog og Blaker]",
    PAGES = "685--752",
    PUBLISHER = "Bygdeboknemnda for Aurskog og Blaker",
    VOLUME = "3"}
\end{filecontents}
\addbibresource{\jobname.bib}

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

which yields enter image description here

moewe
  • 175,683
  • I understand - thanks for the pedagogical explanation! :) But as Oleg mentioned in a comment to my question, final doesn't seem to be needed at all. Any idea why it was there in the first place? – Sverre Nov 07 '13 at 18:53
  • 1
    @Sverre I suppose the final was there because the PLK re-used some other sourcemap he had written before. While in this instance the final is not needed (I think), simply because if no match is found the replacement text will be empty and sortyear will be set to empty and discarded, it is in general not a bad idea to have a final in a place like this. Consider the case in which the second \step is not \step[fieldset = sortyear, fieldvalue = {$1}], but \step[fieldset = sortyear, fieldvalue = {Hi, Sverre}] and compare the output with and without final. – moewe Nov 07 '13 at 19:11
  • 1
    Your comments about including final are taken to heart. It turned out that removing final triggered a lot of warnings about sortyear being empty. – Sverre Nov 07 '13 at 19:42