[Tex/LaTex] In TeXShop, is there a directive to select biber or BibTeX on a per-document basis

biberbibliographiesbibtextexshop

As in the title, I'd like to be able to select whether TeXShop uses biber or BibTeX in the document rather than using the command line or changing the default in the TeXShop preferences.

For TeX engines this is easy; i.e. you can put the following somewhere near the top of your TeX file

% !TEX TS-program = pdflatex

to tell TeXShop to compile with pdflatex, regardless of the default in the preferences. Does such a directive exist for bibliography passers? I want to be able to hit ++B for this. Latexmk provides the ability to do this, but I want to do compilations manually.

Final note

The solution involved a shell script. I've made this available on GitHub, so please feel free to use and improve it.

Best Answer

I solved this is a mildly hacky way. First of all, I wrote a script called TeXShopBib.sh which looks like

#!/bin/sh

filestem=${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."
fi

eval $bibtype $filestem

and placed it somewhere TeXShop will find it (just /usr/texbin for quick testing in my case). Make sure the script's executable bit is set. If you have saved the script in /usr/texbin then you need to issue the command:

sudo chmod +x /usr/texbin/TeXShopBib.sh 

Then I simply altered the preference to pick up this script as the BibTeX engine.

enter image description here

If the TeX file has a line that looks like

% !BIB TS-program = biber

or

% !BIB TS-program = bibtex

in the first 20 lines then it figures the rest out and works properly with the TeXShop console, in a similar way to how TeXShop handles TeX engines. This script crudely defaults to BibTeX when nothing is detected in the tex file, but is no more sophisticated than necessary for my purposes. If there is any interest in this then I can make it more robust.