Posted by luc on Sat 23rd Oct 17:45 (modification of post by view diff)
View followups from luc | 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)
- #
- #Written by: J.Barney
- #Last updated: 22.10.10
- #Save working directory to come back to
- olddir="`pwd`"
- # we did not jet get any options
- interactive=TRUE
- # set default values for interactive usage
- PROG_NAME=$(basename $0)
- biblio=FALSE
- htmlnow=FALSE
- rtfnow=FALSE
- opennow=FALSE
- workdir=FALSE
- backup=FALSE
- while getopts bd:f:hor OPTION ; do
- interactive=FALSE
- case ${OPTION} in
- b) biblio=TRUE ;;
- d) workdir=$OPTARG ;;
- f) texfile=$OPTARG ;;
- h) htmlnow=TRUE ;;
- o) opennow=TRUE ;;
- r) rtfnow=TRUE ;;
- s) bachup=TRUE ;;
- \?) print -u2 "Usage: ${PROG_NAME} [ -bhors -d workdir -b bibfile -f texfile ]"
- exit 2;;
- esac
- done
- # echo $biblio
- # echo $workdir
- # echo $texfile
- # echo $htmlnow
- # echo $opennow
- # echo $rtfnow
- # echo $backup
- #Check directory
- if [[ $interactive = TRUE ]] ; then
- echo ""
- read -p "Is you latex project in the current directory? y|[n]" workdir
- echo ""
- fi
- #Select directory besides the working one
- if [[ $workdir =~ n|N|(no)|(No)|(FALSE) ]] || [[ -z $workdir ]]; then
- workdir=$(zenity --file-selection --directory --title="Select DIRECTORY of the .tex file");
- else workdir="$olddir"
- fi
- cd "$workdir"
- #Optional backup
- if [[ $interactive = TRUE ]] ; then
- read -p "Would you like to create a backup before compiling? [y]|n" backup
- fi
- if [[ $backup =~ y|Y|(yes)|(Yes)|(YES)|(TRUE) ]] || [[ -z $backup ]]; then
- workdir="`pwd`"
- mkdir -p "$workdir/backups"
- # why use "nice" here?
- nice tar cvpzf backups/backup_`date +%m_%d_at_%H_%M`.tar --exclude="$workdir/backups" "$workdir/*"
- else
- echo "Warning: We are NOT making a backup!"
- echo ""
- fi
- #Select .tex file
- if [[ $texfile = FALSE ]] ; then
- #Check to see if there is only one file -- use it if so."
- if [[ `ls *.tex | wc -l` -gt 1 ]]; then
- TEXLIST="`ls *.tex | sed 's/.tex$//'`"
- echo "Select .tex file"
- # TODO: warning this does not work with spaces in filenames!
- select texfile in $TEXLIST
- do
- break
- done
- else
- texfile="`ls *.tex`"
- texfile="{texfile%.tex}"
- echo "Using 1 found .tex file: $texfile"
- fi
- echo ""
- fi
- #Check for .bib
- if [[ $interactive = TRUE ]] && [[ -e *.bib ]] ; then
- read -p ".bib file found. Include it? [y]|n" biblio
- echo ""
- if [[ $biblio =~ y|Y|(yes)|(Yes)|(YES) ]] || [[ -z $biblio ]]; then
- #reset biblio for easy reuse (see further down)
- biblio=TRUE
- #Select if more than 1 .bib file
- if [[ `ls *.bib | wc -l` -gt 1 ]]; then
- BIBLIST="`ls *.bib | sed 's/.bib$//'`"
- echo "Multiple .bibs found, select one"
- # TODO: warning this does not work with spaces in filenames!
- select biblioname in $BIBLIST
- do
- break
- done
- echo ""
- else
- biblioname="`ls *.bib`"
- biblioname="{biblioname%.bib}"
- echo "Using 1 found .bib file: $biblioname"
- sleep 2s
- fi
- else
- #reset biblio for easy reuse (see further down)
- biblio=FALSE
- fi
- fi
- latex "${texfile}.tex"
- #biblio was reset to be either "yes" or "no"
- if [[ $biblio = yes ]] ; then
- bibtex "${biblioname}.bib"
- fi
- latex "${texfile}.tex"
- latex "${texfile}.tex"
- dvips "${texfile}.div"
- ps2pdf "${texfile}.ps" "${texfile}.pdf"
- echo ""
- echo "Compilation complete."
- echo ""
- if [[ $interactive = TRUE ]] ; then
- read -p "Create .rtf version now? [y]|n" rtfnow
- if [[ $rtfnow =~ y|Y|(yes)|(Yes)|(YES) ]] || [[ -z $rtfnow ]]; then
- latex2rtf "${texfile}.tex"
- fi
- echo ""
- fi
- if [[ $interactive = TRUE ]] ; then
- read -p "Create .html version now? [y]|n" htmlnow
- if [[ $htmlnow =~ y|Y|(yes)|(Yes)|(YES) ]] || [[ -z $htmlnow ]]; then
- latex2html "${texfile}.tex"
- fi
- echo ""
- fi
- if [[ $interactive = TRUE ]] ; then
- read -p "Open .pdf now? (Y/n): " opennow
- if [[ $opennow =~ y|Y|(yes)|(Yes)|(YES) ]] || [[ -z $opennow ]]; then
- evince "${texfile}.pdf" &
- fi
- 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.