Compile and display PDF of individual slide in beamer presentation using Vim
I use Vim as my text editor and I quite like beamer as a slide presentation tool. However, compiling a large beamer presentation can take a little bit of time (per开发者_开发技巧haps 10 or 20 seconds). This time is generally fine for a normal LaTeX document because the content often just works. In beamer slides, there are sometimes issues about how well the text fits on the slide. This is also true when a slide involves a more complex layout of graphics, text, and so on.
I'd like to set up a shortcut command using Vim that just compiles the active slide as a PDF (as defined by the cursor being between the relevant frame
environment.
I realise that the preamble and several other features of the document can influence the exact formatting of the slide. However, I imagine an approximation would be sufficient. Perhaps it would be sufficient just to compile the preamble and the active slide.
Any suggestions would be most helpful.
Here's a small function that does what you want (copies the preamble and current frame into a separate file and compiles it):
function! CompileCurrentSlide()
let tmpfile = "current-slide.tex"
silent! exe '1,/\s*\\begin{document}/w! '.tmpfile
silent! exe '.+1?\\begin{frame}?,.-1/\\end{frame}/w! >> '.tmpfile
silent! exe '/\s*\\end{document}/w! >> '.tmpfile
silent! exe '!pdflatex -halt-on-error '.tmpfile.' >/dev/null'
endfunction
"
noremap <silent><buffer> zz :silent call <SID>CompileCurrentSlide()<CR>
Pressing zz will compile whatever frame the cursor is in and put the output in "current-slide.pdf". You can replace -halt-on-error with whatever other option you'd like. The script doesn't open a separate window, like the function in the previous answer; you just continue to edit the main file. I'm not a vim expert so there may be better ways to do this, but the above has worked fine for me in the creation of several Beamer presentations.
The following function should create a new temporary file with only the preamble and the current frame. It opens up the file in a split, and from that point onward, you should be able to compile that file alone and use whatever program you do for viewing it. I don't know a lot about tex and even less about beamer, so you may have to tweak this to better fit your needs.
function! CompileBeamer()
" Collect the lines for the current frame:
let frame = []
if searchpair('\\begin{frame}', '', '\\end{frame}', 'bW') > 0
let frame = s:LinesUpto('\\end{frame}')
call add(frame, getline('.')) " add the end tag as well
endif
" Go to the start, and collect all the lines up to the first "frame" item.
call cursor(1, 1)
let preamble = s:LinesUpto('\\begin{frame}')
let body = preamble + frame
" Open up a temporary file and put the selected lines in it.
let filename = tempname().'.tex'
exe "split ".filename
call append(0, body)
set nomodified
endfunction
function! s:LinesUpto(pattern)
let line = getline('.')
let lines = []
while line !~ a:pattern && line('.') < line('$')
call add(lines, line)
call cursor(line('.') + 1, 1)
let line = getline('.')
endwhile
return lines
endfunction
精彩评论