15

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.

3 Answers 3

9

The citations are contained in the .aux file.

\usepackage{atveryend}
\makeatletter
\let\origcitation\citation
\AtEndDocument{\def\mycites{\@gobble}%
  \def\citation#1{\g@addto@macro\mycites{,#1}\origcitation{#1}}}
\AtVeryEndDocument{\typeout{***^^JCited keys: \mycites^^J***}}
\makeatother

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:

\makeatletter
\let\origcitation\citation
\AtEndDocument{\def\mycites{}%
  \def\citation#1{\g@addto@macro\mycites{#1^^J}\origcitation{#1}}}
\AtVeryEndDocument{\newwrite\citeout\immediate\openout\citeout=\jobname.cit
  \immediate\write\citeout{\mycites}\immediate\closeout\citeout}
\makeatother

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

15
  • 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, 2011 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, 2011 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, 2011 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, 2011 at 15:00
  • 2
    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, 2012 at 13:35
11

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).

2
  • 1
    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, 2011 at 13:37
  • See also bibexport [tex.stackexchange.com/a/41823/28411]
    – alexis
    Jul 23, 2020 at 20:20
3

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.

3
  • 2
    Hey, that's right! For emacs, I found M-x reftex-create-bibtex-file...
    – thias
    Oct 19, 2011 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, 2012 at 14:55
  • Found in menu [Ref] -> [Global Actions] -> [Create BibTeX File]
    – thisirs
    Nov 14, 2012 at 9:15

You must log in to answer this question.

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