In TeXShop, is there a directive to select biber or BibTeX on a per-document basis?

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.

Solutions Collecting From Web of "In TeXShop, is there a directive to select biber or BibTeX on a per-document basis?"

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.

Beginning with version 3.21, this feature is provided (by this very request!).

From the release notes:

The next feature was requested by Alan Munn about a year ago.
Apologies for the delay. In the meantime, Mark Everitt wrote an
ingenious script to provide this feature. The feature provided by that
script is now built into TeXShop.

The command BibTeX in the TeXShop typeset menu runs BibTeX; notice
that this command has a keyboard shortcut. In Japan, a different
program is used instead, so Yusuke Terada provided an item in TeXShop
Preferences under the Engine tab to select the program to be run when
this menu item is selected. Examples are bibtex, biber, pbibtex, etc.
The Preferences item can also be used to add flags to the command.

In TeXShop 3.21, the BibTeX engine can be selected on a file-by-file
basis using the syntax

% !BIB TS-program = 

The alternate syntax

% !BIB program = 

will also be accepted. The item after the equal sign gives the name of
the program (for instance biber) and any required flags. This line
should be written within the first twenty lines of a source file.

The % !BIB TS-program = line takes precedence. If it is absent, the
Preference item determines which command is run.

So, all you need to do is update your copy of TeXShop (it should have asked you to do so).

Unfortunately no. This would be a very useful thing to have, but it doesn’t exist at the moment. There’s been some discussion of it on the TeX on MacOS mailing list, but nothing came of it on the development side.

Here’s a hacky kind of workaround:

  • Make a biber engine
  • At the top of your file put:
    %# !TEX TS-program = biber
    % !TEX TS-program = pdflatex (or whatever regular engine the file uses)

When you want run biber on the file, remove the # from the first line and hit +T; since the biber engine is the first line, it will be run first.

This isn’t as convenient as having the engine run automatically with ++B, but it helps a bit.