我为我的 biblatex 文件定义了一个“extendedof”关系类型,用于将作品的扩展版本与其原始发布版本关联起来。为此,我在 latex 文件的序言中添加了以下几行:
\NewBibliographyString{extendedof}
\DefineBibliographyStrings{english}{extendedof={Extended version of},}
\DeclareFieldFormat{relatedstring:extendedof}{#1\addcolon\addspace}
\DeclareFieldFormat{related:extendedof}{\mkbibparens{#1}}
\newbibmacro*{related:extendedof}[1]{\entrydata*{#1}{\iffieldundef{journaltitle}{\iffieldundef{maintitle}{\printfield[title:hook]{booktitle}}{\printfield[title:hook]{maintitle}}\newunit\newblock\usebibmacro{byeditor+others}\newunit\newblock\printfield{edition}\newunit\iffieldundef{volume}{}{\printfield{volume}\printfield{part}}\newunit\newblock\usebibmacro{series+number}\newunit\newblock\printfield{note}\newunit\newblock\usebibmacro{publisher+location+date}\newunit\newblock\usebibmacro{chapter+pages}}{\usebibmacro{author/editor}\newunit\newblock\usebibmacro{title}\newunit\newblock\renewbibmacro*{journal}{\printfield[title:hook]{journaltitle}}\usebibmacro{journal+issuetitle}\newunit\newblock\usebibmacro{byeditor+others}\newunit\newblock\usebibmacro{note+pages}}}}
它有效。问题是我在所有论文中使用单个主库/biblatex 文件,我宁愿不必手动将上述内容添加到我编辑的每个 latex 文档中。有没有办法将上述内容直接包含在 biblatex 文件中以使其可移植?我尝试将每一行包装起来@Preamble{"..."}
并将其添加到 biblatex 文件中,如下所示:
@Preamble{"\NewBibliographyString{extendedof}"}
@Preamble{"\DefineBibliographyStrings{english}{extendedof={Extended version of},}"}
@Preamble{"\DeclareFieldFormat{relatedstring:extendedof}{#1\addcolon\addspace}"}
@Preamble{"\newbibmacro*{related:extendedof}[1]{\entrydata*{#1}{\iffieldundef{journaltitle}{\iffieldundef{maintitle}{\printfield[title:hook]{booktitle}}{\printfield[title:hook]{maintitle}}\newunit\newblock\usebibmacro{byeditor+others}\newunit\newblock\printfield{edition}\newunit\iffieldundef{volume}{}{\printfield{volume}\printfield{part}}\newunit\newblock\usebibmacro{series+number}\newunit\newblock\printfield{note}\newunit\newblock\usebibmacro{publisher+location+date}\newunit\newblock\usebibmacro{chapter+pages}}{\usebibmacro{author/editor}\newunit\newblock\usebibmacro{title}\newunit\newblock\renewbibmacro*{journal}{\printfield[title:hook]{journaltitle}}\usebibmacro{journal+issuetitle}\newunit\newblock\usebibmacro{byeditor+others}\newunit\newblock\usebibmacro{note+pages}}}}"}
这实现了大部分预期效果,只是书目字符串extendedof
似乎没有生效。不过,我在文件中得到了以下内容.bbl
:
\preamble{%
\NewBibliographyString{extendedof}%
\DefineBibliographyStrings{english}{extendedof={Extended version of},}%
\DeclareFieldFormat{relatedstring:extendedof}{#1\addcolon\addspace}%
\newbibmacro*{related:extendedof}[1]{\entrydata*{#1}{\iffieldundef{journaltitle}{\iffieldundef{maintitle}{\printfield[title:hook]{booktitle}}{\printfield[title:hook]{maintitle}}\newunit\newblock\usebibmacro{byeditor+others}\newunit\newblock\printfield{edition}\newunit\iffieldundef{volume}{}{\printfield{volume}\printfield{part}}\newunit\newblock\usebibmacro{series+number}\newunit\newblock\printfield{note}\newunit\newblock\usebibmacro{publisher+location+date}\newunit\newblock\usebibmacro{chapter+pages}}{\usebibmacro{author/editor}\newunit\newblock\usebibmacro{title}\newunit\newblock\renewbibmacro*{journal}{\printfield[title:hook]{journaltitle}}\usebibmacro{journal+issuetitle}\newunit\newblock\usebibmacro{byeditor+others}\newunit\newblock\usebibmacro{note+pages}}}}%
}
答案1
您将@Preamble
代码写入文件.bbl
,因此只有在读取文件后代码才可用.bbl
。这在文档启动时发生(比传统 BibTeX 早得多,在传统 BibTeX 中,文件.bbl
是在打印参考书目时准确读取的),但还不够早。语言选项和字符串在此时已经定义,并且文档语言也已选定。
\DefineBibliographyStrings
只能在前导码中使用,因为它的工作原理是将一些代码注入.lbx
加载例程。这意味着它必须.lbx
在文件加载之前使用。在 中调用它.bbl
对于检测来说太早,\@onlypreamble
不会引发错误,但对于.lbx
加载来说又太晚了。
您可以定义一个宏来执行字符串分配,以便直接使用它们。此外,您还必须重新选择主文档语言,\begin{document}
因为字符串更改仅在语言切换后才生效。
以下 MWE 编译为所需的输出
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}
\usepackage[style=authoryear, backend=biber]{biblatex}
\newrobustcmd*{\ManuallyDefineBibliographyStringsLater}[3]{%
\csgappto{abx@strings@#1}{%
\csdef{abx@lstr@#2}{#3}%
\csletcs{abx@sstr@#2}{abx@lstr@#2}}}
\begin{filecontents}{\jobname.bib}
@Preamble{"\NewBibliographyString{extendedof}"}
@Preamble{"\DefineBibliographyStrings{english}{extendedof={Extended version of},}"}
@Preamble{"\ManuallyDefineBibliographyStringsLater{english}{extendedof}{Extended version of}"}
@Preamble{"\DeclareFieldFormat{relatedstring:extendedof}{#1\addcolon\addspace}"}
@Preamble{"\newbibmacro*{related:extendedof}[1]{\entrydata*{#1}{\iffieldundef{journaltitle}{\iffieldundef{maintitle}{\printfield[title:hook]{booktitle}}{\printfield[title:hook]{maintitle}}\newunit\newblock\usebibmacro{byeditor+others}\newunit\newblock\printfield{edition}\newunit\iffieldundef{volume}{}{\printfield{volume}\printfield{part}}\newunit\newblock\usebibmacro{series+number}\newunit\newblock\printfield{note}\newunit\newblock\usebibmacro{publisher+location+date}\newunit\newblock\usebibmacro{chapter+pages}}{\usebibmacro{author/editor}\newunit\newblock\usebibmacro{title}\newunit\newblock\renewbibmacro*{journal}{\printfield[title:hook]{journaltitle}}\usebibmacro{journal+issuetitle}\newunit\newblock\usebibmacro{byeditor+others}\newunit\newblock\usebibmacro{note+pages}}}}"}
@book{appleby,
author = {Humphrey Appleby},
title = {On the Importance of the Civil Service},
date = {1980},
related = {sigfridsson},
relatedtype = {extendedof},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\addbibresource{biblatex-examples.bib}
\begin{document}
\selectlanguage{english}
\cite{appleby}
\printbibliography
\end{document}
我真的不认为@preamble
它是为了处理如此复杂的代码而设计的。这是在 BibTeX 中创建小定义的一种聪明方法,但我还没有看到它经常与 一起使用biblatex
。
如果您希望您的定义在所有文档中可用biblatex
,则可以将它们放入biblatex.cfg
。(例如,参见Biblatex.cfg 与 .cls 与 .sty。我不太喜欢,biblatex.cfg
因为我认为很容易忘记它是活动的,但对于这样的修改来说,它可能没问题。当然,它不像在文件中包含所有代码那样具有可移植性.bib
。)