Saturday, October 08, 2011

Make vim-latex to generate output in a specified directory

Recently I started to use latex to write papers.  I want to edit latex in my favorite editor - vim. I found the project vim-latex: http://vim-latex.sourceforge.net/.  It is powerful and convenient to use. 

However, one feature I want is to generate output files (.div, .ps, .log, etc) into a separate directory rather than the same directory as tex files.  It turns out that vim-latex does not support it natively.  So I hacked into the source code to make it work on Windows.

  1. Edit file ~/vimfiles/ftplugin/tex.vim

    Add following config:

      set iskeyword+=: 
      let g:Tex_Outdir='out' 
      let g:Tex_ViewRule_pdf='"Foxit Reader.exe" ' 
      let g:Tex_CompileRule_dvi='mkdir '.g:Tex_Outdir.' & latex -output-directory='.g:Tex_Outdir.' -src-specials --interaction=nonstopmode $*' 
      let g:Tex_CompileRule_pdf=g:Tex_CompileRule_dvi.' & cd '.g:Tex_Outdir.' & dvipdfm $*.dvi'

    Basically above config specifies

    1. output directory of latex compilation
    2. How to view PDF files (if you don't give full path, the command needs to be in env variable PATH)
    3. How to compile dvi: create the output directory and put output there
    4. How to compile pdf: first compile tex to dvi, and then call dvipdfm to generate pdf
  2. Change ~/vimfiles/ftplugin/latex-suite/compiler.vim

    Change line 252 to (this line adds the full path of output directory):
      let execString = 'start '.s:viewer.' "'.expand('%:p:h').'/'.g:Tex_Outdir.'/$*.'.s:target.'"'

    Change line 405 to (this line adds the full path of output directory):
    let execString = 'silent! !'.viewer.' "'.expand('%:p:h').'/'.g:Tex_Outdir.'/'.mainfnameRoot.'.'.s:target.'" '.line('.').' "'.expand('%').'"'

    Note: the text in blue is what I added (diff against the original code)
Use command 'TTarget' to switch amont dvi, ps, pdf, etc.