适用于MDPI的bibtex转bibitem 和 引用先后顺序调整

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,里面的内容就是你最终希望粘贴到正文中的。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值