將參數擴展為 DeclareFieldFormat 以取代 biblatex 欄位中的單字

將參數擴展為 DeclareFieldFormat 以取代 biblatex 欄位中的單字

我正在嘗試使用 biblatex 自動將書名字段中的單字替換為其縮寫(例如 Proceedings to Proc.、Symposium to Symp. 等)。我的方法是使用\DeclareFieldFormat宏(帶有替換宏多子串替換):

\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}}

問題是,當DeclareFieldFormat運行時,參數尚未擴展到實際資料值,因此傳遞給的參數shorten實際上是:(\printfield [titlecase]{booktitle}\setunit {\addperiod }\printfield [titlecase]{booksubtitle}由呼叫列印message)。

關於如何從 biblatex 中獲取實際書名以便我可以在其上運行我的巨集有什麼想法嗎?

答案1

您需要直接套用該格式。巨集booktitle如下圖所示

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

所以booktitle命令其實是在外部\printtext而不是直接在\printfield.

定義一種新格式並使用它

\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}}

您也可以使用 Biber 進行更換,而無需擔心其他任何事情。

\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.}]
    }
  }
}

相關內容