zepinglee / citeproc-lua

A Lua implementation of the Citation Style Language (CSL) for use with LaTeX
MIT License
66 stars 8 forks source link

Parsing of BibTeX files exported from Zotero using BetterBibLaTeX #48

Closed michal-h21 closed 9 months ago

michal-h21 commented 1 year ago

Describe the bug

I've got a bug report from an user who exported his Zotero records using the BetterBibLaTeX plugin. He got a fatal error from the parsing of that file:

...ocal/texlive/2023/texmf-dist/tex/latex/base/ltluatex.lua:89: bad argument #1
 to 'sub' (string expected, got nil)
stack traceback:
        [C]: in function 'string.sub'
        ...ocal/texlive/2023/texmf-dist/tex/latex/base/ltluatex.lua:89: in upvalue 'ms
g_format'
        ...ocal/texlive/2023/texmf-dist/tex/latex/base/ltluatex.lua:105: in field 'mod
ule_warning'
        ...f-dist/scripts/citation-style-language/citeproc-util.lua:176: in function '
citeproc-util.warning'
        ...ripts/citation-style-language/citeproc-bibtex-parser.lua:340: in function '
citeproc-bibtex-parser.split_name_parts'
        .../scripts/citation-style-language/citeproc-bibtex2csl.lua:240: in function '
citeproc-bibtex2csl.convert_field'
        .../scripts/citation-style-language/citeproc-bibtex2csl.lua:101: in function '
citeproc-bibtex2csl.convert_to_csl_item'
        .../scripts/citation-style-language/citeproc-bibtex2csl.lua:75: in function 'c
iteproc-bibtex2csl.convert_to_csl_data'
        .../scripts/citation-style-language/citeproc-latex-core.lua:153: in upvalue 'r
ead_data_files'
        .../scripts/citation-style-language/citeproc-latex-core.lua:199: in function '
citeproc-latex-core.make_citeproc_sys'
        .../scripts/citation-style-language/citeproc-latex-core.lua:237: in function '
citeproc-latex-core.init'
        ...-dist/scripts/citation-style-language/citeproc-latex.lua:33: in function 'c
iteproc-latex.init'
        [\directlua]:1: in main chunk.
\lua_now:e #1->\__lua_now:n {#1}

l.23 \begin{document}

I've found that this is caused by the author field, which looks like this:

author = {family=Geijersstam, given=V., prefix=af, useprefix=true and Kibur, M. and Wang, Z. and Koskela, P. and Pukkala, E. and Schiller, J. and Lehtinen, M. and Dillner, J.}

Additional information

To Reproduce

\documentclass{article}
\begin{filecontents}[overwrite, noheader]{\jobname.bib}
@article{afGeijersstam1998Stability,
  title = {Stability over Time of Serum Antibody Levels to Human Papillomavirus Type 16},
  author = {family=Geijersstam, given=V., prefix=af, useprefix=true and Kibur, M. and Wang, Z. and Koskela, P. and Pukkala, E. and Schiller, J. and Lehtinen, M. and Dillner, J.},
  date = {1998-06},
  journaltitle = {J Infect Dis},
  volume = {177},
  number = {6},
  eprint = {9607854},
  eprinttype = {pmid},
  pages = {1710--1714},
  issn = {0022-1899},
  doi = {10.1086/517428},
  abstract = {The stability over time of serum IgG antibody levels to human papillomavirus type 16 (HPV-16) was determined by comparing the HPV-16 capsid antibody levels in serial serum samples of an age-stratified random subsample of 1656 primiparous mothers resident in Helsinki who were followed until their second pregnancy, on average 29.5 months later. The correlation between the first and second pregnancy HPV-16 serum antibody levels of the same woman was high, even when {$>$}4 years had elapsed between pregnancies (r = .822). Between negativity, indeterminate results, or quartiles of positivity, the predictive values for being classified in the same category on both occasions ranged between 42\% and 91\%. Correlation coefficients, predictive values, and kappa coefficients between serial samples all were comparable with those of repeat analyses of the same sample, indicating that HPV capsid antibody levels are generally stable during several years of follow-up.},
  langid = {english},
  keywords = {Adolescent,Adult,Antibodies,Capsid,Capsid Proteins,Female,Humans,Infectious,Oncogene Proteins,Papillomaviridae,Papillomavirus Infections,Pregnancy,Pregnancy Complications,Time Factors,Tumor Virus Infections,Viral}
}
\end{filecontents}

\usepackage{citation-style-language}
\addbibresource{\jobname.bib}
\begin{document}
\cite{afGeijersstam1998Stability}
\printbibliography
\end{document}

With lualatex, you will get the original error. When compiled using

$ pdflatex mwe
$ citeproc-lua mwe

You will get:

This is citeproc-lua, Version 0.4.5
The top-level auxiliary file: mwe.aux
Database file #1: mwe.bib
...f-dist/scripts/citation-style-language/citeproc-util.lua:181: attempt to concatenate a nil value (local 'message')
zepinglee commented 1 year ago

Thanks for your feedback and I can reproduce the error. The value of author is in biblatex's extended name format which I've not implemented.

At the moment I suggest exporting to CSL-JSON format from Zotero since the reference data is always converted to CSL-JSON before processed if it's in bib(la)tex format. Alternatively the user can turn off BetterBibTeX's option "using BibLaTeX extended name format".

Screenshot 2023-11-16 at 00 04 43
michal-h21 commented 1 year ago

Yes, it was easy for OP to export his record in a different way once we found the source of the issue. I didn't know that this extended name format even exists, so it was surprising for me as well. I don't think it is necessary to support this format completely, it would be just nice to prevent a compilation error :)

zepinglee commented 1 year ago

I don't think it is necessary to support this format completely, it would be just nice to prevent a compilation error :)

It should not be hard to support that. And I'll fix this error.

michal-h21 commented 1 year ago

That's great! Thanks.

zepinglee commented 9 months ago

Added in 2a77ddb.