Thursday, October 18, 2012

One key compile latex documents

Here I want press one key to compile latex document, and launch viewer.

The first question you would ask: Isn't auctex's binding C-c C-c doing it?
-- Not quite. The biggest problem is that, preview-latex breaks the generated pdf file. Thus I cannot have preview and the generated pdf at the same time. Also, finding configuration methods is (sometimes) harder than do it yourself.

What the code does:
* One key compile latex document
* Put temporary files into temporary paths
* Open reader (if not already open)
* On mistake, open log file in new frame
* On warning, send desktop-notification

Now implications



Shell command side:
ORIGINAL_PATH=$(echo $1 | sed 's/\(.*\)\/.*/\1/')
FN=$(echo $1|sed 's/.*\/\(.*\)/\1/'|sed 's/\(.*\)\..*/\1/')
WORKING_PATH=/tmp/cctex_output$ORIGINAL_PATH
LOG_FN=/tmp/cctex_output/cctex.log

mkdir -p /tmp/cctex_output
mkdir -p $WORKING_PATH

echo "Working at " WORKING_PATH >$LOG_FN

echo "Now compile file..." >>$LOG_FN
pdflatex -halt-on-error -synctex=1 -src-specials -aux-directory=$WORKING_PATH -output-directory=$WORKING_PATH $1
if [ $? -ne 0 ]
then
    echo "There are some mistakes..."  >>$LOG_FN
    emacsclient -c +10000 $WORKING_PATH/$FN.log
    exit 1
fi

echo "Examine if there are warnings..."  >>$LOG_FN
grep 'LaTeX Warning: ' $WORKING_PATH/$FN.log
if [ $? -eq 0 ]
then
    echo "Send desktop notification on warning..."  >>$LOG_FN
    MSG=$(grep 'LaTeX Warning: ' multi.log)
#    notify-send "$MSG"
    notify-send "$(echo $MSG | sed 's/LaTeX Warning: / ** /g')"
fi


echo "Check if reader is already open on file..."  >>$LOG_FN
wmctrl -l | grep $FN | grep Okular
if [ $? -ne 0 ]
then
    echo "Now open reader..."  >>$LOG_FN
    nohup okular $WORKING_PATH/$FN.pdf >/dev/null 2>/dev/null
fi

echo "Now exit successfully..."  >>$LOG_FN
exit 0

Emacs side:
(add-hook 'LaTeX-mode-hook '(lambda ()
      (local-set-key (quote [f5]) '(lambda () (interactive)
 (save-buffer)
        (call-process "/home/wangyi/Dropbox/local/bin/cctex.sh" nil  0 nil buffer-file-name)
      ))
      (local-set-key [(shift f5)] '(lambda () (interactive)
 (find-file (concat "/tmp/cctex_output" (substring buffer-file-name 0 -4) ".log")  )
      ))
))

That's it.

No comments:

Post a Comment