20

Normally I use zotero and its bibtex/biblatex export tool to create a .bib file. But this method doesn't put you in charge of the bibliography and you could reproduces errors in the references if zotero fails to retrieve the right data (or if the conversion to bib(La)TeX is wrong).

Therefore, I am considering the possibility to go back to the old method; manage my bibliography within a single .bib edited by hand. I used to do this with the help of Emacs+AUCTeX. It is very simple to enter the different field with Emacs and to create references. However, the file is not organized; there is no order of references. Emacs writes references where the cursor is.

I would like to have some minimum order inside this .bib file. For example, the references could be order alphabetically by name of author. How can I achieve this? And, more generally, what are the tools available to manager a .bib file by hand? (to search inside for example). I have to precise I don't want to use JabRef.

2
  • 3
    Does BibTeX-Edit > Operating on buffer or region > Sort entries help? This sorts BibTeX entries by citation key.
    – giordano
    Dec 15, 2013 at 23:38
  • 1
    Well, if you use a dedicated TeX editor such as TeXMaker you can edit directly your .bib files. It let you to paste directly the bib files from Google Scholar and other sites where you can download the references in bibtex format. My favourite tool for manage and edit my references is still JabRef. Is multiplatform and can manage references in bibtex and biblatex formats, perhaps it helps you. No matter if you edit your .bib file with zotero(in fact you can import them from zotero) or by hand.
    – Aradnix
    Dec 15, 2013 at 23:49

5 Answers 5

20

bibtool can sort your .bib file, among many other things. Although I do edit my bibliography file by hand, I don't use it much for this task, because emacs provides a better solution (see the comment by giordano, e.g.).

To sort by bibkey, it is as simple as (options are not grouped here for clarity):

bibtool -s -i input.bib -o output.bib

To reverse sort by bibkey, use:

bibtool -S -i input.bib -o output.bib

(Note that this tool therefore encourages a rational bibkey naming strategy. Ideally, your bibkeys are logically connected to the author or editor field. E.g., an article written by John Smith and published in the year 2000 would have something like smith2000 as the bibkey. If further disambiguation is needed, I recommend adding the first letters of the first four words: so smith2000aaos might be used for Smith's article with a title like 'An Article on Stuff. My .bib file has about 2700 entries [and growing] and there are no duplicate bibkeys with this system.)

[Edit:] Recent versions of bibtool allow sorting by specific fields. For instance, to sort by author, use

bibtool -s –sort.format=”%N(author)” input.bib output.bib

See the manual for more details.

7
  • So what if I want it sorted by say, journal or date of publication? Is that possible?
    – Canageek
    Dec 18, 2013 at 20:49
  • @Canageek -- Not to my knowledge. Even bibsort lacks that ability. I've never had the need, but GUI applications can probably do stuff like that.
    – jon
    Dec 18, 2013 at 22:29
  • I print out literature lists with \nocite{*}, so it would be nice to be able to organize them, and I do the bib files by hand. Too bad.
    – Canageek
    Dec 18, 2013 at 22:48
  • @Canageek -- Why not organize them with an appropriate bibliography style? It is easy to sort entries any way you want, e.g., with biblatex and biber.
    – jon
    Dec 18, 2013 at 22:51
  • 1
    @Canageek -- Don't give up on it. For example, it could be as easy as using something like \DeclareSourcemap{\maps[datatype=bibtex,overwrite]{\map{\step[fieldsource=journal,fieldset=presort,origfieldval]}}} and \printbibliography[type=article] to print all articles alphabetically by journal name.
    – jon
    Dec 19, 2013 at 5:06
3

Emacs includes bibtex-mode, which provides functions for adding, editing and maintaining .bib files. It will maintain your entries in alphabetical order. It can also be configured to auto-generate keys for new entries based on author, year, title etc. In my case, it produces a key in the form author_author_year, so the keys look very similar to the how the entry will look after processing with natbib.

You can find the documentation from within Emacs with C-h m bibtex-mode.

2

I manage all my bib file by hand using notepad++ and copy and paste, which brings me to the long way to do it: Each entry is just text, so provided your files aren't too long, you could just copy and paste them by hand.

For longer files, things are tricker. I've not heard of a program that is made to sort bib files. However, LaTeX doesn't care about spacing so you could just put each entry on one line, and then use something like the unix sort command to put them in order and save the new file.

2

Possibly one of the better programs for managing BibTeX databases is ebib, which runs inside Emacs.

Ebib allows different ways of sorting. From the manual:

You can specify a sort order in Ebib's customisation buffer. To sort the entries, you must set at least one sort level (that is, a field to sort the entries on). You can also specify more than one sort level: if two entries have identical values for the first sort level, they will be sorted on the second sort level. E.g., if the first sort level is author and the second is year, then the entries are sorted by author, and those entries that have identical values for the author field are sorted by year.

A sort level is not restricted to a single field. You can specify more fields for a single sort level. Within a single sort level, a second sort field is used if the first sort field does not have a value. For example, books that have an editor instead of an author will have an empty author field. If you sort the database on the author field, such entries will all appear at the beginning of the .bib file, which is most likely not what you want.

See Chapter Sorting the .bib File.

1

To sort the entries of a reasonably formatted file:

python bibsort.py references.bib

Where bibsort.py is:

"""Sorts a *formatted* file that has @'s at the beginning of the line."""

import sys

filenames = sys.argv[1:]

for filename in filenames:
    entries = []

    # Parse file. Assume reasonably well-formatted.
    with open(filename) as f:
        for line in f:
            if line.startswith("@"):
                entries.append([])
            entries[-1].append(line)

    # Sort by "key" in "@misc{key,".
    entries.sort(key=lambda lines: lines[0].split("{")[1].lower())

    # Save sorted file.
    with open(filename, "w") as f:
        f.write("".join(x for xs in entries for x in xs))

This only works on files that look like:

@misc{tolkien1954lotr,
    ...
}

But not on files that look like:

          @misc{
tolkien1954lotr,
  ...     }       @misc  {   
      subpar2020formatting, author 
 = {    What Is Formatting et al.}, } @article{...}

...So you may want to give biber or bibtool a run beforehand to format the file in a nice way. (See the other answers on how to use these tools.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .