我想编写一个类似这样的宏:
\get-url{10.1090/tran/8044} ------> result: https://www.ams.org/journals/tran/2021-374-01/S0002-9947-2020-08044-1/
即input=doi
和output= url of paper in publisher webpage
这就是我想要的。但如果可以提取并返回接受和出版日期以及作者信息就太好了。但这只是一个梦想,所以……!
答案1
doi 解析器doi.org可以使用标头返回 json 输出Accept: application/json
。例如,您可以使用 Python 解析此输出并提取相关字段。然后,您可以将此内容写入文件,并将其作为 LaTeX 文档的一部分包含在内。
对于问题中的论文,json 输出的部分内容如下所示:
"resource": {
"primary": {
"URL": "https://www.ams.org/tran/2021-374-01/S0002-9947-2020-08044-1/"
}
},
"subtitle": [],
"short-title": [],
"issued": {
"date-parts": [
[
2020,
11,
3
]
]
},
"references-count": 62,
"journal-issue": {
"issue": "1",
"published-print": {
"date-parts": [
[
2021,
1
]
]
}
},
"alternative-id": [
"S0002-9947-2020-08044-1"
],
"URL": "http://dx.doi.org/10.1090/tran/8044",
"relation": {},
"ISSN": [
"0002-9947",
"1088-6850"
],
"subject": [
"Applied Mathematics",
"General Mathematics"
],
"container-title-short": "Trans. Amer. Math. Soc.",
"published": {
"date-parts": [
[
2020,
11,
3
]
]
}
这已经包含了问题和评论中提到的大部分信息。
pycurl
用于检索数据的小型 Python 脚本:
import pycurl
from io import BytesIO
import json
import calendar
doi = '10.1090/tran/8044'
# download json using pycurl
mybuffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://dx.doi.org/'+doi)
c.setopt(c.FOLLOWLOCATION, True)
c.setopt(c.HTTPHEADER, ["Accept: application/json"])
c.setopt(c.WRITEDATA, mybuffer)
c.perform()
c.close()
# parse with json library
contents = mybuffer.getvalue()
bib_dict = json.loads(contents)
# open file to be included by LaTeX
texout = open("paperinfo.tex", "w")
# start a verbatim environment to prevent problems with special characters
print("\\begin{verbatim}", file=texout)
# retrieve relevant fields from json output
print("doi:", doi, file=texout)
print("URL:", bib_dict['resource']['primary']['URL'], file=texout)
print("URL:", bib_dict['resource']['primary']['URL'], file=texout)
print("authors:", file=texout)
for person in bib_dict["author"]:
print(person['given'], person['family'], file=texout)
print("Published date:", calendar.month_name[bib_dict['published']['date-parts'][0][1]], str(bib_dict['published']['date-parts'][0][2])+",", bib_dict['published']['date-parts'][0][0], file=texout)
# end the verbatim environment and close the file
print("\\end{verbatim}", file=texout)
texout.close()
结果paperinfo.tex
:
\begin{verbatim}
doi: 10.1090/tran/8044
URL: https://www.ams.org/tran/2021-374-01/S0002-9947-2020-08044-1/
URL: https://www.ams.org/tran/2021-374-01/S0002-9947-2020-08044-1/
authors:
Anand Dessai
David González-Álvaro
Published date: November 3, 2020
\end{verbatim}
在完整的 LaTeX 文档中显示此文件:
\documentclass{article}
\begin{document}
\input{paperinfo}
\end{document}
生成的pdf:
注意:工作流程分为两步:首先手动运行 Python 脚本,这将创建文件paperinfo.tex
,然后在需要包含信息的文件上手动运行 LaTeX。