BibTeX citation key import, editing, and export

I did this for myself, but thought I'd share in case others would like my simple solution.

I've tweaked the default bibtex translator to use tags prefixed with "bibtex:" as the bibtex citation key. So, to set the citekey for something you've added, simply add a tag called "bibtex:my_cool_citekey" -- then when you export, you can use \cite{my_cool_citekey}. It will also handle .bib imports correctly, adding the appropriate tag for the input citekey.

The complete translator source file which replaces the existing bibtex translator is here:
  • edited April 23, 2012
    what exactly should I do in order to get this working? - where does this script go?
  • I just replaced the existing bibtex translator, and disabled auto-update for translators so it wouldn't get overwritten. To do the same, go to preferences, and uncheck "Automatically check for updated translators" under "General", then go to the "Advanced" tab and click "Show Data Directory"... find the "BibTeX.js" file and replace it with this one.
  • thank you so much!
    works great :)
  • note, though, that disabling auto-updating for translators will in the medium run really impact how well Zotero works for you - we fix and improve translators _all_ the time, to get a sense, check the commit history for translators:
  • yeah adamsmith is right, but there isn't really a good way to do it otherwise. as a workaround, you can always hit "update now" every once in a while to update everything, and then re-replace the bibtex translator (although my version is based on an older original version, so doesn't have the latest changes).

    ... or we could clean it up and get it into the official translator
  • edited April 23, 2012
    I haven't thought much about this, but wouldn't it be possible to just add this as a separate translator with different name and translator ID? That wouldn't get overwritten by Zotero.

    Before you put any effort into cleaning this up, you should ask over at zotero-dev whether this would be likely to get accepted as a patch. For my taste it's a bit more of a hack than I would like. There is a longstanding plan to allow some type of custom keys which could be used for bibtex (but maybe also for an improved RTF scan) that would solve this on a more fundamental level - maybe in 3.5?
  • The problem I had was that I then had to manually pick the translator every time I imported a .bib file. I played with it a little more today and just noticed the priority value at the top... it looks like if I set that low it prefers mine to the original. Woo.

    I've updated the gist to match the latest BibTeX.js ... so now this code should live independently at least so you can re-enable auto updates. Since I updated it, I went ahead and submitted a pull request on github anyway, so we'll see what comments they have.

    Yeah the custom fields have been "planned" for a number of years now, I'm not really holding my breath on that one...
  • Well done, I used your translator.
    I just had a problem during the import: the bibtexkey tag was created only when there were no other keywords.
    I do not know js, but modifying the code at line 1672, I could get it right:
    } else if(field == "keywords" || field == "keyword") {
    var re = new RegExp(keywordDelimRe, keywordDelimReFlags);
    if(!value.match(re) && keywordSplitOnSpace) {
    // keywords/tags
    //modified by pezzacolori
    for (t in value.split(" ")) {item.tags.push(value.split(" ")[t]);}
    //item.tags = value.split(" ");
    } else {
    //modified by pezzacolori
    for (t in value.split(re)) {item.tags.push(value.split(re)[t]);}
    //item.tags = value.split(re);

    Surely you can write a better code, and maybe update your gist.
    I did not chek if also the export has this problem ...

  • Yeah in those lines the tags are getting overwritten, clobbering the citekey tag. Good catch!. A somewhat more efficient way would be:

    item.tags.push.apply(value.split(" "));

    For some reason github won't let me update the gist right now.
  • Thanks for the insight, everyone! Anyone know any Zotero developers who we can contact to fix this in the next release? BibTeX export is a key feature and while the patch is straightforward, getting here from the cryptic LaTeX errors is a pretty arduous task, and I'd hate to think of someone else having to go through this again.
  • @jcmcclurg: Zotero developers read all posts. Read adamsmith's post above (he's not part of the zotero-dev team but a power user & contributor).

    See also:
Sign In or Register to comment.