Vim script to compile TeX source and launch PDF only if no errors

Posted by Jeet on Stack Overflow See other posts from Stack Overflow or by Jeet
Published on 2010-04-20T23:50:58Z Indexed on 2010/04/21 0:23 UTC
Read the original article Hit count: 623

Filed under:
|
|
|

Hi,

I am switching to using Vim for for my LaTeX editing environment. I would like to be able to tex the source file from within Vim, and launch an external viewing if the compile was successful.

I know about the Vim-Latex suite, but, if possible, would prefer to avoid using it: it is pretty heavy-weight, hijacks a lot of my keys, and clutters up my vimruntime with a lot of files.

Here is what I have now:

if exists('b:tex_build_mapped')
    finish
endif
" use maparg or mapcheck to see if key is free
command! -buffer -nargs=* BuildTex call BuildTex(0, <f-args>)
command! -buffer -nargs=* BuildAndViewTex call BuildTex(1, <f-args>)
noremap <buffer> <silent> <F9> <Esc>:call BuildTex(0)<CR>
noremap <buffer> <silent> <S-F9> <Esc>:call BuildTex(1)<CR>
let b:tex_build_mapped = 1

if exists('g:tex_build_loaded')
    finish
endif
let g:tex_build_loaded = 1

function! BuildTex(view_results, ...)
    write
    if filereadable("Makefile")
        " If Makefile is available in current working directory, run 'make' with arguments
        echo "(using Makefile)"
        let l:cmd = "!make ".join(a:000, ' ')
        echo l:cmd
        execute l:cmd
        if a:view_results && v:shell_error == 0
            call ViewTexResults()
        endif
    else
        let b:tex_flavor = 'pdflatex'
        compiler tex
        make %
        if a:view_results && v:shell_error == 0
            call ViewTexResults()
        endif
    endif
endfunction

function! ViewTexResults(...)
    if a:0 == 0
        let l:target = expand("%:p:r") . ".pdf"
    else
        let l:target = a:1
    endif
    if has('mac')
        execute "! open -a Preview ".l:target
    endif
endfunction

The problem is that v:shell_error is not set, even if there are compile errors. Any suggestions or insight on how to detect whether a compile was successful or not would be greatly appreciated! Thanks!

© Stack Overflow or respective owner

Related posts about vim

Related posts about latex