updated on Sat Jan 21 20:03:50 UTC 2012
[aur-mirror.git] / evince-gtk-synctex / synctex-emacs.sh
blob7e935c7f2d704193937cc2837c4eccf9777629c7
1 #!/bin/bash
2 # Compile latex document and open viewer at specified location via synctex
3 # latexmk (included in texlive-bin) and wmctrl are used by the script. wmctrl
4 # is used to give evince focus after updating the document, you don't have to
5 # install it, if you don't want this functionality.
7 # latexmk is used to compile document - it should automatically
8 # compile your document multiple times and run bibtex as required.
9 # You have to tell latexmk to use the --synctex=1 option when running
10 # pdflatex, to do so, create a file ~/.latexmkrc containing:
11 # $pdflatex = 'pdflatex --interaction nonstopmode --synctex=1 %O %S';
12 # There must be a newline after this line. I havent found any way to
13 # give this info to latexmk directly from the commandline, but that
14 # would be alot easier.
16 # To get AUCTEX to use this scipt, add the following lines to your .emacs file:
17 # You can then use C-c C-c Synctex RET to compile and view in evince
18 # (eval-after-load "tex"
19 # '(add-to-list 'TeX-command-list
20 # '("Synctex" "/etc/scripts/synctex-emacs.sh %t %b %n" TeX-run-TeX nil t) t))
22 MAINFILE=$1
23 FILE=$2
24 LINE=$3
25 COLUMN=-1
26 PDFFILE="$(echo "$MAINFILE" | sed 's/\.tex/\.pdf/g')"
28 ########################################
29 ## Compile document
30 ########################################
31 latexmk -pdf "$MAINFILE"
32 if [ $? -eq 0 ]; then
33 ####################################
34 ## succesful latex compilation -> view with synctex
35 ####################################
36 # Call synctex
37 SYNCTEX_RESULT=$( synctex view -i "$LINE:$COLUMN:$FILE" -o "${PDFFILE}" -x "echo ::%{page}:%{h}:%{v}:%{width}:%{height}::" )
39 # Use bash built-in regex to parse data
40 [[ $SYNCTEX_RESULT =~ ::([^:]+):([^:]+):([^:]+):([^:]+):([^:]+):: ]]
42 # Did synctex succed
43 if [ "${BASH_REMATCH[0]}" ]; then
44 # Get results from regex matching
45 page=$( expr ${BASH_REMATCH[1]} + 1 )
46 x=${BASH_REMATCH[2]}
47 y=${BASH_REMATCH[3]}
48 width=${BASH_REMATCH[4]}
49 height=1
50 # height=$( expr $( echo ${BASH_REMATCH[5]} | grep -o ^[0-9] ) \* 2 )
51 nohup evince --use-absolute-page --page-label "$page" --highlight-rect "${x}:${y}:${width}:${height}" "${PDFFILE}" >/dev/null &
52 else
53 echo "Synctex did not return usable results, viewing without synctex"
54 nohup evince "${PDFFILE}" >/dev/null &
56 wmctrl -xa evince
57 exit 0
58 else
59 exit 1