A compatible re-implementation of the BibTeX program in Common Lisp, with a BST-to-CL compiler. By Matthias Köppe
Clone or download
Latest commit 17a16f5 Dec 5, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
debian Change license to LGPLv2.1 or later with Lisp clarification, update R… Aug 23, 2007
examples Update examples Dec 5, 2018
.cvsignore *** empty log message *** Aug 24, 2007
AUTHORS Update AUTHORS Dec 5, 2018
COPYING Change license to LGPLv2.1 or later with Lisp clarification, update R… Aug 23, 2007
COPYING-preamble.txt Add preamble file Aug 23, 2007
ChangeLog New version 1.1 Dec 5, 2018
Makefile Bump version number Nov 20, 2009
NEWS New version 1.1 Dec 5, 2018
README Update README Dec 5, 2018
TODO *** empty log message *** Dec 29, 2006
bibtex Fix according to new packaging. Dec 12, 2007
bibtex-compiler.lisp Change license to LGPLv2.1 or later with Lisp clarification, update R… Aug 23, 2007
bibtex-program.lisp (emulate-bibtex): Bind *gc-verbose* in the right package. Nov 23, 2009
bibtex-runtime.lisp (setf bib-entry-ref): If SORT.KEY$ is changed, remove the cached sort Nov 30, 2009
bibtex.asd Add ASDF metadata Dec 5, 2018
bibtex.lisp (compile-bst-file): Bind *print-length* to avoid truncation. Nov 23, 2009
bibtex.system New file, cmp. Nov 26, 2009
bst-builtins.lisp Use symbols +cite-key+ (rather than the string "KEY") and +entry-type+ Dec 17, 2007
bst-functions.lisp bst-intern: Shadow only once (for ECL) Dec 5, 2018
bst-reader.lisp Use comparison generics for sorting bib entries. Nov 26, 2009
cmp.lisp cmp.lisp: Remove non-ascii character Dec 5, 2018
icu.lisp Add ICU sort-key comparisons, avoiding the need to define CMP between… Nov 28, 2009
interpreter.lisp bst-execute: Fix for empty wizard-defined functions Dec 5, 2018
kpathsea.lisp kpathsea:find-file: Implement for #+ecl Dec 5, 2018
lisp-form-builder.lisp Change license to LGPLv2.1 or later with Lisp clarification, update R… Aug 23, 2007
packages.lisp New exports bib-entry-ref, bib-entry-sort-key. Nov 26, 2009
run-lisp Added from CLOCC. Nov 25, 2002
test.lisp Add ABCL support May 13, 2015


A BibTeX system in Common Lisp

CL-BibTeX is a replacement for the BibTeX program, written in
Common Lisp.  The aim is to enable the user to format bibliographic
entries using Common Lisp programs, rather than using the stack
language of BibTeX style files.  

It is released under the terms of version 2.1 of the GNU Lesser
General Public License as published by the Free Software Foundation
(see file COPYING) or any later version, as clarified by the preamble
found in COPYING-preamble.txt.

(Note: run-lisp is included from CLOCC and has its own license.)

Development now takes place on GitHub at


A project home page is at


(The old development pages at
http://savannah.nongnu.org/projects/cl-bibtex/ are obsolete.)

There is a mailing list `cl-bibtex-users@nongnu.org'.
You can subscribe to the list at 



A powerful tool is needed for dealing with citations in scientific
documents.  BibTeX is good for formatting bibliographies, but
customizing the format is a pain because it requires writing/changing
a program in BAFLL (BibTeX Anonymous Forth-Like Language -- 
Drew McDermott in comp.lang.lisp), which looks like this:

FUNCTION {tie.or.space.connect}
{ duplicate$ text.length$ #3 <
    { "~" }
    { " " }
  swap$ * *

Other citation-related processing like producing a list of authors and
citation indices is usually done with a combination of BibTeX, TeX
macros, and ad-hoc scripting with Perl or AWK.

The new CL-BibTeX system aims to replace all this.


CL-BibTeX is a compatible and stable re-implementation of the BibTeX
program, which was written by Oren Patashnik in the WEB language.

It consists of the following components:

 * A reader for BibTeX style files (.bst),
 * a reader and a writer for bibliography databases (.bib),
 * a reader for .aux files,
 * an implementation of BibTeX's built-in functions (like for parsing and
   formatting names, etc.),
 * an interpreter for the stack language of BibTeX style files.

Thus CL-BibTeX allows to both use existing BibTeX style files,
and to write new BibTeX styles in Common Lisp.  

In addition to this, CL-BibTeX contains:

 * A compiler that transforms BibTeX style files into comprehensible
   Common Lisp programs (using type analysis).

   The resulting Common Lisp programs can serve as a starting point
   for the customization of the style.

Future directions of CL-BibTeX's development can be found in the TODO

Where to start

Compile and load all Lisp files, using ASDF or bibtex.system.

The function BIBTEX is like the bibtex program.  Pass the file name
STEM; BIBTEX will read STEM.aux, interpret a BST file, and write

	(bibtex-compiler:bibtex "example")

In fact, when the STEM.aux file requests a bibliography style, BIBTEX
first looks whether a Lisp function that implements the style has been
defined using DEFINE-BIBTEX-STYLE.  Otherwise, it tries to find (using
kpathsearch) and load a Lisp file named "STYLE.lbst"; it is supposed
to define the style using DEFINE-BIBTEX-STYLE.  Finally, BIBTEX tries
to find "STYLE.bst" and interpret it.

A BibTeX style implemented in Lisp is supposed to read all
bibliography files in *BIB-FILES* and to write the formatted
bibliography to the stream *BBL-OUTPUT*.  The package BIBTEX-RUNTIME
contains useful routines for reading AUX files, formatting names, etc.

You can invoke the BST-to-Common-Lisp compiler with the function

	(bibtex-compiler:compile-bst-file (kpathsea:find-file "amsabbrv.bst")

The resulting Lisp file is a readable Common Lisp version of the BST
file, which can be run on the AUX file to produce a BBL file, without
using the BST interpreter:

        (compile-file "amsabbrv.lbst")
        (load "amsabbrv.lbst")            ; calls DEFINE-BIBTEX-STYLE

	(bibtex-compiler:bibtex "example")	    ; now uses the Lisp style

There is also a little shell script "bibtex" for invoking CL-BIBTEX
from the shell; it uses the "run-lisp" script from CLOCC.

Restrictions of the compiler

The BST->CL compiler works by analyzing the type of the wizard-defined
functions (i.e., those defined by a FUNCTION command in the BST
file).  The compiler tries to find out how many "literals" (values) a
function pops from the stack and how many it pushes and determines

* If a wizard-defined function tries to pop a function literal that it
  hasn't pushed, or leaves a function literal on the stack, you lose.
  (In other words, you can't define higher-order BST functions.)

* The two branches of an IF$ function must deliver the same net number
  of values.  (As a special exception, the type of IF$ within a WHILE$
  body which occurs in the FORMAT.NAMES function of the standard
  styles is also supported.  This is reported with a warning message.)

On the compilation of a language that no-one can read or write

The language of BibTeX style files (BAFLL) is expected to be read or
written by "wizards" only.  (This is how the original BibTeX calls the
authors of BibTeX style files.)  In fact, when casual users try to
customize a BibTeX style file, they often come up with slightly broken
styles that appear to work but which fail in corner cases.  The reason
is that programs written in BAFLL can hardly be read, written, or even

This causes an extra difficulty for the compiler, because broken
corner cases tend to misbehave on the stack, rendering the function
unanalyzable, hence uncompilable.  The compiler now contains a
heuristic that tries to fix these situations, but it may fail.  In
this case, you should try to understand the error messages, fix the
BibTeX style file accordingly, and try compiling it again.

Implementation dependencies

It does not run on some 0.7.? version of SBCL because it does not like
(COPY-READTABLE NIL).  SBCL 0.7.9 seems to be fine.

I noticed that CMUCL 3.0.8 18c+ does not like (PEEK-CHAR T STREAM NIL
#\Space); it won't skip over whitespace when the EOF character is
whitespace?  (I don't use this any more in CL-BibTeX.)

CLISP 2.30:
1. When I invoke COMPILE-BST-FILE, CLISP says "Lisp stack
   overflow. RESET"; this seems to be caused by the pprint-dispatch
   function for DEFINE-BIBTEX-STYLE.
2. The format directive ~<...~:>  (logical block) does not seem to work
(I have used reader conditionalization to make CL-BibTeX work with CLISP.)

Have fun!

 -- Matthias Koeppe <mkoeppe@math.ucdavis.edu>