Vim Tips Wiki
Advertisement

When you are editing a large LaTeX project, it is easy to jump around in Vim using tags, marks, file tree plugins, etc. One problem is after you jump to a tag in Vim the pdf open in other window does not change page. This tip will allow you to add a command to vim to change the page of the pdf to match the current location in Vim.

Finding pdf filename and line for current cursor

First, copy one of the OpenPDF functions below specific to your operating system. Then drop the following functions in your tex.vim ftplugin file or your vimrc file

"Load PDF to the page containing label
function! LoadEvinceByLabel(l)
	for f in split(glob("*.aux"))
		let label = system('grep "^.newlabel{' . a:l . '" ' . f)
		let page = matchstr(label, '.\{}{\zs.*\ze}}')
		if ! empty(page)
			call OpenPDF(substitute(f, "aux$", "pdf", ""), page)
			return
		endif
	endfor
endfunction

"Load PDF to the page containing the nearest previous label to the cursor
function! EvinceNearestLabel()
	let line = search("\\label{", "bnW")
	if line > 0
		let m = matchstr(getline(line), '\\label{\zs[^}]*\ze}')
		if empty(m)
			echomsg "No label between here and start of file"
		else
			call LoadEvinceByLabel(m)
		endif
	endif
endfunction

The first function looks through the aux files created by compiling the latex file for the label passed in as a parameter. If it finds the label, it loads the page number and then loads the pdf to the given page. The second function searches for the nearest label from the current position and calls LoadEvinceByLabel.

To use them, add something like

map <buffer> <LocalLeader>e :call EvinceNearestLabel()<CR>

Now \e will load the pdf viewer to page containing the nearest label to the current cursor position. Note this works great on multi-file projects since the aux file is searched to find the pdf name. So you can jump around in Vim using all the wonderful tools and plugins (tags, marks, nerdtree, project, etc.) and then tell the pdf viewer to jump to the given page.

If you have set up tags, you can also add

com! -nargs=1 -complete=tag Pdf call LoadEvinceByLabel("<args>")

for a command :Pdf that you can use tab completion on labels to jump to a given location.

Linux

On linux, use the following OpenPDF function

function! OpenPDF(file,page)
	exec 'silent ! evince --page-label=' . a:page . ' ' . a:file . ' > /dev/null 2>&1 &'
endfunction

Evince is smart enough so that if the pdf is already open, it just changes the page of the existing window and if the file is not open yet, it creates the new window and jumps to the given page.

Windows

Acrobat

This is currently untested since I don't have access to a windows machine. If you have access to a windows machine and test it and it works, delete this line. Commands can be sent to acrobat using [1]

function! OpenPDF(file, page)
    exec '!start cmcdde acroviewr10 control [DocOpen("' . a:file . '")][DocGoTo("' . a:file . ',' . a:page . '")]'
endfunction

If you use the vim-latexsuite plugin, acrobat needs to be closed before compiling which can be done with the following commands.

let s:AcroDDE = "cmcdde acroviewr10 control "
let s:ClosePdf = s:AcroDDE.'[DocOpen("%:p:r.pdf")][DocClose("%:p:r.pdf")] '
let g:Tex_CompileRule_pdf = s:ClosePdf.' & texify -bp --src $*'
let g:Tex_ViewRule_pdf =
        \ 'C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe'

Mac

Your help is needed. Some AppleScript in OpenPDF should do the job.

Advertisement