Sunday, February 15, 2015

Make Zotero export to bibtex handle latex math

The default bibtex export module for Zotero is not great at handling math in titles. The only good solution I have found is to change the manually change the titles from using Unicode symbols (γp→ϕp) to using latex math ($\gamma p \to \phi p$). Then you need to modify the bibtex export module so that it will output your math verbatim without escaping it. Modify the file [Firefox profile]/zotero/translators/BibTeX.js. Change
var alwaysMap = {
  "|":"{\\textbar}",
  "<":"{\\textless}",
  ">":"{\\textgreater}",
  "~":"{\\textasciitilde}",
  "^":"{\\textasciicircum}",
  "\\":"{\\textbackslash}",
  "{" : "\\{",
  "}" : "\\}"
};
to
var alwaysMap = {
  "|":"{\\textbar}",
  "<":"{\\textless}",
  ">":"{\\textgreater}",
  "~":"{\\textasciitilde}"//,
  //"^":"{\\textasciicircum}",
  //"\\":"{\\textbackslash}",
  //"{" : "\\{",
  //"}" : "\\}"
};
and in the function writeField() change
    value = value.replace(/[|\<\>\~\^\\\{\}]/g, mapEscape).replace(/([\#\$\%\&\_])/g, "\\$1");
to
    value = value.replace(/[|\<\>\~]/g, mapEscape).replace(/([\#\%\&])/g, "\\$1");
    //extra protect all math against capitalization
    value = value.replace(/(\$.*?\$)/g,"{$1}");
The last line is needed to protect math from being auto-capitalized or auto-uncapitalized with two pairs of brackets. Without this line it's only surrounded by one pair of brackets, which is not always enough. Before fixing this, I had a problem where BibTeX kept changing \Lambda to \lambda, uncapitalizing my greek letters in inappropriate places.

References:
https://ohadsc.wordpress.com/2012/06/20/zotero-exporting-unicode-and-latex-constructs-to-bibtex/
https://forums.zotero.org/discussion/5324/bibtex-and-greek-characters/