Monday, February 8, 2016

Bibliography or Reference Management in LaTeX using Zotero Standalone

I use LaTeX for writing paper but it can be rather difficult, especially for beginners, to add citations and Bibliography. For this purpose I use Zotero Standalone which is an awesome software for reference management. It has a plugin for MS Word. However, for LaTeX, the citations need to be added manually. There are some automated tools available but I could not find an easy to use or dependable one. So I describe a process that you can use to add citations and bibliography directly in your LaTeX document from Zotero.

Adding Citations

Adding citations manually in LaTeX from Zotero is a pain. However, the process described below will make it a breeze for you. It contains the following steps:


  • Installing bibtex-citep.js

Download the bibtex-citep.js translator from here (http://pastebin.com/UNVexqKc). Great work by these guys! This translator will allow you to easily cite the paper using a simple drag-drop procedure in your LaTeX file. However, first we need to install it. Place the downloaded file in the "translators" folder in Zotero standalone.  You can access this folder by opening Zotero Standalone and navigating as (see image below): Tools > Preferences > Advanced (tab) > File and Folders (sub tab) > Show Data Directory. This data directory contains the "translators" folder.




  • Restart Zotero
  • For exporting citations as \cite{...} from Zotero, you would need to set the export mode of Zotero to bibtex-citep. You can do this as follows: In Zotero, Go to Tools > Preferences > Export (tab) > Select Export Format (drop down menu), select "bibtext-citep" and click OK.
  • Now you can add citations from Zotero directly. To do this you can select a paper in Zotero and drag-drop it in your opened LaTeX file in a text editor and it will create a \citep{...} citation automatically. If you want \cite{...} or \citet{...} you can manually edit the name of the field in your text editor. An alternative way is to click on the paper in Zotero to select it and then press Ctrl+shift+C to export the citation to the clipboard which can then be pasted anywhere you like.

Adding Bibliography

Adding bibliography requires a simple export of the Zotero Library as BibLaTeX. This can be easily accomplished by Clicking on File > Export Library. An "export" dialog will appear and you can simply select "BibLateX" as the format and click OK.It will ask you where to save the bib file and what name to give it. You can choose any name (say, "mybib.bib") for it and save it in the directory containing your LaTeX sources. You can then simply add "\bibliography{mybib}" to your LaTeX file and compile your LaTeX source.

If you do not want to export your whole Zotero library, you can choose to create a collection or add specific tags in Zotero to the papers that you cite or use in your LaTeX document and then export only that collection by right clicking on the collection in the left pane of Zotero Standalone. You can read more about Collections and tags hereAlternatively you can use the following python script to get a cleaned up bib file containing the bib entries for the citations you have in your paper. It takes the zotero exported bib file and a bbl file generated by LaTeX from the zotero bib file to create another bib file containing only the cited entries. This output file can then be used in your LaTeX document.

Enjoy!


# -*- coding: utf-8 -*-
"""
Created on Sun Jan 18 12:23:36 2015

@author: Afsar
"""
from pyparsing import nestedExpr
import re

bbl = '.\paper.bbl' #input bbl file
bibin = '.\mybib.bib' #zotero bib file 
bibout = 'outbib.bib' #output file containing only the citations used

B = []
with open(bbl,'r') as fbbl:
    for ln in fbbl:
        if 'bibitem' in ln:
            B.append(ln[ln.find('{')+1:-2])

fields = []
i=-1
with open(bibin,'r') as fbibin:
    for ln in fbibin:
        if ln and ln[0] == '@':
            i+=1
            fields.append('')
        if i<0:
            continue
        fields[i]+=ln

entries = {}       
for f in fields:
    hdr = f.split(',')[0].split('{')[1]
    entries[hdr]= f

with open(bibout,'w') as fout:
    for b in B:
        try:
            fout.write(entries[b])
        except KeyError as e:

            print e,':',b



1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete