Advertisement
anjishnu

makelatex2

Sep 15th, 2011
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.04 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## Todo:
  4. ## *) Preview for selected text? Check latex-suite.
  5. ## *) Option for live-preview? Problem: Okular blinks when reloading file.
  6. ##      Might distract the user.
  7. ## *) Embed the shellscripts makelatex2 and deljunk in tex_makelatex.vim
  8. ##      file. Possible?
  9. ## *) :cprev and :cnext to jump to errors. Also read :make and :errorformat
  10. ## *) Check for latexmk version requires 4.25 or higher
  11. ## *) [Done] Better error messages
  12. ## *) [Done] Show/don't show underfull/overfull \hbox warning messages.
  13. ## *) [Done] Update help
  14. ## *) [Done] Stop from running further compilation if there is error
  15. ##      in first run.
  16. ## *) [Done] Some rc file via which options can be changed easily. This rc file
  17. ##      has to be parsed after cli line arguments has been parsed so that
  18. ##      user can easily change options without changing the vim script from
  19. ##      which this script is called.
  20. ## *) [Done] Boolean to parse rcfile before and after cli arguments
  21. ## *) [Done] Show/don't-show warning messages.
  22. ## *) [Done] Which log file should one parse, when the project is
  23. ##      multi-document? Does sub-documents even have log files? How to
  24. ##      specify which sub-document has an error.
  25. ##      -> Only master.log has to be checked.
  26. ## *) [Done] Show/don't-show error messages
  27. ## *) [Done] Forward and reverse search for dvi file
  28. ##      Forward and reverse search is not possible with ps files
  29. ## *) [Done] Switch to call external scripts tex2html and update pdf+html file
  30. ## *) [Done] Switch to tell not to open pdf file.
  31.  
  32. ScriptName=$(basename $0)
  33. Author="Anjishnu Sarkar"
  34. Version="0.7"
  35. RCFile="$HOME"/.makelatexrc
  36. ParseRCFile="true"
  37. Compile="true"
  38. DocumentViewer="okular --unique"
  39.  
  40. ErrorMsg="true"
  41. WarningMsg="true"
  42. WarnBoxMsg="false"
  43. View="true"
  44.  
  45. ## Errors messages are verbose
  46. # ltxoptions="-synctex=1 -interaction=nonstopmode -shell-escape"
  47. ltxoptions="-synctex=1 -interaction=batchmode -shell-escape"
  48.  
  49. ## Errors messages are a compact
  50. # ltxoptions="-file-line-error -synctex=1 -interaction=batchmode -shell-escape"
  51.  
  52. pdflatexcmd="pdflatex "$ltxoptions""
  53. latexcmd="latex -src-specials "$ltxoptions""
  54. xelatexcmd="xelatex "$ltxoptions""
  55.  
  56. # latexmkcmd="latexmk -g -dvi -latex="$latexcmd" "$RootFileName""
  57. # pdflatexmkcmd="latexmk -g -pdf -pdflatex="$pdflatexcmd" "$RootFileName""
  58. # xelatexmkcmd="latexmk -g -pdf -pdflatex="$xelatexcmd" "$RootFileName""
  59.  
  60. HelpText="Wrapper script to latex/pdflatex as many types as required via
  61. latexmk (version 4.25 or higher). Runs bibtex if required.
  62. Author: "$Author"
  63. Version: "$Version"
  64.  
  65. Usage: ${ScriptName} [options] file.tex
  66.  
  67. Options:
  68. -h|--help       Show this help and exit.
  69. -l <line-num>   Line number to jump to in the dvi/pdf file after compilation.
  70. -e|--errors     Show error messages if compilation fails. Default.
  71. -e-             Don't show error messages if compilation fails.
  72. -w|--warnings   Show warning messages. Default.
  73. -w-             Don't show warning messages.
  74. -b              Show overfull/underfull warning messages.
  75. -b-             Don't show overfull/underfull warning messages. Default.
  76. -v|--view       View output (dvi/ps/pdf) after compilation. Default.
  77.                The output format depends on \"%!TeX program\". See below.
  78. -v-             Don't view output after compilation.
  79. -c              Choose a different config file. Default master config file is
  80.                \"~/.makelatexrc\". One can even create a local config file
  81.                with the name makelatexrc in the same directory as the
  82.                input texfile. The local config file is parsed later than
  83.                the master config file. Further, unlike most scripts the
  84.                config files in this script are parsed after command line
  85.                arguments. This is to ensure that the user doesn't have to
  86.                change the vimscript to change options.
  87. -O|--options    Show the options that can written in the config file
  88.                along with their default values and exit. Useful in creating
  89.                the config files via the commands
  90.                $ScriptName -O > ~/.makelatexrc     ## Global config
  91.                or
  92.                $ScriptName -O > makelatexrc        ## Local config
  93. -N              Don't parse the config file.
  94. -N-             Parse the config file. Default.
  95. -E              Show the output format in the input tex file and exit.
  96.  
  97. For multi-document project each sub-document files should have the following
  98. magic comment in the first 5 lines
  99.    %!TeX root = master.tex
  100. where \"master.tex\" is the main tex file on which compilation ought to be
  101. done.
  102.  
  103. To specify the typesetting command use the following magic comment in the
  104. first 5 lines of the input tex file
  105.    %!TeX program = latex+dvipdf
  106. This is compatible with texworks. Typesetting commands can be
  107.    pdflatex
  108.    latex
  109.    latex+dvips
  110.    latex+dvipdf
  111.    latex+dvips+ps2pdf
  112.    xelatex
  113. If nothing is specified as typesetting command, then \"pdflatex\" is used.
  114. Custom typesetting commands are also supported.
  115.  
  116. Note:
  117. *)  Forward and reverse search however doesn't work with ps file, i.e. with
  118.    latex+dvips.
  119. *)  For multi-document the typesetting comment \"%!TeX program\" has to be
  120.    specified only in the master tex file.
  121. "
  122. ErrMsg="${ScriptName}: Unspecified option. Aborting."
  123.  
  124. ShowOptions(){
  125.     echo "## Options that can be written in config file with their default values are:"
  126.     echo "ErrorMsg=\""$ErrorMsg"\""
  127.     echo "WarningMsg=\""$WarningMsg"\""
  128.     echo "WarnBoxMsg=\""$WarnBoxMsg"\""
  129.     echo "View=\""$View"\""
  130.     echo "ltxoptions=\""$ltxoptions"\""
  131. }
  132.  
  133. while test -n "$1"
  134. do
  135.     case "$1" in
  136.         -h|-help|--help)
  137.                     echo -n "$HelpText"
  138.                     exit 0 ;;
  139.  
  140.         *.tex)      InputTeX="$1" ;;
  141.  
  142.         -l)         LineNum="$2"
  143.                     shift ;;
  144.  
  145.         -e|--errors) ErrorMsg="true" ;;
  146.  
  147.         -e-)        ErrorMsg="false" ;;
  148.  
  149.         -w|--warnings) WarningMsg="true" ;;
  150.  
  151.         -w-)        WarningMsg="false" ;;
  152.  
  153.         -b)         WarnBoxMsg="true" ;;
  154.  
  155.         -b-)        WarnBoxMsg="false" ;;
  156.  
  157.         -v|--view)  View="true" ;;
  158.  
  159.         -v-)        View="false" ;;
  160.  
  161.         -c)         RCFile="$2"
  162.                     shift
  163.                     ;;
  164.  
  165.         -O|--options)  ShowOptions
  166.                     exit 0
  167.                     ;;
  168.  
  169.         -N)         ParseRCFile="false"
  170.                     ;;
  171.  
  172.         -N-)        ParseRCFile="true"  ## Default
  173.                     ;;
  174.  
  175.         -E)         Compile="false"
  176. #                     View="false"
  177.                     ;;
  178.  
  179.         *)          echo "$ErrMsg"
  180.                     exit 1 ;;
  181.     esac
  182.     shift
  183. done
  184.  
  185.  
  186. ## Check for existence of input file
  187. FileExt=${InputTeX##*.}
  188. if [ ! -f "$InputTeX" ];then
  189.     echo "Input file "$InputTeX" not found. Aborting."
  190.     exit 1
  191. else
  192.     DirName=$(dirname "$InputTeX")
  193.     LocalRCFile="$DirName"/makelatexrc
  194. fi
  195.  
  196. ## Parse RCFiles
  197. if [ "$ParseRCFile" == "true" ];then
  198.     ## Parse master config file
  199.     if [ -f "$RCFile" ];then
  200.         source "$RCFile"
  201.     fi
  202.     ## Parse local config file
  203.     if [ -f "$LocalRCFile" ];then
  204.         source "$LocalRCFile"
  205.     fi
  206. fi
  207.  
  208. ## Requires latexmk with version 4.25 or higher.
  209. if ! which latexmk > /dev/null;then
  210.     echo "latexmk not installed. Aborting."
  211.     exit 1
  212. fi
  213.  
  214. # Choose master file if specified
  215. RootFile=$(head -n 5 "$InputTeX" | grep -m 1 "root.*=" \
  216.     | sed 's/.*root.*=//' | cut -d "%" -f1 | sed 's/^[ \t]*//;s/[ \t]*$//')
  217. if [ -n "$RootFile" ];then
  218.     RootFileName=$(basename "$RootFile" .tex)
  219. else
  220.     RootFileName=$(basename "$InputTeX" .tex)
  221. fi
  222.  
  223. # Check if file exists or not
  224. if [ ! -f "$RootFileName".tex ];then
  225.     echo "File "$RootFileName".tex doesn't exist. Aborting."
  226.     exit 1
  227. fi
  228.  
  229. ## Check for errors
  230. CheckErrors(){
  231.     Errors=$(grep -e "^l\." -e "^\!" "$RootFileName".log \
  232.         | sed 's/^l\./Line: /' | sed 's@\\@\\\\@g' \
  233.         | sed 's@\/@\/\/@g' \
  234.         | sed 's/&/&amp;/g' \
  235.         | sed '$!N; /^\(.*\)\n\1$/!P; D' \
  236.     )
  237. }
  238.  
  239. ## Check for warnings
  240. CheckWarnings(){
  241.     if [ "$WarningMsg" == "true" ] ;then
  242.         Warnings=$(awk '/^$/{p=0} /LaTeX Warning:/{p=1} p' "$RootFileName".log \
  243.             | sed 's/^LaTeX //' |  sed 's@\\@\\\\@g' \
  244.             | sed 's/\&/\&amp;/g' \
  245.             | sed '$!N; /^\(.*\)\n\1$/!P; D' \
  246.         )
  247.     fi
  248. }
  249.  
  250. ## Check for overfull and underfull boxes
  251. CheckBoxes(){
  252.     if [ "$WarnBoxMsg" == "true" ] ;then
  253.         BoxLines=$(grep -e "^Overfull" -e "^Underfull" "$RootFileName".log \
  254.             | awk '{print $NF}' | sed '$!N; /^\(.*\)\n\1$/!P; D')
  255.  
  256.         OldIFS="$IFS"
  257.         IFS=$'\n'
  258.         WarnBoxes="$(
  259.            for line in $BoxLines
  260.            do
  261.                warn=$(grep -m 1 "$line" "$RootFileName".log)
  262.                echo "$warn"
  263.            done
  264.        )"
  265.         IFS="$OldIFS"
  266.     fi
  267. }
  268.  
  269. ## Check for errors and warnings
  270. CheckErrorsWarnings(){
  271.  
  272.     if [ $Status -ne 0 ];then
  273.  
  274.         Title="Compilation errors and warnings"
  275.  
  276.         CheckErrors
  277.         CheckWarnings
  278.         CheckBoxes
  279.  
  280.         Msgs=""$Errors"
  281. "$Warnings"
  282. "$WarnBoxes""
  283.         zenity --error --title="$Title" --text="$Msgs" &
  284.         exit $Status
  285.  
  286.     else
  287.         ## Check for warnings
  288.         Title="Compilation warnings"
  289.  
  290.         CheckWarnings
  291.         CheckBoxes
  292.  
  293.         if [ -n "$Warnings" ] || [ -n "$WarnBoxes" ] ;then
  294.             Msgs=""$Warnings"
  295. "$WarnBoxes""
  296.             zenity --error --title="$Title" --text="$Msgs" &
  297.         fi
  298.     fi
  299. }
  300.  
  301. # Get compilation program
  302. TeXProgram=$(head -n 5 "$RootFileName".tex | grep -m 1 "program.*=" \
  303.     | sed 's/.*program.*=//' | cut -d "%" -f1 \
  304.     | sed 's/^[ \t]*//;s/[ \t]*$//')
  305.  
  306. ## Call latexmk or other scripts
  307. if [ -z "$TeXProgram" ];then
  308.     Ext="pdf"
  309.     if [ "$Compile" == "true" ];then
  310.         latexmk -g -pdf -pdflatex="$pdflatexcmd" "$RootFileName"
  311.         Status=$(echo $?)
  312.         CheckErrorsWarnings
  313.     fi
  314. else
  315.     OldIFS="$IFS"
  316.     IFS='+'
  317.  
  318.     for Program in $TeXProgram
  319.     do
  320.     case "$Program" in
  321.  
  322.         pdflatex)
  323.                 Ext="pdf"
  324.                 if [ "$Compile" == "true" ];then
  325.                     latexmk -g -pdf -pdflatex="$pdflatexcmd" "$RootFileName"
  326.                     Status=$(echo $?)
  327.                     CheckErrorsWarnings
  328.                 fi
  329.                 ;;
  330.  
  331.         latex)  Ext="dvi"
  332.                 if [ "$Compile" == "true" ];then
  333.                     latexmk -g -dvi -latex="$latexcmd" "$RootFileName"
  334.                     Status=$(echo $?)
  335.                     CheckErrorsWarnings
  336.                 fi
  337.                 ;;
  338.  
  339.         xelatex)
  340.                 Ext="pdf"
  341.                 if [ "$Compile" == "true" ];then
  342.                     latexmk -g -pdf -pdflatex="$xelatexcmd" "$RootFileName"
  343.                     Status=$(echo $?)
  344.                     CheckErrorsWarnings
  345.                 fi
  346.                 ;;
  347.  
  348.         dvips)  Ext="ps"
  349.                 if [ "$Compile" == "true" ];then
  350.                     ${Program} "$RootFileName"
  351.                 fi
  352.                 ;;
  353.  
  354.         dvipdf) Ext="pdf"
  355.                 if [ "$Compile" == "true" ];then
  356.                     ${Program} "$RootFileName"
  357.                 fi
  358.                 ;;
  359.  
  360.         ps2pdf) Ext="pdf"
  361.                 if [ "$Compile" == "true" ];then
  362.                     ${Program} "$RootFileName".ps
  363.                 fi
  364.                 ;;
  365.  
  366.         *)      ## For other user defined scripts
  367.                 if [ "$Compile" == "true" ];then
  368.                     ${Program} "$RootFileName"
  369.                 fi
  370.                 ;;
  371.     esac
  372.     shift
  373.     done
  374.     IFS="$OldIFS"
  375. fi
  376.  
  377. if [ "$Compile" == "false" ];then
  378. #     echo -n "$Ext"
  379.     echo "$Ext"
  380.     exit 0
  381. fi
  382.  
  383. ## Load or reload output
  384. if [ "$View" == "true" ] && [ -n "$Ext" ] ;then
  385.     if [ -n "$LineNum" ];then
  386. #         okular --unique "$RootFileName"."$Ext"\#src:"$LineNum""$InputTeX" &
  387.         ${DocumentViewer} "$RootFileName"."$Ext"\#src:"$LineNum""$InputTeX" &
  388.     else
  389. #         okular --unique "$RootFileName"."$Ext" &
  390.         ${DocumentViewer} "$RootFileName"."$Ext" &
  391.     fi
  392. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement