Protect underscores in latex equation when bib data is written to a file

Issue #164 new
Xiaowei Wang created an issue

There is a latex equation “${\mathrm{N}}_{2}$“ in the title of my bib data. A backslash is added before the underscore when the bid data is written to a bib file. The following code:

from pybtex.database import BibliographyData,Entry,Person

fields = {}
fields['title'] = 'Test formula of ${\mathrm{N}}_{2}$'
ety = Entry('article',fields=fields)
bibdata = BibliographyData(entries={'test':ety})
bibdata.to_file('minisample.bib',bib_format='bibtex')

outputs:

@article{test,
    title = "Test formula of ${\mathrm{N}}\_{2}$"
}

Is there a way to avoid the backslash insertion?

Comments (2)

  1. Sebastian Schöps

    I just came across the same problem. The latex encoding is applied to all fields, e.g. DOIs, URLs and crossrefs are messed up by this.

  2. Xiaowei Wang reporter

    It seems the backslash insertion is unavoidable. So I have to change it back manually.

    bibstr = bibdata.to_string(bib_format='bibtex',encoding=encoding)
    # undo the bashslash insertion
    #\#$\%^\&*()\_
    bibstr = bibstr.replace('\#','#')
    bibstr = bibstr.replace('\%','%')
    bibstr = bibstr.replace('\&','&')
    bibstr = bibstr.replace('\_','_')
    

  3. Log in to comment