通过脚本使用 \addbibresource

通过脚本使用 \addbibresource

是否有某种方法可以\addbibresource与 biblatex 一起使用来获取远程书目(在我的情况下是从 CiteULike 获取),但首先通过脚本进行传输?

CiteULike 的输出并不完全符合我的需要,所以目前我使用 bash 脚本来抓取它,通过 sed 管道传输它以去除垃圾并纠正一些内容,然后使用标准 BibTeX 为我汇编参考书目。

在这种情况下,最小工作示例似乎没有意义,因此我将固定我的脚本。

#!/bin/bash

curl "http://www.citeulike.org/bibtex/user/MarkEveritt/tag/nv_notes" | sed \
-e 's/pages = {\(.*\)-.*--.*-.*},/pages = {\1},/' \
-e 's/pages = {\(.*\)+},/pages = {\1},/' \
-e 's/comment =/note =/' \
-e 's/\\\$/$/g' \
-e 's/\(^.*title.*\)\\_/\1_/' \
-e '/^[ ]*abstract.*/d' \
-e '/^[ ]*citeulike-.*/d' \
-e '/^[ ]*priority.*/d' \
-e '/^[ ]*posted-at.*/d' \
-e '/^[ ]*keywords.*/d' \
> bibliography.bib

答案1

您可以使用 biber 忽略/重命名/映射字段(请参阅 biber 手册第 3.1.1 节)。这涵盖了您的大多数用例,因为它允许您重命名和删除字段。现在支持以一般方式更改字段的值。

例如,以下是如何删除 PAGES 字段中的“+”字符:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <sourcemap>
    <maps datatype="bibtex" bmap_overwrite="1">
      <map>
        <map_step map_field_source="PAGES" map_match="\+" map_replace=""/>
      </map>
    </maps>
  </sourcemap>
</config>

编辑以显示此选项的 Biber 0.9.8 格式

答案2

首先,\addbibresource如果您将 biblatex 与 BibTeX 结合使用,则无法与远程资源一起使用。只有 biber 支持远程资源。biblatex 本身只会将远程资源的链接添加到配置文件中,该文件将从 biber 中使用。biber 不支持下载远程资源的命令。并且它不支持下载后处理。因此,使用 biber 代替 BibTeX 不会有帮助。

但你可以做的是:

  1. 创建一个新的 LaTeX 命令,将远程源信息写入辅助文件。
  2. 编写一个脚本,从辅助文件读取远程源信息,调用 curl,通过管道传输输出并运行 BibTeX

下面是使用 BibTeX 和简单的 unsrt 样式代替 biblatex 的完整示例:

\begin{filecontents*}{mybibtex.sh}
#!/bin/sh
#
# $1 is a aux file

echo "$1"
for arb in "`grep '\\remotebibresource {' "$1"`"; do
    echo "$arb"
    remote=`echo "$arb" | cut -b 21- | cut -d\} -f 1`
    echo "$remote"
    lfile=`echo "$arb" | cut -b 21- | cut -d\} -f 2 | cut -b 2-`
    echo "$lfile"
    curl "$remote" | sed        -e 's/pages = {\(.*\)-.*--.*-.*},/pages = {\1},/'       -e 's/pages = {\(.*\)+},/pages = {\1},/'        -e 's/comment =/note =/'        -e 's/\\\$/$/g'         -e 's/\(^.*title.*\)\\_/\1_/'       -e '/^[ ]*abstract.*/d'         -e '/^[ ]*citeulike-.*/d'       -e '/^[ ]*priority.*/d'         -e '/^[ ]*posted-at.*/d'        -e '/^[ ]*keywords.*/d'         >  "$lfile"
done

bibtex "$1"
\end{filecontents*}
\documentclass{article}
\bibliographystyle{unsrt}

\makeatletter
\newcommand*{\addremotebibresource}[2]{%
  \protected@write\@auxout{}{\protect\remotebibresource{#1}{#2}}%
  \bibliography{#2}%
}
\newcommand*{\remotebibresource}[2]{}
\AtBeginDocument{%
  \protected@write\@auxout{}{\global\let\protect\remotebibresource\protect\@gobbletwo}%
}
\makeatother

\begin{document}
\nocite{*}
Test
\addremotebibresource{http://www.citeulike.org/bibtex/user/MarkEveritt/tag/nv_notes}{tempbib1.bib}
\end{document}

第一次运行后,您必须先使 mybibtex.sh 可执行,然后运行mybibtex.sh <aux-file>

再运行两次 Latex 后,你将获得:

在此处输入图片描述

要使其与biblatexbiber一起工作,\addbibresource您只需将 和 调用(在 )替换\bibliography\addbibresource调用bibtexmybibtex.sh当然biber,您必须更改示例文档以使用biblatex ;-)

脚本本身可以改进。但它已经可以工作,并且对一些用户有用。这就是我制作这个改进示例的原因。

最后但并非最不重要的一点:biblatex 和 biber 有很好的支持。也许一个功能请求支持用户定义的远程资源类型(运行 curl)和用户定义的命令,以便在 biber 处理之前预处理(临时)数据库是一个好主意。

相关内容