TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a fool-proof way to extract all bibtex citation-keys that are cited in a .tex file?

I do not mean regular-expression magic on the .tex-file because this is bound to cause problems when switching between natbib, apacite etc. which all use different citation commands. Also, citations made using \nocite{*} will not be included ...

I though about looking into the .bbl file which does contain all references included in the final document but the format of the .bbl file differs vastly between packages as well such that the key-extraction is difficult.

share|improve this question
up vote 8 down vote accepted

The citations are contained in the .aux file.

\AtVeryEndDocument{\typeout{***^^JCited keys: \mycites^^J***}}

This will show on screen and in the .log file, at the end of the LaTeX run, a message such as

Cited keys: xxx,yyy,*

It would be possible to avoid the appearance of *, but I don't think it's worthy the trouble. Only actually cited keys will appear (BibTeX uses \citation{*} as a signal for including the whole database).

One can output the citations to an auxiliary file, instead:


Then, if the file is test.tex, the citation keys will be saved in the file test.cit one per line.

share|improve this answer
Yes, I noted that \nocite{*} citations do not appear. However, in the .aux file, all items appear in \bibcite{} commands (at least in my current test-setup)... – thias Oct 19 '11 at 13:34
\bibcite entries are written when reading the thebibliography environment, so also keys coming from \nocite{*} will be there. The right entries are the \citation ones. – egreg Oct 19 '11 at 14:39
so, replacing \citation with \bibcite everywhere in your code will output all the used citation keys? That would be exactly what I need... – thias Oct 19 '11 at 14:58
I tested it and it works. One minor issue: latex breaks the output at (probably) exactly 80 characters such that the list is broken at weird places. Since I want to use the output in a script, is there a way to output it without line-breaks? – thias Oct 19 '11 at 15:00
In unix-like systems, one can write cat myfile.aux | grep "\\\\citation" | sed 's/\\citation{\(.*\)}/\1/g' | sort | uniq > myfile.cit and the resulting file contains all used citations, and each of them exactly once. – yo' Feb 3 '12 at 13:35

With bibtool you can do as follows:

bibtool -- preserve.key.case=on -x file.aux -o bibliography.bib

This extracts your cited bibliography. Now if you just grep the file for lines with @ in them, you get fairly close to a list of keys. The option preserve.key.case=on ensures that the case of the keys is not altered (in response to the comment below).

share|improve this answer
nice! However, I want to get along without bibtool. I found that it destroys some of my entries. For example, it decapitalizes my keys all the time... – thias Oct 19 '11 at 13:37

Various TeX-aware programming editors have macros to achieve this. For instance, there's a package called bibmacros for use with winedt which (inter alia) does the job you describe. It works on the .aux file created by latex and BibTeX, and creates a new bib file called jobname-minimal.bib, where jobbame is the name of the aux file (without the "aux" extension, of course). Other editors must have similar macros, either built-in or accessible as extra packages.

share|improve this answer
Hey, that's right! For emacs, I found M-x reftex-create-bibtex-file... – thias Oct 19 '11 at 15:05
@thias --- Really? I don't see that option in reftex 4.31. Could you elaborate? (I usually use bibtool, but it doesn't handle cross-references in the .bib file very well, so an emacs solution would be great.) – jon Feb 3 '12 at 14:55
Found in menu [Ref] -> [Global Actions] -> [Create BibTeX File] – thisirs Nov 14 '12 at 9:15

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.