Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ## Todo:
- ## *) Preview for selected text? Check latex-suite.
- ## *) Option for live-preview? Problem: Okular blinks when reloading file.
- ## Might distract the user.
- ## *) Embed the shellscripts makelatex2 and deljunk in tex_makelatex.vim
- ## file. Possible?
- ## *) :cprev and :cnext to jump to errors. Also read :make and :errorformat
- ## *) Check for latexmk version requires 4.25 or higher
- ## *) [Done] Better error messages
- ## *) [Done] Show/don't show underfull/overfull \hbox warning messages.
- ## *) [Done] Update help
- ## *) [Done] Stop from running further compilation if there is error
- ## in first run.
- ## *) [Done] Some rc file via which options can be changed easily. This rc file
- ## has to be parsed after cli line arguments has been parsed so that
- ## user can easily change options without changing the vim script from
- ## which this script is called.
- ## *) [Done] Boolean to parse rcfile before and after cli arguments
- ## *) [Done] Show/don't-show warning messages.
- ## *) [Done] Which log file should one parse, when the project is
- ## multi-document? Does sub-documents even have log files? How to
- ## specify which sub-document has an error.
- ## -> Only master.log has to be checked.
- ## *) [Done] Show/don't-show error messages
- ## *) [Done] Forward and reverse search for dvi file
- ## Forward and reverse search is not possible with ps files
- ## *) [Done] Switch to call external scripts tex2html and update pdf+html file
- ## *) [Done] Switch to tell not to open pdf file.
- ScriptName=$(basename $0)
- Author="Anjishnu Sarkar"
- Version="0.7"
- RCFile="$HOME"/.makelatexrc
- ParseRCFile="true"
- Compile="true"
- DocumentViewer="okular --unique"
- ErrorMsg="true"
- WarningMsg="true"
- WarnBoxMsg="false"
- View="true"
- ## Errors messages are verbose
- # ltxoptions="-synctex=1 -interaction=nonstopmode -shell-escape"
- ltxoptions="-synctex=1 -interaction=batchmode -shell-escape"
- ## Errors messages are a compact
- # ltxoptions="-file-line-error -synctex=1 -interaction=batchmode -shell-escape"
- pdflatexcmd="pdflatex "$ltxoptions""
- latexcmd="latex -src-specials "$ltxoptions""
- xelatexcmd="xelatex "$ltxoptions""
- # latexmkcmd="latexmk -g -dvi -latex="$latexcmd" "$RootFileName""
- # pdflatexmkcmd="latexmk -g -pdf -pdflatex="$pdflatexcmd" "$RootFileName""
- # xelatexmkcmd="latexmk -g -pdf -pdflatex="$xelatexcmd" "$RootFileName""
- HelpText="Wrapper script to latex/pdflatex as many types as required via
- latexmk (version 4.25 or higher). Runs bibtex if required.
- Author: "$Author"
- Version: "$Version"
- Usage: ${ScriptName} [options] file.tex
- Options:
- -h|--help Show this help and exit.
- -l <line-num> Line number to jump to in the dvi/pdf file after compilation.
- -e|--errors Show error messages if compilation fails. Default.
- -e- Don't show error messages if compilation fails.
- -w|--warnings Show warning messages. Default.
- -w- Don't show warning messages.
- -b Show overfull/underfull warning messages.
- -b- Don't show overfull/underfull warning messages. Default.
- -v|--view View output (dvi/ps/pdf) after compilation. Default.
- The output format depends on \"%!TeX program\". See below.
- -v- Don't view output after compilation.
- -c Choose a different config file. Default master config file is
- \"~/.makelatexrc\". One can even create a local config file
- with the name makelatexrc in the same directory as the
- input texfile. The local config file is parsed later than
- the master config file. Further, unlike most scripts the
- config files in this script are parsed after command line
- arguments. This is to ensure that the user doesn't have to
- change the vimscript to change options.
- -O|--options Show the options that can written in the config file
- along with their default values and exit. Useful in creating
- the config files via the commands
- $ScriptName -O > ~/.makelatexrc ## Global config
- or
- $ScriptName -O > makelatexrc ## Local config
- -N Don't parse the config file.
- -N- Parse the config file. Default.
- -E Show the output format in the input tex file and exit.
- For multi-document project each sub-document files should have the following
- magic comment in the first 5 lines
- %!TeX root = master.tex
- where \"master.tex\" is the main tex file on which compilation ought to be
- done.
- To specify the typesetting command use the following magic comment in the
- first 5 lines of the input tex file
- %!TeX program = latex+dvipdf
- This is compatible with texworks. Typesetting commands can be
- pdflatex
- latex
- latex+dvips
- latex+dvipdf
- latex+dvips+ps2pdf
- xelatex
- If nothing is specified as typesetting command, then \"pdflatex\" is used.
- Custom typesetting commands are also supported.
- Note:
- *) Forward and reverse search however doesn't work with ps file, i.e. with
- latex+dvips.
- *) For multi-document the typesetting comment \"%!TeX program\" has to be
- specified only in the master tex file.
- "
- ErrMsg="${ScriptName}: Unspecified option. Aborting."
- ShowOptions(){
- echo "## Options that can be written in config file with their default values are:"
- echo "ErrorMsg=\""$ErrorMsg"\""
- echo "WarningMsg=\""$WarningMsg"\""
- echo "WarnBoxMsg=\""$WarnBoxMsg"\""
- echo "View=\""$View"\""
- echo "ltxoptions=\""$ltxoptions"\""
- }
- while test -n "$1"
- do
- case "$1" in
- -h|-help|--help)
- echo -n "$HelpText"
- exit 0 ;;
- *.tex) InputTeX="$1" ;;
- -l) LineNum="$2"
- shift ;;
- -e|--errors) ErrorMsg="true" ;;
- -e-) ErrorMsg="false" ;;
- -w|--warnings) WarningMsg="true" ;;
- -w-) WarningMsg="false" ;;
- -b) WarnBoxMsg="true" ;;
- -b-) WarnBoxMsg="false" ;;
- -v|--view) View="true" ;;
- -v-) View="false" ;;
- -c) RCFile="$2"
- shift
- ;;
- -O|--options) ShowOptions
- exit 0
- ;;
- -N) ParseRCFile="false"
- ;;
- -N-) ParseRCFile="true" ## Default
- ;;
- -E) Compile="false"
- # View="false"
- ;;
- *) echo "$ErrMsg"
- exit 1 ;;
- esac
- shift
- done
- ## Check for existence of input file
- FileExt=${InputTeX##*.}
- if [ ! -f "$InputTeX" ];then
- echo "Input file "$InputTeX" not found. Aborting."
- exit 1
- else
- DirName=$(dirname "$InputTeX")
- LocalRCFile="$DirName"/makelatexrc
- fi
- ## Parse RCFiles
- if [ "$ParseRCFile" == "true" ];then
- ## Parse master config file
- if [ -f "$RCFile" ];then
- source "$RCFile"
- fi
- ## Parse local config file
- if [ -f "$LocalRCFile" ];then
- source "$LocalRCFile"
- fi
- fi
- ## Requires latexmk with version 4.25 or higher.
- if ! which latexmk > /dev/null;then
- echo "latexmk not installed. Aborting."
- exit 1
- fi
- # Choose master file if specified
- RootFile=$(head -n 5 "$InputTeX" | grep -m 1 "root.*=" \
- | sed 's/.*root.*=//' | cut -d "%" -f1 | sed 's/^[ \t]*//;s/[ \t]*$//')
- if [ -n "$RootFile" ];then
- RootFileName=$(basename "$RootFile" .tex)
- else
- RootFileName=$(basename "$InputTeX" .tex)
- fi
- # Check if file exists or not
- if [ ! -f "$RootFileName".tex ];then
- echo "File "$RootFileName".tex doesn't exist. Aborting."
- exit 1
- fi
- ## Check for errors
- CheckErrors(){
- Errors=$(grep -e "^l\." -e "^\!" "$RootFileName".log \
- | sed 's/^l\./Line: /' | sed 's@\\@\\\\@g' \
- | sed 's@\/@\/\/@g' \
- | sed 's/&/&/g' \
- | sed '$!N; /^\(.*\)\n\1$/!P; D' \
- )
- }
- ## Check for warnings
- CheckWarnings(){
- if [ "$WarningMsg" == "true" ] ;then
- Warnings=$(awk '/^$/{p=0} /LaTeX Warning:/{p=1} p' "$RootFileName".log \
- | sed 's/^LaTeX //' | sed 's@\\@\\\\@g' \
- | sed 's/\&/\&/g' \
- | sed '$!N; /^\(.*\)\n\1$/!P; D' \
- )
- fi
- }
- ## Check for overfull and underfull boxes
- CheckBoxes(){
- if [ "$WarnBoxMsg" == "true" ] ;then
- BoxLines=$(grep -e "^Overfull" -e "^Underfull" "$RootFileName".log \
- | awk '{print $NF}' | sed '$!N; /^\(.*\)\n\1$/!P; D')
- OldIFS="$IFS"
- IFS=$'\n'
- WarnBoxes="$(
- for line in $BoxLines
- do
- warn=$(grep -m 1 "$line" "$RootFileName".log)
- echo "$warn"
- done
- )"
- IFS="$OldIFS"
- fi
- }
- ## Check for errors and warnings
- CheckErrorsWarnings(){
- if [ $Status -ne 0 ];then
- Title="Compilation errors and warnings"
- CheckErrors
- CheckWarnings
- CheckBoxes
- Msgs=""$Errors"
- "$Warnings"
- "$WarnBoxes""
- zenity --error --title="$Title" --text="$Msgs" &
- exit $Status
- else
- ## Check for warnings
- Title="Compilation warnings"
- CheckWarnings
- CheckBoxes
- if [ -n "$Warnings" ] || [ -n "$WarnBoxes" ] ;then
- Msgs=""$Warnings"
- "$WarnBoxes""
- zenity --error --title="$Title" --text="$Msgs" &
- fi
- fi
- }
- # Get compilation program
- TeXProgram=$(head -n 5 "$RootFileName".tex | grep -m 1 "program.*=" \
- | sed 's/.*program.*=//' | cut -d "%" -f1 \
- | sed 's/^[ \t]*//;s/[ \t]*$//')
- ## Call latexmk or other scripts
- if [ -z "$TeXProgram" ];then
- Ext="pdf"
- if [ "$Compile" == "true" ];then
- latexmk -g -pdf -pdflatex="$pdflatexcmd" "$RootFileName"
- Status=$(echo $?)
- CheckErrorsWarnings
- fi
- else
- OldIFS="$IFS"
- IFS='+'
- for Program in $TeXProgram
- do
- case "$Program" in
- pdflatex)
- Ext="pdf"
- if [ "$Compile" == "true" ];then
- latexmk -g -pdf -pdflatex="$pdflatexcmd" "$RootFileName"
- Status=$(echo $?)
- CheckErrorsWarnings
- fi
- ;;
- latex) Ext="dvi"
- if [ "$Compile" == "true" ];then
- latexmk -g -dvi -latex="$latexcmd" "$RootFileName"
- Status=$(echo $?)
- CheckErrorsWarnings
- fi
- ;;
- xelatex)
- Ext="pdf"
- if [ "$Compile" == "true" ];then
- latexmk -g -pdf -pdflatex="$xelatexcmd" "$RootFileName"
- Status=$(echo $?)
- CheckErrorsWarnings
- fi
- ;;
- dvips) Ext="ps"
- if [ "$Compile" == "true" ];then
- ${Program} "$RootFileName"
- fi
- ;;
- dvipdf) Ext="pdf"
- if [ "$Compile" == "true" ];then
- ${Program} "$RootFileName"
- fi
- ;;
- ps2pdf) Ext="pdf"
- if [ "$Compile" == "true" ];then
- ${Program} "$RootFileName".ps
- fi
- ;;
- *) ## For other user defined scripts
- if [ "$Compile" == "true" ];then
- ${Program} "$RootFileName"
- fi
- ;;
- esac
- shift
- done
- IFS="$OldIFS"
- fi
- if [ "$Compile" == "false" ];then
- # echo -n "$Ext"
- echo "$Ext"
- exit 0
- fi
- ## Load or reload output
- if [ "$View" == "true" ] && [ -n "$Ext" ] ;then
- if [ -n "$LineNum" ];then
- # okular --unique "$RootFileName"."$Ext"\#src:"$LineNum""$InputTeX" &
- ${DocumentViewer} "$RootFileName"."$Ext"\#src:"$LineNum""$InputTeX" &
- else
- # okular --unique "$RootFileName"."$Ext" &
- ${DocumentViewer} "$RootFileName"."$Ext" &
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement