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.

8
  • Does this help: tex.stackexchange.com/questions/74920/… Commented Nov 6, 2013 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
    Commented Nov 6, 2013 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, Commented Nov 6, 2013 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
    Commented Nov 6, 2013 at 17:45
  • Biber can do regex replacements; but surely a regex for this problem would be huge.
    – egreg
    Commented Nov 6, 2013 at 18:14

1 Answer 1

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

3
  • It works, but it undoes the effect of another DeclareSourcemap that PLK gave me here: tex.stackexchange.com/a/95378/9077 ... I cannot do both at the same time?
    – Sverre
    Commented Nov 6, 2013 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
    Commented Nov 6, 2013 at 21:18
  • @moewe It worked when I put the two \step commands under \maps\map{...} - otherwise it didn't work.
    – Sverre
    Commented Nov 6, 2013 at 21:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .