4

My .bib file is using US postal codes as abbreviations for US states (CA and MA for California and Massachusetts). For a paper I'm writing, these need to be changed to abbreviations such as Calif. and Mass. (cf. Wikipedia).

I'd prefer not to change my .bib file, but instead include a command in my preamble that says "When the entry LOCATION in the .bib file has strings such as , CA, , MA, etc., then substitute these with , Calif., , Mass., etc.". (I've added a comma plus space before the strings to be absolutely certain that it doesn't catch any two-letter strings that aren't US states - not that I know right now what that would be, but just in case).

MWE:

\documentclass{article}
\usepackage[style=authoryear]{biblatex}
\begin{filecontents}{\jobname.bib}
@BOOK{lehiste1970,
    AUTHOR = "Ilse Lehiste",
    TITLE = "Suprasegmentals",
    YEAR = "1970",
    LOCATION = "Cambridge, MA",
    PUBLISHER = "The M.I.T. Press"}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\nocite{lehiste1970}
\printbibliography
\end{document}

enter image description here

This should read

Lehiste, Ilse (1970). Suprasegmentals. Cambridge, Mass.: The M.I.T. Press.

Sverre
  • 20,729
  • Does this help: http://tex.stackexchange.com/questions/74920/abbreviating-text-within-a-reference/99426#99426 – Steven B. Segletes Nov 06 '13 at 17:24
  • @StevenB.Segletes: I'd prefer to have a command in the preamble in my .tex file that I can simply comment out whenever I don't need it. All my other biblatex tweaks are in my preamble too - delving into .bst files seems a bit too complicated for me. – Sverre Nov 06 '13 at 17:35
  • 3
    You don't need to delve into bst files. You create several 50-line .bib files with a line for each state, like thus: @STRING{ak.us = "Alaska"} in one versus @STRING{ak.us = "AK"} in the other, and then you just include the desired one as the first bib file in your list. The only extra effort is in composing your LOCATION fields in your bib file as LOCATION = "Fairbanks, " # ak.us, – Steven B. Segletes Nov 06 '13 at 17:42
  • @StevenB.Segletes I didn't fully understand that. But since I know (from previous questions) that it's fully possible to have one command in the .tex preamble that searches for a specific string in a specific entry in the .bib file and then replaces that string with another string, I'd prefer to go with such an approach (which in the end seems like an easier solution). – Sverre Nov 06 '13 at 17:45
  • Biber can do regex replacements; but surely a regex for this problem would be huge. – egreg Nov 06 '13 at 18:14
  • @egreg I would only start with CA and MA, and add others as they appear in my bibliography (I don't think there are too many of them). Is there a simpler solution, though (Steven's suggestion doesn't strike me as a simpler one)? – Sverre Nov 06 '13 at 18:16
  • Just use biblatex+biber's \DeclareSourcemap feature with one regexp map per state which you need to change. Someone will certainly show you how if you can't work it out from the docs. – PLK Nov 06 '13 at 19:13
  • @PLK I think the bigger problem is that I don't really know how to use regex. I have a couple of DeclareSourcemap commands in my premable with some regex stuff in them, but they're much too complicated for me to understand. – Sverre Nov 06 '13 at 19:16

1 Answers1

5

Here's a proof of concept.

\begin{filecontents}{\jobname.bib}
@BOOK{lehiste1970,
    AUTHOR = "Ilse Lehiste",
    TITLE = "Suprasegmentals",
    YEAR = "1970",
    LOCATION = "Cambridge, MA",
    PUBLISHER = "The M.I.T. Press"}
@BOOK{mehiste1970,
    AUTHOR = "Ilse Mehiste",
    TITLE = "Suprasegmentals",
    YEAR = "1970",
    LOCATION = "San Francisco, CA",
    PUBLISHER = "The California Press"}
\end{filecontents}

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

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=location,
        match=\regexp{,\s*(MA|CA)},
        replace=\regexp{,~\\State\{$1\}}]
    }
  }
}
\addbibresource{\jobname.bib}
\def\State#1{\csname State#1\endcsname}
\def\StateMA{Mass.}
\def\StateCA{Calif.}

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

Instead of replacing MA with Mass. I think it's better to replace it with \State{MA}, so that a definition can be given (and modified at will).

enter image description here

egreg
  • 1,121,712
  • It works, but it undoes the effect of another DeclareSourcemap that PLK gave me here: http://tex.stackexchange.com/a/95378/9077 ... I cannot do both at the same time? – Sverre Nov 06 '13 at 21:11
  • 1
    @Sverre I think I read somwhere \DeclareSourcemap{...} may only be used once, so just put the two \maps{...} commands into the same \DeclareSourcemap{...}. – moewe Nov 06 '13 at 21:18
  • @moewe It worked when I put the two \step commands under \maps\map{...} - otherwise it didn't work. – Sverre Nov 06 '13 at 21:27