Commits

John Lenz committed f342ae3

Add post on vim plugins for TeX

  • Participants
  • Parent commits fe7b5d2

Comments (0)

Files changed (3)

File posts/2013-04-05-latex-vim-plugins.markdown

+---
+title: TeX and LaTeX plugins for Vim
+author: John Lenz
+tags: latex, vim
+date: April 5, 2013
+---
+
+I used [Vim-LaTeX](http://vim-latex.sourceforge.net/) for many years, but in the past year or so I
+have dropped Vim-LaTeX and replaced it with several single-purpose plugins.  My main issues with the
+Vim-LaTeX plugin was the way it handled compilation and error messages, I didn't like the way it
+handled folding, I wanted more control over snippets, and there was quite a bit of the package I did
+not use.  Don't get me wrong, Vim-LaTeX is a great plugin and I was happy with it for a long time,
+but just small little things led me to investigate alternatives.
+
+# Managing Plugins
+
+The best way of managing plugins is [pathogen](https://github.com/tpope/vim-pathogen).  I'm not a
+big git user, so I install and use the hg-git plugin for mercurial and then clone the various
+plugins using mercurial into the .vim/bundle directory.  Also, instead of using subrepositories or
+[guestrepo](https://bitbucket.org/selinc/guestrepo), I wrote a [short shell
+script](../code/vimplugs.sh) to initially clone and keep the plugins up to date.  Maybe instead I
+could make all the plugins guestrepo's of my main academic mercurial repository, but the script
+works for me.  I store this script in my main academic mercurial repository so it is easy to keep
+the list of plugins synced between computers.
+
+# Vim Configuration
+
+I keep my main vim configuration inside my academic mercurial repository, so my entire ~/.vimrc is
+
+~~~ {.vim}
+set nocompatible
+filetype off
+set rtp+=~/.vim/bundle/vim-pathogen/
+call pathogen#infect()
+
+source ~/academic/tools/vimrc
+
+filetype plugin indent on
+~~~
+
+and the first line of ~/academic/tools/vimrc is
+
+~~~ {.vim}
+set rtp+=~/academic/tools
+~~~
+
+Therefore, ~/academic/tools is on the runtime path so for example my TeX specific configuration is
+in the file ~/academic/tools/ftplugin/tex.vim, all tracked and synced by mercurial.  My ~/.vim
+directory is empty except for the bundle directory for pathogen, but even that could be removed if I
+moved to guestrepos.
+
+# Some General Plugins
+
+The general (non TeX-specific) plugins I use are [nerdtree](http://github.com/scrooloose/nerdtree),
+[bufexplorer](http://github.com/vim-scripts/bufexplorer.zip),
+[ctrlp](http://github.com/kien/ctrlp.vim), and
+[lawrencium](https://bitbucket.org/ludovicchabant/vim-lawrencium).  In particular, ctrlp is awesome,
+especially the ability to jump to tags. When building (see below), I create tags from labels, so
+with ctrlp I can quickly jump to a lemma or theorem no matter which file it is in.
+
+# Building
+
+To build, I use the tool [rubber](https://launchpad.net/rubber).  I wrote a generic makefile
+[latex.mk](../code/latex.mk), which I store at ~/academic/tools/latex.mk.  Note several things about
+this makefile which you might need to edit: I have hard-coded the path to my
+[references](2012-06-15-bibtex-and-pandoc.html) and also a hard-coded check that a file exists (to
+see if the repository is checked out properly).
+
+Inside ~/academic/tools/ftplugin/tex.vim, I have the following:
+
+~~~ {.vim}
+setlocal errorformat=%f:%l:\ %m,%f:%l-%\\d%\\+:\ %m
+if filereadable('Makefile')
+    setlocal makeprg=make
+else
+    exec "setlocal makeprg=make\\ -f\\ ~/academic/tools/latex.mk\\ " . substitute(bufname("%"),"tex","pdf", "")
+endif
+~~~
+
+For small projects I don't write a custom makefile and so tex.vim sets makeprg to directly use
+latex.mk.  For my larger projects, I create a makefile that looks something like the following (from
+[this paper](http://arxiv.org/abs/1201.2123)).
+
+~~~ {.makefile}
+PAGES=Multicolor?Ramsey \
+      Eigenvalues?and?Expanders
+
+finite_field_ams_DEPS=construction.tex intro.tex ramsey.tex appendix.tex smallm.tex openprob.tex
+
+.PHONY: all
+all: finite-field-ams.pdf tags
+
+include ../../tools/latex.mk
+~~~
+
+Either with or without the makefile, I can type ":make" within vim and the combination of rubber
+and the errorformat I set causes the errors to be pulled into the vim quickfix window, so can list
+errors, jump between errors, etc. using the normal vim quickfix commands.
+
+# Next Time
+
+In the next post, I will show how I setup [snippets](https://launchpad.net/ultisnips) and
+folding by markers from the snippets, forward searching to open the resulting PDF to a location
+matching the location in vim, and some other short custom vim code I have accumulated to help with
+LaTeX editing.

File static/code/latex.mk

+.PHONY: clean
+
+ifeq "$(REFNAME)" ''
+REFNAME=refs.bib
+endif
+
+ifneq "$(PAGES)" ''
+PWITHPATH=$(addsuffix .page,$(addprefix ~/academic/gitit/,$(PAGES)))
+CHECKPATH=$(wildcard ~/academic/gitit/Ramsey-Turan.page)
+ifeq "$(CHECKPATH)" ''
+REFS=$(EXTRAREFS)
+else
+REFS=$(REFNAME) $(EXTRAREFS)
+$(REFNAME): $(PWITHPATH)
+	~/academic/gitit/mkbib.sh $(subst ?,\ ,$(PWITHPATH)) > $(REFNAME)
+endif
+endif
+
+.SECONDEXPANSION:
+
+%.pdf: %.tex $(DEPENDS) $(REFS) $$($$(subst -,_,$$*)_DEPS)
+	rubber -f --pdf -s $<
+	rubber-info --check $<
+
+tags: *.tex
+	ctags --langdef=latex --langmap=latex:.tex '--regex-latex=/\\label\{([^}]*)\}/\1/l,label/' *.tex
+
+clean:
+	rm -rf $(REFNAME) *.aux *.bbl *.blg *.log *.pdf *.toc *.snm *.out *.nav tags

File static/code/vimplugs.sh

+#!/bin/bash
+
+plugs=()
+plugs+=("git://github.com/tpope/vim-pathogen")
+ # Many more plugins go here
+
+mkdir -p ~/.vim/bundle
+cd ~/.vim/bundle
+
+COL_BLUE="\x1b[34;01m"
+COL_RESET="\x1b[39;49;00m"
+
+for repo in "${plugs[@]}"; do
+	dir=`echo $repo | sed 's/.*\///'`
+	if [ -d "$dir" ]; then
+		echo -e "${COL_BLUE}Updating $repo ${COL_RESET}"
+		(cd "$dir" && hg pull -u)
+	else
+		echo -e "${COL_BLUE}Cloning $repo ${COL_RESET}"
+		hg clone "$repo" "$dir"
+	fi
+done
+
+vim -c ':Helptags' -c q