Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile and display PDF of individual slide in beamer presentation using Vim

Tags:

vim

latex

beamer

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 (perhaps 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.

like image 223
Jeromy Anglim Avatar asked Sep 15 '25 20:09

Jeromy Anglim


2 Answers

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.

like image 73
Jon Avatar answered Sep 17 '25 10:09

Jon


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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!