bibtex2bibitem
MDPI文章需要使用bibitem格式的引用在.tex文件中而不是.bib格式的引用。因此需要进行一个格式的转换。
环境:
Environment: TeX Live 2023
IDE: TeXworks
首先下载MDPI的latex模板,里面会包含以下内容:
Defintions
template.tex
...
如果编译不通过,参考下面的回答:
有发过MDPI的大佬没,求大佬救救孩子!!!网站下载的latax模板运行不出来? - 略知一二就行的回答 - 知乎
https://www.zhihu.com/question/535796834/answer/3353170673
新建文件夹test
,最好路径中不要带有空格,将Definitions
文件夹放入test
中,同时在text
中新建文件trans.tex
,在Definitions
中新建文件references.bib
,文件结构如下:
test
|-Definitions
|-mdpi.cls
|-references.bib
|-mdpi.bst
|-...
|-trans.tex
reference.bib
中的格式为BibTex格式,trans.tex
中内容如下:
\documentclass{article}
\usepackage{hyperref} % doi可以跳转连接
\begin{document}
\nocite{*} % 此处设置为不引用文献
\bibliography{Definitions/references.bib} % 导入你的bib文件
\bibliographystyle{Definitions/mdpi} % (.bst文件)
\end{document}
使用TeXworks的编译模式为pdfLaTeX进行一次编译用于获得.aux文件。之后的操作都使用BibTeX进行编译,根据.bib的内容可以获得相应的.bbl文件,这里面的内容就是最后贴到论文里的内容。
order
另外,使用bibitem会出现引用顺序与bibitex的顺序一致,而与文中引用先后顺序不一致的问题,这一点很讨厌,既然用了latex那就是不想在文献顺序上花太多功夫的。我写了一个小脚本,半自动的,但是方便不少:
创建新python工程bibitemSquence
文件夹结构如下
bibitemSquence
|-main.py
|-trans.bbl
|-main.tex
其中trans.bll
是前文bibtex转bibitem中生成的,main.tex
是你的正文,main.py
是脚本,内容如下:
import re
import json
def document_extract(document):
"""
Extract the refs with sequence.
:param document: The whole tex file.
:return: list
"""
result = []
lines = document.splitlines()
for line in lines:
# empty line
if len(line) == 0:
continue
# notation line
if line[0]=='%':
continue
citations = re.findall(r'cite\{([^\}]+)\}', line)
for citation in citations:
cites = citation.split(',')
for cite in cites:
if cite in result:
continue
else:
result.append(cite)
return result
def bibitex_extract(bbl):
bbl = bbl.replace(r'\begin{thebibliography}{999}','')
bbl = bbl.replace(r'\end{thebibliography}','')
matches = re.findall(r"(\\bibitem.*?\{(.*?)\})(.*?)(?=\\bibitem|\Z)", bbl,re.DOTALL)
return matches
def reorder(document,bbl):
new = []
document_extract_out = document_extract(document)
bibitex_extract_out = bibitex_extract(bbl)
for cite_index,cite in enumerate(document_extract_out):
for bib_index,bib in enumerate(bibitex_extract_out):
if cite in bib[1]:
new.append(bibitex_extract_out[bib_index])
for index,new_bib in enumerate(new):
new[index]=new_bib[0]+new_bib[2]
return new
if __name__=='__main__':
with open('main.tex', 'r') as file:
file_content = file.read()
with open('trans.bbl', 'r') as file:
bbl = file.read()
out = reorder(file_content,bbl)
with open("ref_order.txt", "w") as file:
for bibitex in out:
bibitex = bibitex.replace('\\\\', '\\')
file.write(bibitex)
执行后会在当前工程目录生成ref_order.txt,里面的内容就是你最终希望粘贴到正文中的。