Writing a Law PhD with Latex (5)

Tue 01 July 2014 | tags: Latex, Free Software, PhD, Social Sciences, Oscola, -- (permalink)

What do we need?

There are a few things our compile scripts need to do. For the thesis document, we need to insert all chapter titles, and for the separate chapter generator, we need to replace both placeholders in the file with the proper one. After that is done, we need to compile our document, and to do that properly with the bibliography, PDFLatex needs to be run a few times, together with "Biber", the bibliography generator in this order:

pdflatex texfile
biber texfile
pdflatex texfile
pdflatex texfile

Latex creates a lot of temporary files, and with each run it can puzzle more pieces together. After those three runs, the document together with the bibliography, chapter headings, cross references and everything else, is complete.

The actual scripts

thesis.sh

For the whole thesis, we generate a new texfile first to replace the placeholder with the appropriate title that we will save in a file called title in the chapter folder. All of this will run perfectly fine under Linux and should run under MacOS and under Windows with the help of Cygwin. Let me know if you are having problems! Simply save the file as "thesis.sh".

#!/usr/bin/env bash

home=`pwd`
texfolder="$home/text"
pdffolder="$home/output"

cd $texfolder

tex=$(<thesis.tex)

for i in {1..9}
do
    if [ -d "chapter_$i" ]; then
        title=`head chapter_$i/title`
        tex=${tex/chaptertitle_$i/$title}
    fi
done

echo "$tex" > thesis-tmp.tex

pdflatex thesis-tmp
biber thesis-tmp
pdflatex thesis-tmp
pdflatex thesis-tmp

mv thesis-tmp.pdf $pdffolder/thesis.pdf

cd $home

What does all this do? Let's go through it line by line:

  1. All this does is to tell which shell to use. Probably not necessary, but it can't hurt
  2. Execute the pwd command (which returns the current directory) and save the result into the variable "home"
  3. Save the location for the directory containing the texfiles and for the directory where the finished pdf should end up. $home simply means the content of the "home" variable we defined before.
  4. Change into the texfolder
  5. Save the content of the file "thesis.tex" into the variable "tex"
  6. For every number from 1 to 9 (feel free to adapt this should you have more chapters), execute the following:
    1. Test, if the directory with the running number ($i) exists.
    2. If that's the case, execute the head command on the file called title and save the outcome (the first line of the file) into the title variable.
    3. Replace the word chaptertitle_$i (the current number) with the title variable, save the content into the same tex variable
  7. Write the content of the tex variable into a new file (thesis-tmp.tex)
  8. Execute the different Latex commands as outlined above on the modified file
  9. Move the pdf file into the "output" folder
  10. Go back to the home folder

Make the file executable by typing on the command line chmod u+x thesis.sh.

chapter.sh

The script for the chapters is quite similar, with one major difference: you will need to specify which chapter you want to generate when you start the script. Instead of typing ./thesis.sh into the command line, you will need to type ./chapter.sh 2 if you want to generate a pdf from chapter 2. In bash scripting, the variable that is used for that is $1 - the first parameter after the script's name itself.

First, the script will test whether a parameter was given and whether that chapter exists, then it will run through similar steps as before: replacing the placeholders with the correct title and the number given by the user:

#!/usr/bin/env bash

if [ -z "$1" ]; then
    echo "Please specify which chapter (for example \"./chapter.sh 1\")"
    exit 1
fi

home=`pwd`
texfolder="$home/text"
pdffolder="$home/output"

if [ ! -d "$texfolder/chapter_$1" ]; then
    echo "This chapter does not exist"
    exit 1
fi


cd $texfolder

tex=$(<chapter.tex)

title=`head chapter_$1/title`
tex=${tex/chaptertitle/$title}
tex=${tex/chapternumber/$1}

echo "$tex" > chapter-tmp.tex

pdflatex chapter-tmp
biber chapter-tmp
pdflatex chapter-tmp
pdflatex chapter-tmp

mv chapter-tmp.pdf $pdffolder/chapter-$1.pdf


cd $home

Again - make the file executable, commit the changes and you're done!

chmod u+x chapter.sh
git add .
git commit -m "Wrote scripts to generate pdfs"