vimlatexmk

Mappings for compiling and viewing LaTeX documents in Vim


I used to use LaTeX-Box plugin to compile and view LaTeX documents with these mappings.

But I don't use it anymore because \begin and \end highlighting (and to a lower degree parentheses matching), which can't be turned off, make Vim a lot slower in big .tex files.

I know how to create an alternative mappings for compiling .tex documents:

autocmd FileType tex nnoremap <buffer> <F2> :!latexmk -xelatex %<cr>
autocmd FileType tex inoremap <buffer> <F2> <Esc>:!latexmk -xelatex %<cr>a

But this will create output files in directory I am currently in, not in the directory where .tex file is (for example, if I am in ~/Desktop and I open in Vim a file which is in ~/Documents, if I press F2, .pdf and all other files are created in ~/Desktop instead of in ~/Documents).

I don't know how to create mappings for viewing the compiled .pdf document.

So, I am asking you to help me create mappings that will compile .tex file, and view created .pdf file.


Solution

  • You can easily modify your mappings to generate files in the same directory as your source file with :help filename-modifiers:

    augroup LaTeX
        autocmd!
        autocmd FileType tex nnoremap <buffer> <F2> :!cd %:p:h && latexmk -xelatex %<cr>
        autocmd FileType tex inoremap <buffer> <F2> <Esc>:!cd %:p:h && latexmk -xelatex %<cr>a
    augroup END
    

    To open the generated file with an imaginary pdfviewer program (command shortened for legibility):

    !cd %:p:h && latexmk -xelatex % && pdfviewer %:p:r.pdf