Posts Tagged ‘LaTex’

Automate pdf preview when using vi to edit latex on OSX.

Friday, March 26th, 2010

You can add the following to your .vimrc in order to have vi automaticly update and show the pdf version of the LaTex .tex file you are currently editing. The CursorHold part triggers updating after a set amount of inactivity, while the BufWritePost part triggers an update before each save (when you type :w ). This is based on a post doing the same for ubunu.

Note (deprecated now due to the edit below): the ” !open -a Terminal” is just a work-around since i didn’t find a way to make Preview.app update in the background (aka not steal the focus).

EDIT 2010-03-29: Since Guillermo pointed out how to do the update properly i changed my .vimrc entry. But i left the old line in as a comment.

""""""""""""""""""""""""""""""""""""""""""""""""""
" for LaTeX files
""""""""""""""""""""""""""""""""""""""""""""""""""
au BufEnter *.tex set autowrite "save before making PDF"
au BufEnter *.tex set updatetime=500 "wait ms"
au BufEnter *.tex set makeprg=pdflatex\ -halt-on-error\ %\ >&\ /dev/null

"make the file after specified time of not moving and after every save"
au CursorHold *.tex call UpdateFile()
au BufWritePost *.tex call UpdateFile()

function! UpdateFile()
  silent make
    !open -g %<.pdf  "update view in preview"
    " following comment line was needed before i learned about the -g option "
    " !open -a Terminal   switch back to vi assuming you run it in Termial"
    redraw! "remove artifacts during save"
    endfunction

au BufRead *.tex silent !open -g -a preview %<.pdf