## Friday, June 12, 2009

### Vim tips for Latex editing

#### Simple Latex Makefile

With this simple Makefile you can compile your latex projects from within vim and use the quickfix window in case of errors to traverse and fix them one by one.

#

# This makefile generates a PDF of the MAINTEX file.

# The output can be found inside the build subdirectory.

#

# Makefile based on  http://www.wlug.org.nz/LatexMakefiles

#

#

# This makefile has been tested in Kubuntu 9.04

#

# Prerequisites:

#

# sudo aptitude install ptex-bin ptex-base okumura-clsfiles ptex-jisfonts \

#     xdvik-ja dvipsk-ja dvi2ps gv jbibtex-bin jmpost mendexk okumura-clsfiles \

#     vfdata-morisawa5 dvi2ps-fontdesc-morisawa5 texlive-latex-extra latexmk \

#     dvipng xpdf gs-cjk-resource vfdata-morisawa5 dvi2ps-fontdesc-morisawa5 \

#

# When writing Japanese make sure your editor is saving the tex files

# in euc-jp encoding. In VIM this can be accomplished by setting the fenc

# variable:

#

#  - Open file as normal using vim <filename>

#  - Type  :edit ++enc=euc-jp

#  - Type  :set fenc=euc-jp

#  - Type  :set enc=utf-8

## TODO Add rule to convert images

## TODO Add makeindex to create .toc files

###########################################################################

## Put here the file name of the main tex file.

MAINTEX      = main

############################################################################

## Change the followin only if you know what you are doing

LATEXCMD     = platex     # [latex | platex]

BIBCMD       = jbibtex    # [bibtex | jbibtex]

DVIPDFCMD    = dvipdf     # [dvipdf | dvipdfm | dvipdfmx]

PDFVIEWER    = xpdf       # [okular | xpdf | evince ]

DVIVIEWER    = xdvi-ja    # [xdvi | xdvi-ja | kdvi ]

## No need to change anything below this line

TEXFILES     = $(wildcard *.tex) TEXINPUTS=:$(PWD)//:    # Path to search for .tex, .cls and .sty files

BSTINPUTS=:$(PWD)//: # Path to search for .bst files BIBINPUTS=:$(PWD)//:    # Path to search for .bib files

TEXMFOUTPUT=$(PWD)/build # Output dir for bibtex and jbibtex LATEXOPTS= -output-directory=build -file-line-error -interaction=nonstopmode export TEXFILES TEXINPUTS BSTINPUTS BIBINPUTS TEXMFOUPUT .PHONY: all wordcount charcount pdf dvi all: build/$(MAINTEX).pdf

build :

@echo "Creating build directory"

@mkdir build

build/$(MAINTEX).aux:$(MAINTEX).tex $(TEXFILES) build$(LATEXCMD) $(LATEXOPTS)$(MAINTEX)

build/$(MAINTEX).bbl: build/$(MAINTEX).aux

$(BIBCMD) build/$(MAINTEX)

build/$(MAINTEX).dvi: build/$(MAINTEX).bbl

$(LATEXCMD)$(LATEXOPTS) $(MAINTEX)$(LATEXCMD) $(LATEXOPTS)$(MAINTEX)

build/$(MAINTEX).pdf: build/$(MAINTEX).dvi

$(DVIPDFCMD) build/$(MAINTEX).dvi build/$(MAINTEX).pdf dvi: build/$(MAINTEX).dvi

$(DVIVIEWER) build/$(MAINTEX).dvi

pdf: build/$(MAINTEX).pdf$(PDFVIEWER) build/$(MAINTEX).pdf # Word counting can be done in VIM usng Ctrl-g g but this command also includes # latex commands. This make rule strips the latex and counts what is left wordcount: @echo Approximate word count: grep -v '^\\\\'$(TEXFILES)|grep -v '^%'|wc -w

charcount:

@echo Approximate char count: grep -v '^\\\\' \$(TEXFILES)|grep -v '^%'|wc -c

Put all your tex files, images, sty, cls, bib, bst files inside a directory with any subfolder tree structure you like. This Makefile sets some environment variables that allow it to find all these files as long as they are below the current directory tree.

Make sure your main tex file is called main.tex or if you prefer another one replace the MAINTEX variable in the Makefile. Take note that it has no extension!.

Also note that this Makefile is to compile Japanese EUC-JP tex files (IEICE Trans). If you do not need Japanese replace platex with simple latex and jbibtex with bibtex.

All output files generated by latex/platex, bibtex/jbibtex are stored inside a build subdirectory.

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Working with Makefiles

"

" Press F5 to compile and open the error window if there

" are errors. If there are errors you can use :cn and :cN to

" jump foward and backward thru the error list. Press :ccl to

" close the error list or F5 again to recompile

map <F5> <ESC>:make<CR><ESC>:botright cwindow<CR> " Compile and open quick fix list

Now when editing you latex files press <F5> to compile them and if errors occur you will be presented with a quickfix window where you can use <F6> and <F7> to quickly jump to the previous and next error message.

Additionally this Makefile also has a few useful commands you can invoke from within vim:

• :!make pdf to display the pdf file using xpdf or if you prefer a different viewer replace the PDFVIEWER variable in the Makefile.

• :!make dvi to display the dvi file using xdvi or if you prefer a different viewer replace the DVIVIEWER variable in the Makefile.

• :!make wordcount to display the number of words in all the tex documents. In vim you can also use g<ctrl-g> in normal mode but this also counts latex commands.

• :!make charcount same as wordcount but for characters instead of words.

#### Latex with Vim TagList plugin

We can enable TagList to display important keywords like section, labels and references for easy navigation (link).

First create a ~/.ctags file that contains the following:

--langdef=tex
--langmap=tex:.tex
--regex-tex=/\\subsubsection[ \t]*\*?\{[ \t]*([^}]*)\}/- \1/s,subsubsection/
--regex-tex=/\\subsection[ \t]*\*?\{[ \t]*([^}]*)\}/+\1/s,subsection/
--regex-tex=/\\section[ \t]*\*?\{[ \t]*([^}]*)\}/\1/s,section/
--regex-tex=/\\chapter[ \t]*\*?\{[ \t]*([^}]*)\}/\1/c,chapter/
--regex-tex=/\\label[ \t]*\*?\{[ \t]*([^}]*)\}/\1/l,label/
--regex-tex=/\\ref[ \t]*\*?\{[ \t]*([^}]*)\}/\1/r,ref/

Then modify the taflist.vim file and add a new language. To do this search for "yacc language" and add these line before it:

" tex language
let s:tlist_def_tex_settings = 'tex;s:section;c:chapter;l:label;r:ref'

" yacc language

Now when you open the taglist window you will get a nice list of section, subsections, chapters, references, labels, etc... that you can navigate and use to jump quickly to each of them.

If you do not have the taglist plugin installed you can follow my instructions here.

#### Reference autocompletion

If you write your labels as \label{fig:something}, then put this in your ~/.vimrc file:

set iskeyword+=:           " type /ref{fig: and prec <C-n> to autocomplete references

With this now you can type \ref{fig: and press to get a list of labels you can cycle as shown in the image above.

#### Spell checking

This is not only for latex files. Simply put the following in your ~/.vimrc file to enable automatic spell checking. Learn how to use the spell checker to correct, add and/or ignore words.

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Enable spell checking in latex, bib and txt files

" Commands:

"            z=      ->  suggest word

"            zg      ->  mark word as good (add to dictionary)

"            zw      ->  mark word as bad  (remove from dictionary)

"            :h spell -> get more details about spelling

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

au BufRead,BufNewFile *.txt,*.tex,*.bib  setlocal spell spelllang=en_us