summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/hybrid-latex/shell/pylatex.sh
blob: 434800205410a17f22a751783201cc25020490fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash

file="<none>"
silent="no"
keep="no"
skiplatex="no"
Python="python"
sty=""

# -----------------------------------------------------------------------------------------
# Parse the command-line options

OPTIND=1

while getopts 'i:I:P:skxh' option
do
   case "$option" in
   "i")  file="$OPTARG"      ;;
   "I")  sty="-I$OPTARG"     ;;
   "P")  Python="$OPTARG"    ;;
   "s")  silent="yes"        ;;
   "k")  keep="yes"          ;;
   "x")  skiplatex="yes"     ;;
   "h")  echo "usage : pylatex.sh -i file [-P<path to python>] [-I<path to pymacros.sty>] [-s] [-k] [-x] [-h]"
         echo "options :  -i file : source file (without .tex extension)"
         echo "           -I file : full path to pymacros.sty file"
         echo "           -P path : where to find the Python binary"
         echo "           -s : silent, don't open the pdf file"
         echo "           -k : keep all temporary files"
         echo "           -x : don't call latex"
         echo "           -h : this help message"
         echo "example : pylatex.sh -i file -P/usr/bin/python"
         exit                ;;
   ?)    echo "pylatex.sh : Unknown option specified."
         exit                ;;
   esac
done

if [[ ! -e $file.tex ]]; then
   echo "> file ""$file.tex"" not found, exit"
   exit 1;
fi

if [[ $file = "<none>" ]]; then
   echo "> no source file given, use pylatex.sh -i file"
   exit 1;
fi;

file=$(basename -s ".tex" "$file")
num=$(egrep -c -e'^\s*\\Input\{' "$file".tex)
name=$file

if ! [[ $num = 0 ]]; then
   merge-tex.py -i $file.tex -o .merged.tex
   name=".merged"
fi

touch $file.pytxt

pypreproc.py -i $file -m $name      || exit 1

$Python $file"_.py" > $file.pytxt   || exit 3

pypostproc.py -i $file $sty         || exit 5

if [[ $skiplatex = "no" ]]; then
   pdflatex -halt-on-error -interaction=batchmode -synctex=1 $file || exit 7
   echo " " # for some silly reason pdfsync forgets a trailing \n
else
   silent="yes"
fi

if [[ $silent = "no" ]]; then
   open $file.pdf       # macOS
   # xdg-open $file.pdf   # Linux
   # evince $file.pdf     # Linux
fi

if [[ $keep = "no" ]]; then
   rm -rf comment.cut
   rm -rf .merged.tex .tmp.txt
   rm -rf $file.log $file.out $file.py $file"_.py" $file.pyidx $file.pytxt
fi