fresh
fresh

Reputation: 411

Vim - LaTeX. How to conditionally open the PDF viewer?

I'm currently trying to write my own setup for autocompiling and previewing LaTeX code using Vim and Zathura. This is the code I have currently have in my tex.vim file:

:map <F6> :w <bar> ! pdflatex -synctex=1 -interaction=nonstopmode %<CR><CR>
:map <F7> :! zathura $(echo % \| sed 's/tex$/pdf/') & disown<CR><CR>

This works fine as long as when I compile the code (from Vim) I am in the directory where my tex files are. However, I would like this to work regardless of my position (since I frequently open files from Vim using the :e command). How do I fix this?

Also, I would like Zathura to open only if the PDF preview is not already open. Does anyone have any suggestions as to how to do such a thing?

Upvotes: 0

Views: 1554

Answers (2)

Johannes Titz
Johannes Titz

Reputation: 1001

An alternative would be to simply use vimtex (https://github.com/lervag/vimtex)

Mapping: <localleader>lv

Upvotes: 0

Hauleth
Hauleth

Reputation: 23556

You can use :h filename-modifiers instead of your sed hack, so this should do:

nnoremap :<C-u>w <bar> ! pdflatex -synctex=1 -interaction=nonstopmode %:p<CR>
nnoremap :<C-u>! zathura %:p:r.pdf<CR>

I have also changed these mappings to use :nnoremap which you should prefer anyway.


If you want to expand your Vim-fu even further you can check :h compiler and :h compilet-tex which will handle setting proper 'makeprg' for you which will allow you to use :make to build your TeX files.

Upvotes: 1

Related Questions