Posted by jmbarnes on Sat 23rd Oct 18:48 (modification of post by view diff)
download | new post
- #!/bin/bash
- #
- #A script to compile .tex files to .pdf (and optionally, .rtf or .html)
- #with bibliography support.
- #
- #Depends on: latex, bibtex, dvips, ps2pdf,
- #Suggests: latex2rtf, latex2html, evince (or any other pdf viewer)
- #
- #Started by: jmbarnes
- #moved along by: luc
- #
- #Last updated: 23.10.10
- #
- #USAGE:
- #Accepts the following command line arguments:
- #-d DIRECTORY -- directory to work from
- #-f TEXFILE -- name of .tex file to process
- #-b BIBFILE -- use named .bib file.
- #-s -- create a .tar backup
- #-h -- create an html version using latex2html
- #-r -- create an rtf version using latex2rtf
- #-o -- open the .pdf when finished
- #
- #EXAMPLE:
- #./latexcompile.sh -d ~/Dropbox/Paper -f thesis.tex -b references.bib -ho
- #This would: use "thesis.tex" from the Paper directory, process the
- #"references.bib" file, create an html version and open the .pdf. No .rtf would be created
- #
- #Save working directory to come back to
- OLDDIR="`pwd`"
- PROG_NAME=$(basename $0)
- #Set variables
- while getopts d:f:b:hors OPTION ; do
- case ${OPTION} in
- b) biblioname=$OPTARG
- biblio=y ;;
- d) workdir=$OPTARG ;;
- f) texfile=$OPTARG ;;
- h) htmlnow=y ;;
- o) opennow=y ;;
- r) rtfnow=y ;;
- s) backup=y ;;
- \?) print -u2 "Usage: ${PROG_NAME} [ -bhors -d workdir -b bibfile -f texfile ]"
- exit 2;;
- esac
- done
- #Check directory if necessary
- if [ "$workdir" = "" ]; then
- echo ""
- read -p "Is you latex project in the current directory? y|[n] :" switchdir
- echo ""
- else
- cd $workdir
- fi
- #Select directory besides the working one
- if [ "$switchdir" = "n" ] || [ "$switchdir" = "N" ] || [ "$switchdir" = "no" ] || [ "$switchdir" = "No" ]; then
- texdirectory=$(zenity --file-selection --directory --title="Select DIRECTORY of the .tex file");
- cd $texdirectory
- fi
- #Optional backup
- if [ "$backup" = "" ]; then
- read -p "Would you like to create a backup before compiling? y | [n] : " backup
- fi
- case $backup in
- y | Y | yes | Yes )
- CURDIR="`pwd`"
- if [ ! -e "$CURDIR/backups" ]; then
- echo "Backups directory not detected, making one..."
- mkdir $CURDIR/backups
- fi
- BACKUPDATE=`date +%m_%d_at_%H_%M`
- cd $CURDIR/backups
- nice tar cvpzf backup_$BACKUPDATE.tar --exclude=$CURDIR/backups $CURDIR/*
- cd $CURDIR
- ;;
- n | N | no | No )
- echo "Warning: NO backup being made."
- echo ""
- ;;
- esac
- #Select .tex file
- if [ "$texfile" = "" ]; then
- if [[ `ls *.tex | wc -l` -gt 1 ]]; then
- TEXLIST="`ls | grep .tex | sed 's/\(.*\)\..*/\1/'`"
- echo "Select .tex file"
- select texfile in $TEXLIST
- do
- break
- done
- else
- texfile="`ls | grep .tex | sed 's/\(.*\)\..*/\1/'`"
- echo "Using 1 found .tex file: $texfile"
- fi
- echo ""
- fi
- #Check for .bib
- if [ "$biblioname" = "" ]; then
- if [ -e *.bib ]; then
- read -p ".bib file found. Include it? (y/n): " biblio
- echo ""
- if [ "$biblio" = "y" ] || [ "$biblio" = "Y" ] || [ "$biblio" = "yes" ] || [ "$biblio" = "Yes" ]; then
- #Reset biblio for down-script ease
- biblio="yes"
- if [[ `ls *.bib | wc -l` -gt 1 ]]; then
- BIBLIST="`ls | grep .bib | sed 's/\(.*\)\..*/\1/'`"
- echo "Multiple .bibs found, select one"
- select biblioname in $BIBLIST
- do
- break
- done
- echo ""
- else
- biblioname="`ls | grep .bib | sed 's/\(.*\)\..*/\1/'`"
- echo "Using 1 found .bib file: $biblioname"
- sleep 2s
- fi
- fi
- fi
- fi
- #The actual compiling bit
- latex $texfile.tex
- if [ "$biblio" = "yes" ]; then
- bibtex $biblioname.bib
- fi
- latex $texfile.tex
- latex $texfile.tex
- dvips $texfile.dvi
- ps2pdf $texfile.ps $texfile.pdf
- echo ""
- echo "Compilation complete."
- echo ""
- #Create rtf?
- if [ "$rtfnow" = "" ]; then
- read -p "Create .rtf version now? (y/n): " rtfnow
- fi
- if [ "$rtfnow" = "y" ] || [ "$rtfnow" = "Y" ]; then
- latex2rtf $texfile.tex
- fi
- echo ""
- #Create html?
- if [ "$htmlnow" = "" ]; then
- read -p "Create .html version now? (y/n): " htmlnow
- fi
- if [ "$htmlnow" = "y" ] || [ "$htmlnow" = "Y" ]; then
- latex2html $texfile.tex
- fi
- echo ""
- #Open pdf?
- if [ "$opennow" = "" ]; then
- read -p "Open .pdf now? (Y/n): " opennow
- fi
- if [ "$opennow" = "Y" ] || [ "$opennow" = "y" ]; then
- evince $texfile.pdf &
- fi
- cd $OLDDIR
- exit 0
- #end
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.