Feb 28, 2011

TeXShop and BibTeX for lazy guys - automatic call of bibtex

If you want to write a larger scientific document, like a diploma or doctoral thesis, you surely will use bibtex for creating your bibliography (and especially for the German guys: don't just copy and paste... but really use a good bibliography).

Just for completeness, a very nice and free client for working with a bibtex database is JabRef, which is available for MacOS, Linux and Windows.

If you work with TeXShop and use bibtex, compiling your document is quite hard work: first you need to set the texmode to "LaTeX", compile it, set it to "bibtex", compile it, set it back to "LaTeX" compile it. Indeed, a lot to point and click. The following tip will add an entry to your TeXShop called "lat-bib-lat", which does exactly this procedure for you:

  1. Open "Terminal". This will give you the nerdy mysterious window with the cursor line.
  2. Type in (or copy this line, and do a ctrl+click at the cursor position and paste it) and press return afterwards
    cd Library/TeXShop/Engines/
    This changes the console to the folder Library/TeXShop/Engines in your home folder.
  3. Create a file named lat-bib-lat.engine in that specific folder by casting the following command closed by the return key in the terminal window
    vi lat-bib-lat.engine
    This creates the file and opens it in a console text editor, so you will see nothing than a cursor with many tildes below.
  4. Press the "i"-key once. In the last line of the window "-Insert-" should appear (so you just switched the text editor to insert mode)
  5. Copy the following stuff and paste it by an ctrl + click at the cursor position in the Terminal
    #!/bin/bash
    pdflatex "$1"
    for file in *.aux ; do
    bibtex $file
    done
    pdflatex "$1"

    pdflatex "$1"
    This is a so-called script which does exactly what you want: execute latex, do bibtex and execute latex again.
  6. Press the "Esc" key (which ends the insert mode) and write ":wq" (without quotation marks) followed by a keystroke of return which tells the editor to Write the file and Quit. You now should be back at the console as it was before the "vi"-command.
  7. Make the script executable with the command (press return at the end)
    sudo chmod a+x lat-bib-lat.engine
    You will be prompted for your password. Sudo is the command to become SUperuser and DO the following command. The command chmod a+x makes the file executable (x) for all.
  8. Restart TeXShop - you will now find an entry called lat-bib-lat. If you choose it, your document will be latexed, bibtexed and latexed again
Happy bibtexing.

Remark:
Apparently you need two runs of pdflatex in order to get the correct citations, so the script does two calls of pdflatex after calling bibtex.

Update:
The lat-bib-lat.engine can be found in the drop-down menu where you can choose the profile to be run as can be seen here:

 

19 comments:

poet said...

Thanks for the advice. I'm kind of disappointed, though, by your misleading blog title. I was hoping the people behind it would be some kickass nerd girls (who also happened to be Greek but living in Germany, or whatever)... turns out it's just another guy demeaning and objectifying women. 'cos that's what you're doing if you name an object after a woman and subsequently refer to said object as a bitch...

DaWuzzzz said...

Thanks for this 'genious' comment. Just to let you know: the name was actually created by a woman who was the creator of the partner blog (which is now called eumenidae.blogspot.com for reasons you can read over there) and not by myself. We just took the German version of it to show the interconnection. And I would be very careful to blame other people of objectifying women (especially if you don't know them personally and know how their relation to women is...)

Best regards

avocadohead said...

Just for the record: I do kick ass. Preferably daWuzzzz's. Ὁ μὴ δαρεὶς ἄνθρωπος οὐ παιδεύεται.
And while I do commend your ardour, petitioning for equal opportunities in Greek mythology looks like a lost cause to me.

The Youd said...

Thanks for this helpful tip. However I can't find where the engine appears? Yes the lat-bib-lat.engine file is in the correct folder as an executable and I restarted TexShop. I also checked that the script works by putting this line in my source:
% !TEX TS-program = lat-bib-lat
But I still can't see where in TexShop (including preferences) this option appears. Any help appreciated.

DaWuzzzz said...

Hey "The Youd",

here you are, I've updated the blog article with a screenshot. You should find the entry "lat-bib-lat" in the dropdown menu, where you can choose the executed profile (normally set to "LaTeX")

Ankit said...

Hi, nice blog. I did all steps mentioned in the post. Even compiles but could not able see biblography in my document. Can u please help me with what possible thing may go wrong.

Dan Jones said...

Very helpful, thanks!

Neel J. Bhagodia said...

RE: Ankit

I had the same problem as you. As I was continually adding items to my bibliography, I found I had to compile bibtex first before using lat-bib-lat. This is easily remedied by a small addition to the engine file you create in terminal:

#!/bin/bash
for file in *.aux ; do
bibtex $file
done
pdflatex "$1"
for file in *.aux ; do
bibtex $file
done
pdflatex "$1"
pdflatex "$1"

MG said...

Thank you so much, I'm writing my thesis and compiling the bibliography was very annoying, now I can focus on writing without worrying about that troublesome situation.
Again, thanks.

Geoffrey said...

Thank you for this useful piece of code. I am also writing my thesis with two weeks left. I have inserted all my citations and using a single bibtex library that I call in each chapter. However I keep getting question marks in place of numbers but the list of references in each chapter is still displayed at the end. Any ideas on how to fix this?

Geoffrey said...

Thank you for this useful piece of code. I am also writing my thesis with two weeks left. I have inserted all my citations and using a single bibtex library that I call in each chapter. However I keep getting question marks in place of numbers but the list of references in each chapter is still displayed at the end. Any ideas on how to fix this?

G. said...

Is there also a way to trash the aux files immediately after the last pdflatex command?

Unknown said...

Really good post, mate. Cheers!

Unknown said...

Great. Works perfectly.

Unknown said...

Hi every one,

A little improvement for the one interested:

#!/bin/bash
pdflatex "$1"
for file in *.aux ; do
bibtex $file
done
pdflatex "$1"
pdflatex --synctex=1 "$1"

The `--synctex` option in the last compilation allows you to jump from pdf to corresponding line on .tex file (and vice-versa). In mac for example it will be (cmd + click).

Anyway, very useful trick! Thank guy!

Unknown said...

\HUGE Thank you !!

FRei said...

$ cd ~/Library/TeXShop/Engines/

$ subl lat-bib-lat.engine

#!/bin/bash
pdflatex "$1"
for file in *.aux ; do bibtex "$file"; done
pdflatex "$1"
pdflatex --synctex=1 "$1"
#See it : "$file" : names w/ space: corrigido por mim em 2020JUL10

$ sudo chmod a+x lat-bib-lat.engine

restart texshop :)

Carlo said...

Many thanks for this little trick, it has at this point probably saved me hours I would otherwise have spent switching back and forth between Latex and Bibtex.

This being said, I just encountered a problem when I tried installing the command on a new (late 2020) MacBook with Big Sur pre-installed. I can create the new engine without problems, it also appears in TexShop and the first part (LaTeX - BiBTex) runs fine. It does crash when it starts the last two runs of LaTeX and returns the following error (replace 'filename' with actual name of .tex file):

! I can't find file 'filename.tex
pdflatex'.

<*> filename.tex
pdflatex

filename.tex
(Press Enter to retry, or Control-D to exit)
Please type another file name:

It seems to me that there's some confusion with the quotation marks, but I can't figure it out (neither an expert with the Terminal nor with Latex-hacks). If I run everything by hand, it works fine.

Any help would be greatly appreciated!!

DaWuzzzz said...

Hey Carlo,

as far as I know big sure changed the standard shell, but since I don't have a machine running Big Sure I can't help you there. Maybe changing the standard shell is possible.