LyZ: Zotero and LyX integration
Hello,
I have put together a simple plugin for Zotero that does little more than just inserting citations to LyX documents. It's been inspired by Lytero, which I wanted to improve, but ended up starting from scratch (with some help of Lytero code). Hence the new name.
My ideal for LyX/Zotero plugin was:
1. no need to export to bibtex every time I added articles to Zotero
2. automatic updates of the bibtex database
3. no need to specify the document and bibtex file every time I close Zotero or LyX
4. custom keys
Hope others will find it useful.
Please see: http://www.klubko.net/?page_id=945 for details and the installation file.
I have tested only on Windows XP, Firefox 3.5.6. It seems to be working quite smoothly, but more testing and some improvement will follow. Please let me know about any problems you encounter and comments/improvement ideas.
Petr
I have put together a simple plugin for Zotero that does little more than just inserting citations to LyX documents. It's been inspired by Lytero, which I wanted to improve, but ended up starting from scratch (with some help of Lytero code). Hence the new name.
My ideal for LyX/Zotero plugin was:
1. no need to export to bibtex every time I added articles to Zotero
2. automatic updates of the bibtex database
3. no need to specify the document and bibtex file every time I close Zotero or LyX
4. custom keys
Hope others will find it useful.
Please see: http://www.klubko.net/?page_id=945 for details and the installation file.
I have tested only on Windows XP, Firefox 3.5.6. It seems to be working quite smoothly, but more testing and some improvement will follow. Please let me know about any problems you encounter and comments/improvement ideas.
Petr
http://www.lyx.org/trac/ticket/6300
Petr
1) No need to send output to the clipboard just to get it back. Also, translation is asynchronous, which is probably why you're (according to a comment in the code) getting intermittent failures. Rather than use Zotero_File_Interface.exportItemsToClipboard(), just use the underlying code:
var translation = new Zotero.Translate("export");
translation.setItems(items);
translation.setTranslator(format);
translation.setHandler("done", callback);
translation.translate();
and pass a callback.
2) Make sure your variables are declared locally with 'var'. Many of yours are global.
3) Looks like you're running Zotero.Lyz.init() each time a new window opens. You'll need to run createUI() in each window, but you shouldn't need to set up the DB connection, etc., each time. You can probably init() from include.js (without createUI()) and just call createUI() in the window load handler.
I have a question though. (This is probably not the place to ask, but I tried javascript IRC and no one knowledgeable was around).
I need user to select the bibtex file, i.e. create new file with modeSave, but I also want to let user use already existing file. This should be done by same dialog. However, when I use modeSave and select existing file, there's a default alert that he is about to overwrite that file, which is little annoying. Is there a way how to suppress that alert or how can I create dialog that would allow me to select existing file or create the file if it doesn't exist?
I don't know if there's any way to suppress the replace warning, but I doubt it, since it's an OS window, and the warning itself may be from the OS. What's wrong with that behavior?
The plugin works this way: first insertion of citation into document, if the plugin does not know the document yet, add it to database and ask for the bibtex file associated with that document. I need to use modeSave in nsIFilePicker, because the bibtex file does not exist yet. One bibtex file can however be used in several documents, so when citing into a new document, the must be allowed to select existing file, but it's done with modeSave file picker. It's not a big problem, just annoying alert, that's meaningless.
I have an idea how to do it, but I wanted to see if there's simple way. So I will need to offer the user to choose from the bibtex files that he's been using before or create new one with modeOpen.
I will be adding new functionality to delete old records and at the same time I can add a function to add new bibtex files into the database (modeOpen) and then let the user choose from those (modeOpen).
I
If I use http instead of https, the link works.
I only tried it with "Save link as...", which worked. Silly of me. Changed.
I will publish the plugin at Firefox Addons when it's a bit more mature.
When I first cited a reference after defining the bibtex file, I got the following error and no citation.
Error: ckre.exec(text) is null
Source File: chrome://lyz/content/lyz.js
Line: 536
Tested with OS X 10.6
Also, I noticed a couple of minor issues that are probably more feature requests than bugs.
1) We often keep LyX and bibtex documents under subversion. If two people edit the same document and cite the same source, this creates a duplicate entry to the bibtex file.
2) Sometimes the imported citation information in Zotero is not correct. Normally I read through the bibliography and fix these after I have finished writing a paper. In the current version, however, there is no way to push edits done in Zotero to the bibtex file after initially exporting the reference.
In all, I think that even as it is now, this plugin greatly eases the use of Zotero with LyX.
many thanks for the response.
Ad Error: ckre.exec(text) is null:
Could you export the reference to bibtex and send me that? Did that happen with other references?
What do you mean by "seems to have some problems in detecting the active document", does the citation go into wrong document?
Ad 1)
Ha, I didn't really think of that. I assume that you have a unified cite key for all the authors. I keep that in mind, but no simple solution comes to me at the moment.
How do you add references into the bibtex file? If the writers don't check the bibtex file before adding new reference, they also would add duplicates.
Ad 2)
Excellent point and also a reason why I set out to write the plugin in the first place. I just forgot about it. I will add it soon.
Error: uncaught exception: [Exception... "Component returned failure code: 0x80520008 (NS_ERROR_FILE_ALREADY_EXISTS) [nsILocalFile.create]" nsresult: "0x80520008 (NS_ERROR_FILE_ALREADY_EXISTS)" location: "JS frame :: chrome://lyz/content/lyz.js :: anonymous :: line 585" data: no]
Steps to reproduce
1) Create two new blank documents with lyx, leave the first one active
2) Add a citation using and existing bibtex file
This results in the error above and no citation added.
What comes to my suggestions
1) We use a group library for the reference and the key format would be the same for all authors. Currently we just export the entire library as a bibtex file whenever a change is made and replace the existing file. With this new plugin I see two scenarios that can result in duplicates. (I assume that you know how subversion works.)
a) Two people edit the document at the same time and add the same citation. In this case subversion either detects that the bibtex files are identical and just uses one of these. If the authors also add other citations so that the bibtex files are no longer identical, this would create a conflict. So no problem here.
b) One person adds a citation and commits the bibtex file. A second person updates his working copy and now has a bibtex file containing the said reference. When he now cites this reference, it is added to the bibtex file again. This is a problem that can be solved only by your plugin.
2) Good to hear this. How do you plan to implement this?
Also, a third issue. Is it possible to use the same ID as Zotero uses for the citation key. The problem with the current approach is that if I correct a typo from the article name, the key changes. A related suggestion would be to have a couple of "hard coded" alternatives in the preference dialog for the citation key instead of the current text field. The last alternative could be called custom and choosing this would present the text box.
Your first error is perhaps already fixed. I've changed quite a bit, so hopefully the second error does not occur any more. Could you please try with the new version?
Ad 1a) I will need to read the bibtex file and update lyz database. This could only work if Zotero IDs are part of the bibtex file (as comments or part of citekey). This shouldn't be to difficult actually. But the unforeseen always makes itself seen. I'll think about it soon.
Ad 2) it's done in the new version. I have changed the version number to 1.0 because I had to change the database. I don't think any major change that would break backward compatibility is on the horizon. If that should happen, I will make the upgrade properly automatic, but at this stage that seemed unnecessary.
It's quite simple really. I store the zotero id in lyz database (something I should have been doing from the start), which will allow me to get the changed references from zotero and update the whole bibtex file.
Ad 3) that's certainly possible. I wanted to stick to "readable" cite keys, so the LyX interface could be used side by side with Zotero. Using zotero id's is in fact ideal. I don't want to touch the LyX document so the main constrain is the cite key cannot be changed or has to be manually updated in LyX doc. I could set the cite key to authoryear_zoteroid, this would make is sufficiently readable to find it in the Lyx dialog and would be rarely changed, assuming that the first authors name has to be correct.
Many thanks for the ideas
Petr
2) Do you plan for the plugin to automatically detect if references in Zotero database are changed or will there be a separate "synchronize bibtex"-button or menu item?
3) From the user interface perspective, citation keys are important only when default citations are used in LyX. If one uses natbib instead, Zotero will format the citations based on the bibtex file.
I will test the new version later today when I get to my office.
Zotero.Items.getLibraryKeyHash(item), Zotero.Items.parseLibraryKeyHash(libraryKey), and Zotero.Items.getByLibraryAndKey(libraryID, key) can help out with this.
understood.
2) not automatic, the menu how has "Update Bitex" item that does that.
3) I don't understand this point. What do you mean that Zotero will format the citations?
The use of the citation dialog will remain necessary at least for editing text before and after the citation, such as "p. 34" etc. This cannot be done from the outside (only inserting is possible, but not editing)
I didn't know that. I'll have a look.
many thanks
Petr
3) Instead of "Zotero will format..." I meant to write that LyX will format the citation based on the bibtex file when editing the LyX file. So the citation key is only shown in the citation dialog and here the full citation info is available. For users of natbib, the choice of bibtex keys is probably not a big issue. Making it optional to use "technical IDs" instead of human readable IDs would be great.
could you paste the error from firefox console. I tested on both windows and linux, firefox 3.5.5 and 3.5.6, without any issues.
3) understood. True, you don't really need to look through the citation dialog, because it's displayed automatically. I'm convinced.
Will have to learn about the keys a bit first.
after a brief look... so you say the key is not unique?
Many of my libraryIDs are actually null.
Thanks
OS X and FF 3.6
I was too hasty. I was running both firefoxes at the same time and that might have been the problem (I'll check that). Other than that it work on linux with firefox 3.5.5 and 3.6beta5.
If you're getting by date, Zotero.Items.getNewer(JSDate) might also be handy.