2

I'm trying to use biblatex to automatically replace words in the booktitle field with their abbreviations (e.g. Proceedings to Proc., Symposium to Symp. etc.). My approach has been to use the \DeclareFieldFormat macro (with a substitution macro courtesy of Multi-substring replace):

\newcommand{\shorten}[1]{%
    \saveexpandmode\noexpandarg
    \def\x{#1}%
    \xStrSubstitute{\x}{Proceedings}{Proc.}[\x]%
    \x%
    \message{\x}
    \restoreexpandmode
}
\newcommand*{\xStrSubstitute}{%
    \expandafter\StrSubstitute\expandafter
}

\DeclareFieldFormat[inproceedings]{booktitle}{\shorten{#1}}

The problem is that when the DeclareFieldFormat is run, the argument hasn't yet been expanded to the actual data value, so the argument passed to shorten is actually: \printfield [titlecase]{booktitle}\setunit {\addperiod }\printfield [titlecase]{booksubtitle} (printed by the message call).

Any ideas on how to get the actual booktitle out of biblatex so I can run my macro on it?

2

You need to apply the format directly. The booktitle macro looks as follows

\newbibmacro*{booktitle}{%
  \ifboolexpr{
    test {\iffieldundef{booktitle}}
    and
    test {\iffieldundef{booksubtitle}}
  }
    {}
    {\printtext[booktitle]{%
       \printfield[titlecase]{booktitle}%
       \setunit{\subtitlepunct}%
       \printfield[titlecase]{booksubtitle}}%
     \newunit}%
  \printfield{booktitleaddon}}

So the booktitle command is actually in the outer \printtext and not directly in the \printfield.

Define a new format and use that

\DeclareFieldFormat{shortenbooktitle}{#1}
\DeclareFieldFormat[inproceedings]{shortenbooktitle}{\shorten{#1}}

\renewbibmacro*{booktitle}{%
  \ifboolexpr{
    test {\iffieldundef{booktitle}}
    and
    test {\iffieldundef{booksubtitle}}
  }
    {}
    {\printtext[booktitle]{%
       \printfield[shortenbooktitle]{booktitle}%
       \setunit{\subtitlepunct}%
       \printfield[shortenbooktitle]{booksubtitle}}%
     \newunit}%
  \printfield{booktitleaddon}}

You can also use Biber to do the replacement and don't have to worry about anything else.

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \pertype{inproceedings}
      \step[fieldsource=booktitle,
              match=\regexp{Proceedings},
              replace=\regexp{Proc.}]
      \step[fieldsource=booktitle,
              match=\regexp{Symposium},
              replace=\regexp{Symp.}]
    }
  }
}
3
  • Thanks! Where did you find the code of the booktitle macro? I also want to do this to the journal field in the article type.
    – Joe
    Jul 11 '17 at 13:00
  • @Joe You can find most of the code in biblatex.def and standard.bbx. The booktitle and journal macros are both in biblatex.def. Let me emphasise again that I would use Biber over string manipulation via \DeclareFieldFormat.
    – moewe
    Jul 11 '17 at 13:03
  • Thanks again. Unfortunately, I'm forced to use bibtex due to co-author requirements, but I'll keep it in mind for the future.
    – Joe
    Jul 11 '17 at 13:09

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.