4

I'm using Mac OS 10.9.3, TeXLive-2013 and Scrivener to write my papers. I'd like to use Apple's Automator in order to create a service (available from the Finder's right-click menu over a file) called "Compile TeX file" that runs a simple command using the selected file as an input.

Exemple:

  1. Inside a folder (/Volumes/Data/TeX_docs/) I've an exemple.tex file created by Scrivener that is ready for compiling with LaTeX.
  2. I'd like to right click this file and from the Finder's Service menu choose the option "Compile TeX file"
  3. Once chosen, this service will execute the command latexmk -pdf -bibtex exemple.tex and so compile the .tex file inside the same folder, creating a .pdf file (This Terminal command is working perfectly right now, so the need to automate this action!)
  4. After compiling the .tex file, a dialog box appears and asks if I want to delete all the temporary files with a Yes or No option. If Yes is chosen, it deletes all TeX temp files (.aux,.bbl,.bcf,.fdb_latexmk,.fls,.log,.out,.run.xml) and keep only the original exemple.tex and the new exemple.pdf file. Otherwise, it does nothing more and I have all the files in the end!
  • I give up. Debugging this is impossible when all I get is a "some error occured". dropbox.com/sh/3crkhyf9cez09eu/AABo33ISjsW4xEwh8Hw_w8_ba If someone wants to give it a shot. My problem was to actually get alle the files in the same directory as the .tex-file, something that proved pretty damn hard (for me atleast). – Argo May 24 '14 at 10:24
1

This is not the optimal solution, but it's working for now. Here is the Automator window:

Automator actions

The first shell script compiles the .tex file using the sequence pdflatex - biber - pdflatex and contains the following code:

for f in "$@"
do 
    file_ext=${f##*.}
    if [ "$file_ext" = "tex" ]
    then
        foldername=$(dirname "$f")
        export foldername
        filename=$(basename "$f")
        filesimple=${filename%.tex*}
        cd $foldername
        /usr/local/texlive/2013/bin/x86_64-darwin/pdflatex "$filename"
        /usr/local/texlive/2013/bin/x86_64-darwin/biber "$filesimple"
        /usr/local/texlive/2013/bin/x86_64-darwin/pdflatex "$filename"
    else
        echo $f n\'est pas un fichier TeX
    fi
done

The second shell script deletes all temp files and opens the .pdf file. It contains the following code:

for f in "$@"
do 
    file_ext=${f##*.}
    if [ "$file_ext" = "tex" ]
    then
        foldername=$(dirname "$f")
        filename=$(basename "$f")
        filesimple=${filename%.tex*}
        cd $foldername
        rm $filesimple.aux
        rm $filesimple.bbl
        rm $filesimple.bcf
        rm $filesimple.blg
        rm $filesimple.log
        rm $filesimple.run.xml
        open $filesimple.pdf
    else
        echo $f n\'est pas un fichier TeX
    fi
done

I'm still waiting for a better solution using the latexmk command!

1

This isn't the prettiest solution, but it gets some of the way towards what you wanted (i.e., using latexmk and a confirmation dialog):

First, your first script can be substituted for something like this:

for f in "$@"
do
    file_ext=${f##*.}
    if [ "$file_ext" = "tex" ]
    then
        foldername=$(dirname "$f")
        filename=$(basename "$f" .tex)
        cd "$foldername"
        /usr/texbin/latexmk -pdf -bibtex "$filename"
        open "${filename}.pdf"
    else
        echo $f n\'est pas un fichier TeX
    fi
done

Note that I've added the open command to the first script, so that the second script is only responsible for cleaning up the temporary files. Also, if you want to simplify things further, you could create a .latexmkrc in the same directory as your generated TeX files with the following:

$pdf_mode = 1; # equivalent to -pdf
$bibtex_use = 2; # equivalent to -bibtex

And then simplify the latexmk command to /usr/texbin/latexmk "$filename". Then, before the second Get Finder Items step, add an Ask for Confirmation step. where you can enter the message text and change the buttons to "Yes" or "No". NB, that the "OK" button should be changed to "Yes", assuming you want "Yes" to be the default option.

Finally, you can change your second script to something like this:

for f in "$@"
do
    file_ext=${f##*.}
    if [ "$file_ext" = "tex" ]
    then
        foldername=$(dirname "$f")
        filename=$(basename "$f" .tex)
        cd "$foldername"
        /usr/texbin/latexmk -c "$filename"
        rm -f "$filename".bbl
        rm -f "$filename".run.xml
    else
        echo $f n\'est pas un fichier TeX
    fi
done

-c is the option to run latexmk in clean-up mode, but it won't delete your pdf. Note that I still needed to manually delete the .run.xml and .bbl files. I don't quite understand why latexmk isn't cleaning up the .bbl file.

Something closer to a solid solution would likely have to involve some more scripting and breaking this up into multiple workflows to, e.g., gracefully handle the case where compilation fails and display the log file for the user. Plus, having an independent service which can clean-up the TeX auxiliary files might be nice. But, as far as simplicity goes, this is the best I can think of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.