Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}}
share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

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.

share|improve this answer
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
Thanks Jukka, I have edited the answer accordingly. –  Daniel Oct 13 '11 at 7:00
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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