Vim Tips Wiki
(14 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipProposed
When using Adobe Reader (on Windows) to view pdf files it will lock the current file. This makes it necessary to close the pdf file before recompiling a tex file into pdf. An easy way to do so is to use [http://www.istri.fr/zip/CMCDDE.zip cmcdde].
 
  +
|id=0
  +
|previous=0
  +
|next=0
  +
|created=November 28, 2011
  +
|complexity=basic
  +
|author=Wuzzeb
  +
|version=7.0
  +
|subpage=/201111
  +
|category1=LaTeX
  +
|category2=
  +
}}
  +
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 that after you jump to a tag in Vim, the pdf open in another application does not change page. This tip adds a command to Vim to change the page of the pdf to match the current location in Vim, and assumes you are using the [http://sourceforge.net/projects/vim-latex/ Latex-Suite].
   
  +
==Finding pdf filename and line for current cursor==
To close the pdf output file of the currently edited tex file one would thus use:
 
  +
Add the following code to your <tt>tex.vim</tt> file (<tt>~/.vim/ftplugin/tex.vim</tt> on Unix, or <tt>$HOME/vimfiles/ftplugin/tex.vim</tt> on Windows). Also add one of the <tt>OpenPDF</tt> functions listed below, depending on your operating system.
!start cmcdde acroviewr10 control [DocOpen("%:p:r.pdf")][DocClose("%:p:r.pdf")]
 
  +
<pre>
  +
"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
   
Here DocOpen registers the pdf with the dde server. DocClose then closes the pdf.
+
"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
  +
</pre>
   
  +
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.
NOTE: The name Adobe uses for the DDE server changed from version 9 to 10. For Adobe Reader version prior to 10 use '''acroview''' instead of '''acroviewr10'''. Adobe also announced that future versions will continue to have the version number in the server name.
 
   
  +
The second function searches for the nearest label from the current position and calls <tt>LoadEvinceByLabel</tt>.
==Automatically close pdf before compiling==
 
   
  +
To use them, add something like:
I am using the vim-latex suite. To automatically close Adobe before recompiling using the suite I put the following in my tex.vim file:
 
  +
<pre>
  +
nnoremap <buffer> <LocalLeader>e :call EvinceNearestLabel()<CR>
  +
</pre>
   
  +
Now <tt>\e</tt> will load the pdf viewer to the page containing the nearest label to the current cursor position (assuming the default backslash for the LocalLeader key). This works well on multi-file projects since the aux file is searched to find the pdf name. So you can jump around in Vim using its wonderful tools, then tell the pdf viewer to jump to the same page.
if has("win32")
 
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'
 
endif
 
   
  +
If you have set up tags, you can also add:
NOTE: This configuration assumes that the folder containing cmcdde is in your path. Alternatively just specify the full path name to cmcdde in s:AcroDDE.
 
  +
<pre>
  +
command! -nargs=1 -complete=tag Pdf call LoadEvinceByLabel("<args>")
  +
</pre>
   
  +
to create command <tt>:Pdf</tt> so you can use Tab completion on labels to jump to a given location.
<!--
 
==References==
 
*{{help|toc}}
 
-->
 
   
==Comments==
+
==Linux==
  +
On Linux, use the following <tt>OpenPDF</tt> function:
There are also a few other DDE commands available with the Adobe Reader. Most interestingly, DocGoTo and DocGoToNameDest. The first can be used to jump to a particular page in the pdf while the second jumps to a named destination. I haven't figured out yet how to do so, but maybe one could use the second one to implement some sort of forward search with the Adobe Reader. (The idea is to call DocGoToNameDest with a fixed bookmark, say "cursorposition", and use latex to create that bookmark when compiling. Not sure if there exists some latex packet which could be used for that...)
 
  +
<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 to 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.
Here is a little trick to open Adobe Reader on the last page. Just use DocGoTo with a large page number (which doesn't need to exist):
 
  +
!start cmcdde acroviewr10 control [DocOpen("%:p:r.pdf")][DocGoTo("%:p:r.pdf,10000")]
 
  +
==Windows==
[[Category:LaTeX]]
 
  +
====Acrobat====
  +
Commands can be sent to Acrobat using [http://www.istri.fr/zip/CMCDDE.zip cmcdde], and the following version of <tt>OpenPDF</tt> can be used:
  +
<pre>
  +
function! OpenPDF(file, page)
  +
execute '!start cmcdde acroviewr10 control [DocOpen("' . a:file . '")][DocGoTo("' . a:file . ',' . a:page . '")]'
  +
endfunction
  +
</pre>
  +
  +
Acrobat needs to be closed before compiling which can be done with the following commands (the <tt>has("win32")</tt> test allows this code to be in a file that may be used on systems other than Windows, where different commands would be available):
  +
<pre>
 
if has("win32")
 
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'
  +
endif
  +
</pre>
  +
 
The above assumes that <tt>cmcdde.exe</tt> is in a directory in your PATH. Alternatively, specify the full path name to <tt>cmcdde</tt> in <tt>s:AcroDDE</tt>.
  +
  +
===Notes for Windows===
  +
:''May merge or remove this material later; keeping as miscellaneous notes for now.''
 
When using Adobe Reader on Windows to view pdf files, the current file is locked. That makes it necessary to close the pdf file before recompiling a tex file into pdf.
  +
 
To close the pdf output file of the currently edited tex file, use:
  +
<pre>
 
:!start cmcdde acroviewr10 control [DocOpen("%:p:r.pdf")][DocClose("%:p:r.pdf")]
  +
</pre>
  +
  +
<tt>DocOpen</tt> registers the pdf with the dde server. <tt>DocClose</tt> then closes the pdf.
  +
 
For Adobe Reader versions prior to 10, use <tt>acroview</tt> instead of <tt>acroviewr10</tt>. Adobe has announced that future versions will also have the version number in the server name.
  +
 
Some other DDE commands are available with Adobe Reader, for example, <tt>DocGoTo</tt> and <tt>DocGoToNameDest</tt>. The first can be used to jump to a particular page in the pdf while the second jumps to a named destination. Perhaps the second one could be used to do a forward search with the Adobe Reader. (The idea is to call <tt>DocGoToNameDest</tt> with a fixed bookmark, say "cursorposition", and use latex to create that bookmark when compiling. Not sure if there exists some latex packet which could be used for that.)
  +
 
Here is a trick to open Adobe Reader on the last page: use <tt>DocGoTo</tt> with a large page number (which doesn't exist):
  +
<pre>
 
:!start cmcdde acroviewr10 control [DocOpen("%:p:r.pdf")][DocGoTo("%:p:r.pdf,10000")]
  +
</pre>
  +
  +
==Mac==
  +
''Need some ideas. Some AppleScript in OpenPDF should do the job.''
  +
  +
==Comments==
  +
Couldnt get this to work

Revision as of 08:56, 30 September 2012

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created November 28, 2011 · complexity basic · author Wuzzeb · version 7.0

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 that after you jump to a tag in Vim, the pdf open in another application does not change page. This tip adds a command to Vim to change the page of the pdf to match the current location in Vim, and assumes you are using the Latex-Suite.

Finding pdf filename and line for current cursor

Add the following code to your tex.vim file (~/.vim/ftplugin/tex.vim on Unix, or $HOME/vimfiles/ftplugin/tex.vim on Windows). Also add one of the OpenPDF functions listed below, depending on your operating system.

"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:

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

Now \e will load the pdf viewer to the page containing the nearest label to the current cursor position (assuming the default backslash for the LocalLeader key). This works well on multi-file projects since the aux file is searched to find the pdf name. So you can jump around in Vim using its wonderful tools, then tell the pdf viewer to jump to the same page.

If you have set up tags, you can also add:

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

to create command :Pdf so 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 to 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 cmcdde, and the following version of OpenPDF can be used:

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

Acrobat needs to be closed before compiling which can be done with the following commands (the has("win32") test allows this code to be in a file that may be used on systems other than Windows, where different commands would be available):

if has("win32")
  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'
endif

The above assumes that cmcdde.exe is in a directory in your PATH. Alternatively, specify the full path name to cmcdde in s:AcroDDE.

Notes for Windows

May merge or remove this material later; keeping as miscellaneous notes for now.

When using Adobe Reader on Windows to view pdf files, the current file is locked. That makes it necessary to close the pdf file before recompiling a tex file into pdf.

To close the pdf output file of the currently edited tex file, use:

:!start cmcdde acroviewr10 control [DocOpen("%:p:r.pdf")][DocClose("%:p:r.pdf")]

DocOpen registers the pdf with the dde server. DocClose then closes the pdf.

For Adobe Reader versions prior to 10, use acroview instead of acroviewr10. Adobe has announced that future versions will also have the version number in the server name.

Some other DDE commands are available with Adobe Reader, for example, DocGoTo and DocGoToNameDest. The first can be used to jump to a particular page in the pdf while the second jumps to a named destination. Perhaps the second one could be used to do a forward search with the Adobe Reader. (The idea is to call DocGoToNameDest with a fixed bookmark, say "cursorposition", and use latex to create that bookmark when compiling. Not sure if there exists some latex packet which could be used for that.)

Here is a trick to open Adobe Reader on the last page: use DocGoTo with a large page number (which doesn't exist):

:!start cmcdde acroviewr10 control [DocOpen("%:p:r.pdf")][DocGoTo("%:p:r.pdf,10000")]

Mac

Need some ideas. Some AppleScript in OpenPDF should do the job.

Comments

Couldnt get this to work