使用Vim编译和显示激光演示中单个幻灯片的PDF格式



我使用Vim作为我的文本编辑器,我非常喜欢beamer作为幻灯片演示工具。然而,编译一个大型的激光演示可能需要一点时间(可能是10或20秒)。对于普通的LaTeX文档来说,这个时间通常是合适的,因为内容通常可以正常工作。在激光幻灯片中,有时会出现文本在幻灯片上是否合适的问题。当幻灯片包含更复杂的图形、文本等布局时也是如此。

我想使用Vim设置一个快捷命令,将活动幻灯片编译为PDF(由光标在相关frame环境之间定义)。

我意识到文档的序言和其他几个特征可以影响幻灯片的确切格式。不过,我想一个近似值就足够了。也许只编辑序言和活动幻灯片就足够了。

这里有一个小函数,它可以完成你想要的(将序言和当前帧复制到一个单独的文件中并进行编译):

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>

按zz将编译光标所在的帧,并将输出放在"current-slide.pdf"中。您可以用您喜欢的任何其他选项替换-halt-on-error。脚本不打开一个单独的窗口,像前面的答案中的函数;您只需继续编辑主文件。我不是vim专家,所以可能有更好的方法来做到这一点,但上面的方法对我来说在创建几个Beamer演示文稿时效果很好。

下面的函数应该创建一个只有序言和当前帧的新临时文件。它以拆分的方式打开文件,从那时起,您应该能够单独编译该文件,并使用您所做的任何程序来查看它。我对tex不太了解,对beamer就更不了解了,所以你可能需要调整一下以更好地满足你的需求。

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