Vim Tips Wiki
(Adding categories)
No edit summary
Line 1: Line 1:
 
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.
 
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 ==
== Linux ==
 
   
First, drop the following functions in your tex.vim ftplugin file or your vimrc file
+
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
   
 
<pre>
 
<pre>
Line 12: Line 13:
 
let page = matchstr(label, '.\{}{\zs.*\ze}}')
 
let page = matchstr(label, '.\{}{\zs.*\ze}}')
 
if ! empty(page)
 
if ! empty(page)
exec 'silent ! evince --page-label=' . page . ' ' . substitute(f, "aux$", "pdf", "") . ' > /dev/null 2>&1 &'
+
call OpenPDF(substitute(f, "aux$", "pdf", ""), page)
 
return
 
return
 
endif
 
endif
Line 33: Line 34:
   
 
The first function looks through the aux files created by compiling the latex file for the label
 
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
+
passed in as a parameter. If it finds the label, it loads the page number and then loads the pdf to the given page.
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.
 
The second function searches for the nearest label from the current position and calls LoadEvinceByLabel.
   
Line 45: Line 43:
 
</pre>
 
</pre>
   
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.
+
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
 
If you have set up tags, you can also add
Line 53: Line 51:
 
</pre>
 
</pre>
   
for a command :Pdf that you can use tab completion on labels to jump to a given location in evince.
+
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
  +
  +
<pre>
  +
function! OpenPDF(file,page)
  +
exec 'silent ! evince --page-label=' . a:page . ' ' . a:file . ' > /dev/null 2>&1 &'
  +
endfunction
  +
</pre>
  +
 
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 ==
 
== Windows ==
   
  +
==== Acrobat ====
Your help is needed!
 
  +
  +
Commands can be sent to acrobat using [http://www.istri.fr/zip/CMCDDE.zip]
  +
  +
<pre>
  +
function! OpenPDF(file, page)
  +
exec '!start cmcdde acroviewr10 control [DocOpen("' . a:file . '")][DocGoTo("' . a:file . ',' . a:page . '")]'
  +
endfunction
  +
</pre>
  +
  +
If you use the vim-latexsuite plugin, acrobat needs to be closed before compiling which can be done with the following
  +
commands.
  +
  +
<pre>
  +
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'
  +
</pre>
   
 
== Mac ==
 
== Mac ==
   
  +
Your help is needed. Some AppleScript in OpenPDF should do the job.
Copy stuff from [[Backward search for LaTeX documents on Mac OS X]]
 
  +
[[Category:LaTeX]]
  +
[[Category:LaTeX]]
  +
[[Category:LaTeX]]
 
[[Category:LaTeX]]
 
[[Category:LaTeX]]

Revision as of 03:26, 28 November 2011

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

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.