## Fun with BibTeX and biber: Switching in TeXShop/TeXWorks and filtering external resources

Posted on February 12 2012 21:04:48 JST by Mark S. Everitt

I'm an avid LaTeX user, and I've been watching the develoment of Biblatex with a great deal of interest. Sadly, a lot of the work I do uses the REVTeX class which is bound to BibTeX, but for some reports I did recently I took the opportunity to check Biblatex out. This entry is split into two parts. The first is about getting biber, an advanced bibliography engine that Biblatex can use, to clean up a remotely acquired .bib file from CiteULike with regular expressions. The second part is about getting TeXShop and TeXWorks (and actually any TeX shell) to use BibTeX or biber using a directive within the LaTeX file itself.

## Grabbing and filtering remote bibliography files with biber

My current workflow uses a bash script to grab a bibliography file from CiteULike, and filters it through a sed script to tidy up some problems with the code and strip out anything that I don't need to keep it clean. Biblatex using the biber backend has a nice feature that allows it to grab a remote file automatically, so I asked a question on TeX.sx about whether of not a file could be grabbed and then sent to a script for filtering. It turned out that the answer was no, but the maintainer himself answered the question and added Perl regular expressions support into biber for me! This is even better, because the script isn't needed anymore. Instead, a local biber.conf file tells biber how to filter the remote file before using it.

The file I used was

<config>  <sourcemap>    <maps datatype="bibtex" bmap_overwrite="1">      <map maptype="field">        <map_pair map_source="PAGES" map_match="\+" map_replace=""/>        <map_pair map_source="DOI" map_null="1"/>        <map_pair map_source="URL" map_null="1"/>        <map_pair map_source="ABSTRACT" map_null="1"/>        <map_pair map_source="ISSN" map_null="1"/>        <map_pair map_source="EPRINT" map_null="1"/>      </map>    </maps>  </sourcemap></config>

which removes + symbols from PAGES fields, and simply removes the fields DOI, URL, ABSTRACT, ISSN and EPRINT. In the reports I was writing, the LaTeX changes a little compared with LaTeX using BibTeX. Romoving fields can be done more simply with optional arguments when adding the biblatex package in the LaTeX source, but I like to have clean BibTeX files. In the preamble I used the following lines:

\usepackage[style=nature,backend=biber,maxnames=15]{biblatex}\addbibresource[location=remote]{http://www.citeulike.org/bibtex/user/MarkEveritt/tag/final_report_2011}

and the command

\printbibliography

where I wanted the bibliography to be printed. Biber must be invoked instead of BibTeX of course.

## Getting TeXShop or TeXWorks to use biber or BibTeX with a switch

As I mentioned earlier, I have to use BibTeX for some of my documents, which means that if I want to use biber then I have to be able to use either easily. For TeX engines in TeXShop and TeXWorks this is easy. For example:

% !TEX TS-program = pdflatex

at the top of the file tells TeXShop to compile using pdflatex. No such facility exists for bibliography engines, so the options were either to keep a terminal open all the time, or change the bibtex engine in the preferences every time I have to switch. To verify that this was the case and I wasn't missing something I asked a question on TeX.sx. I was right, but there was a few hints I hadn't considered. I decided to solve the problem myself.

I wanted to have a line at the top of my file just like that used to select an engine. I decided on the syntax

% !BIB TS-program = biber

where the selected bibliography engine is on the right of the equality symbol. To handle this command, I wrote a short bash script.

#!/bin/sh# TeXShopBib.shfilestem=${1%.*}bibtype=head -n20 "${filestem}.tex" \| sed -n 's/\%[ ]*![ ]*BIB[ ]*TS-program[ ]*=[ ]*$$[a-z]*$$/\1/p'if [ -z $bibtype ]; then echo "No option detected in TeX file. Defaulting to BibTeX." bibtype="bibtex"else echo "Option$bibtype detected in TeX file."fieval $bibtype$filestem

This script searches the top 20 lines of the file for the line, and using that runs biber or BibTeX. If the line is not found, it defaults to BibTeX. This is not a particularly sophisticated script, and can certainly be enhanced. It does work though. All that is needed is to place this script somewhere that TeXShop or TeXWorks will find it, and change the BibTeX Engine field in the perferences to TeXShopBib.sh. Now when the hot key combination ⇧+⌘+B is typed, the rest is automatic! As an added bonus, this will work for any TeX shell that can be told to use TeXShopBib.sh as the BibTeX engine. The script is available on github.