Vim Tips Wiki
Register
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.

Linux

First, 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)
			exec 'silent ! evince --page-label=' . page . ' ' . substitute(f, "aux$", "pdf", "") . ' > /dev/null 2>&1 &'
			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 calls evince on the pdf and page. 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.

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 evince 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 evince 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 in evince.

Windows

Your help is needed!

Mac

Copy stuff from Backward search for LaTeX documents on Mac OS X

Advertisement