修改生成的 Biblatex 文件以添加额外字段(后期处理)

修改生成的 Biblatex 文件以添加额外字段(后期处理)

我使用的参考管理器(Zotero)不支持我想要使用的某些字段(例如用于选择书籍的 DOI)。

是否有某种工具可以自动将字段 + 值添加到某个 BibTex 键?因此,每次我从 Zotero 导出时,我的构建过程都会注入额外的字段。

所以我正在寻找这样的东西:

cat myBibliography.bib | BibTexAddFieldTool -keyId Author_2015 -fieldId doi -value "MyDOI" > newMyBibliography.bib

答案1

这不是一个真正的 *TeX 答案,但你可以用你选择的任何编程语言编写一个小脚本,例如 perl(仅用于演示目的,即正则表达式不够强大,请调整为 Zotero 输出或使用一些适当的键值对库):

open(BIB,"<$ARGV[0]");
open(XTR,"<$ARGV[1]");
while($line=<BIB>){
    if($line=~/^\s*@(.*?){\s*(.*),\s*$/){   #% read BibTeX key
        $key = $2;
        $type{$key}=$1;
    }
    if($line=~/^\s*(\w+)\s*=\s*(.*?),?\s*$/){   #% store field values for key
        $val{$key}{$1}=$2;
    }
}
while($line=<XTR>){
    if($line=~/^(.*?),(.*?),(.*)$/){   #% store additional fields
        $val{$1}{$2}=$3;
    }
}
foreach $key(sort keys %val){   #% print everything to stdout
    print "@".$type{$key}."{$key,\n";
    foreach $field (sort keys %{$val{$key}}){
        print "\t$field\t=\t$val{$key}{$field},\n";
    }
    print "}\n\n";
}

用法:perl BibTexAddFieldTool.pl myBibliography.bib myAddedFields.txt

参考书目样本:

@book{chicken:2010fj,
    author = {Chicken, C},
    title = {Chicken},
    publisher = {Chicken Press},
    year = {2010}
}
@book{lemur:2009ii,
    author = {Lemur, L},
    title = {Lemur},
    publisher = {Lemur Press},
    year = {2009}
}

附加字段:

chicken:2010fj,doi,{12345}
lemur:2009ii,doi,{54321}
lemur:2009ii,title,{Lemur Adventures}

输出:

@book{chicken:2010fj,
    author  =   {Chicken, C},
    doi =   {12345},
    publisher   =   {Chicken Press},
    title   =   {Chicken},
    year    =   {2010},
}

@book{lemur:2009ii,
    author  =   {Lemur, L},
    doi =   {54321},
    publisher   =   {Lemur Press},
    title   =   {Lemur Adventures},
    year    =   {2009},
}

相关内容