Is there a tool that "flattens" a complicated BibTeX file?

In particular, I would like to:

  1. Expand @STRING macros, for example:

    @STRING{foo = {Bar}}
    @MISC{x, title = foo}

    @MISC{x, title = {Bar}}
  2. Expand cross references, for example:

    @INPROCEEDINGS{x, title = {A}, crossref = y}
    @PROCEEDINGS{y, title = {B}, booktitle = {B}}

    @INPROCEEDINGS{x, title = {A}, booktitle = {B}}

This can be performed by the bib2bib tool, which provides pretty flexible and reliable ways to filter/extract/expand bibtex entries. This (little known) utility is part of the bibtex2html tool suite. (Note: you have to look for the PDF documentation, the HTML documentation does not discuss bib2bib!)

For instance, to flatten a bib file, one just writes:

bib2bib --expand --expand-xrefs -ob flattened.bib original.bib   

It is also possible to specify filter and sorting options. Multiple conditions can be grouped and combined with and/or/not to extract only entries matching certain criteria.

Edit: One thing that apparently is not supported is filtering out @comment entries. These entries are, for instance, inserted by JabRef. To get rid of them just pipe the result through some regex tool. Jukka suggested perl -p0 -e 's/\s*(\@comment\{\{[^}]*\}\}\s*)+/\n\n/g' for the job.

  • This is almost exactly what I need; being able to flatten and filter with the same tool is great! There is one problem, though: bib2bib seems to preserve all @comment entries, no matter what I do. Even if I use the filtering options, I seem to always get all @comment entries... – Jukka Suomela Oct 12 '11 at 21:30
  • Right, it even adds some @comment entries. If your @comment entries are single-line only (like those added by bib2bib itself), you may just filter the resulting file through sed/grep/awk to get rid of them, e.g. grep -v "@comment" flattened.bib > nocomment.bib. – Daniel Oct 12 '11 at 21:38
  • You can use --no-comment to prevent bib2bib from adding any more comments, but it still preserves all existing comments. In my case I have lots of multi-line comments that are created by JabRef, and they are a bit more tricky to filter out... – Jukka Suomela Oct 12 '11 at 21:44
  • (Of course this is not an unsolvable problem. For example, something like perl -p0 -e 's/\s*(\@comment\{\{[^}]*\}\}\s*)+/\n\n/g' happens to work in my specific case.) – Jukka Suomela Oct 12 '11 at 22:00
  • Does anyone have guidance as to how I can install bib2bib? Their instructions are not working for me. Alternatively, perhaps there is a different software that accomplishes the same goal? – Gene G. Aug 26 '15 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.