[Tex/LaTex] How to do forward search to PDF file opened with Okular from include files when editing using VIM/GVIM

forward-inverse-searchokularpdfvim

I'm using vim to edit my tex files and I use latex suite as a plugin to compile my tex sources and view them in using Okular pdf viewer.

Setting the following in Okular settings makes inverse search work perfectly

gvim --servername GVIM --remote +%l %f

But having looked on the web and tried different ways to make forward search work within vim with latex suite, forward search is still working only partly for me in vim. I'm only able to do forward search with \ls in the main tex file. If I do \ls inside an include or input file then Okular would complain that it can't find the particular pdf for that include/input file which is true because only the main tex file has a pdf compiled.

I also tried a custom function which I found from an answer to a similar question here on TeXExchange: https://tex.stackexchange.com/a/2947/2031 but the path passed to Okular with this command is just a concatenation of the paths to both the main pdf and tex files.

Below are my current settings in my .vimrc related to this question:

let g:Tex_CompileRule_pdf = 'pdflatex -synctex=1 -src-specials -interaction=nonstopmode $*'
let g:Tex_ViewRule_pdf = 'okular --unique'
function! SyncTexForward()
     let execstr = "silent !okular --unique %:p:r.pdf\#src:".line(".")."%:p &"
     exec execstr
endfunction
nmap <Leader>f :call SyncTexForward()<CR>

Best Answer

I had exact the same problem. Modifying the function to pass the correct paths did the trick, at least for me. Here is the code:

function! SyncTexForward()
let s:syncfile = fnamemodify(fnameescape(Tex_GetMainFileName()), ":r").".pdf"
let execstr = "silent !okular --unique ".s:syncfile."\\#src:".line(".").expand("%\:p").' &'
exec execstr
endfunction
nnoremap <Leader>f :call SyncTexForward()<CR>
Related Question