Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Friday, September 23, 2011

Synctex with GVim and Okular

What is synctex?

Synctex is the method of synchronization of your latex editor with the pdf/dvi output. Forward synchronization is where you jump from any line in your latex editor to the corresponding line in pdf/dvi output. Reverse synchronization is where you jump any line in your pdf/dvi output to the corresponding line in your tex editor. Synctex has been around since TeXLive 2008. Many latex editors and pdf/dvi viewer support synctex. Some of these are texworks texmaker, gedit with latex plugin. All of the above can be installed in ubuntu via synaptic. However, AFAIK the lucid version of gedit latex plugin doesn't support synctex. It's better to install the gedit plugin from it's website. GVim by default doesn't support synctex. I wrote couple of scripts, which, along with okular helps in bringing synctex support in GVim.
Even though I haven't tried implementing synctex in Emacs editor, for those of you who are interested can check this blog post out.

Requirements 


The following are the requirements:
  • Vim version 7 or higher.
  • Okular with synctex option compiled. Version 0.10.5 works.
  • latexmk: Version 4.25 or higher
  • Zenity
  • (g)awk
In ubuntu install the above softwares (excepting latexmk) via synaptic or apt-get
sudo apt-get install vim-gtk okular zenity gawk
The latexmk package found in Ubuntu lucid (10.04) is an older version. One needs at least version 4.25. Download latexmk from here. Unzip it and copy the file latexmk.pl to your $PATH.
mkdir -p $HOME/bin/             ## Creating $HOME/bin/ directory.
chmod +x latexmk.pl             ## To make the script executable.
cp latexmk.pl $HOME/bin/latexmk ## Copying and changing the name
## of the script to latexmk.
Note that if you didn't have a $HOME/bin/ directory, then you have to logout and log back in.    

Installation of scripts

Download the scripts makelatex2, deljunk and makelatex.vim. After downloading the scripts rename them makelatex2, deljunk and tex_makelatex.vim respectively. Copy the scripts makelatex2, deljunk to $HOME/bin/. To make the scripts executable run the command
chmod +x makelatex2 deljunk 
cp makelatex2 $HOME/bin/
cp deljunk $HOME/bin/
Copy the file tex_makelatex.vim to $HOME/.vim/ftplugin/ directory. Create the directory if you don't have it. Notice the ``dot" in-front of ``vim". This makes it a hidden directory. Also copy the file editexisting.vim from the runtime/macros directory of vim to your local plugin directory. This is required for reverse search.

mkdir -p $HOME/.vim/ftplugin
cp tex_makelatex.vim $HOME/.vim/ftplugin
mkdir -p $HOME/.vim/plugin
cp /usr/share/vim/addons/plugin/editexisting.vim $HOME/.vim/plugin/
Create a hidden file ``$HOME/.vimrc" if you already don't have one and add the following lines.
" For filetype plugins
filetype plugin on
Note that the ``dot" infront of vimrc. The dot makes the file hidden.

Okular config


Okular > Settings > Configure Okular > 
    General > Program Features > Uncheck "Reload document on change"
    Editor > 
        Editor: Custom Text Editor
        Command: gvim +%l %f

Usage

Open or create a new tex file in gvim. Press ``Esc" and then ``Alt+1" to compile via pdflatex. Okular automatically opens the pdf file in the exact page where your cursor is in gvim.
For forward search (gvim to okular), put your cursor in any text part of your tex file and press ``Esc" and then ``Shift+f". Okular opens the file to the exact page of your text.
For reverse search (okular to gvim), ``Shift+click" on any part of your pdf file in okular. Gvim scrolls the file in the exact location.

Multi-document project

For a multi-document project, each sub-document files should have the following ``magic comment" in the first 5 lines of the tex file
%!TeX root = master.tex
where ``master.tex" is the main tex file on which compilation ought to be done.


Selecting type-setting command

Compilation by default is via pdflatex. However to compile via some other typesetting command (eg. latex+dvipdf), specify the typesetting command to be used via the following magic comment in the first 5 lines of the input tex file
%!TeX program = latex+dvipdf
This is compatible with texworks. Typesetting commands can be
pdflatex
    latex
    latex+dvips
    latex+dvipdf
    latex+dvips+ps2pdf
    xelatex
Custom typesetting commands are also supported. Note that
  • Forward and reverse search doesn't work with ps file, i.e. with latex+dvips.
  • For multi-document project the typesetting comment ``%!TeX program" has to be specified only in the master tex file.

Optional (g)vim configurations

For other useful (g)vim configurations you can check out my last two posts on the topic here and here.


This file was generated via LATEX2html.

Monday, September 19, 2011

Vim configurations: Part 2

Continuing from my last post on vim configurations, in this post I will discuss some vim plugins that I have written, along with couple of third-party plugins which I find very useful. If you already haven't installed the full vim (with gui support), then do it. For ubuntu the package is called vim-gtk. Also if you don't have the following directory structure, create them before we start.
mkdir -p ~/.vim/plugin
mkdir -p ~/.vim/ftplugin

Scripts I have written

There are a couple of vim scripts that I have written over the years. I will discuss two of them now.

CommentLines

This script was written to (un)comment multiple lines simultaneously. It was inspired by vim tip #271 from the vim tips website, now appropiately called the vim tips wiki. You can download the plugin from here.
After downloading the file, rename it CommentLines.vim and copy the file to $HOME/.vim/plugin/. Now open any programming file (c, perl, tex, python, html, shellscript etc) in gvim. To comment lines select multiple lines and press ctrl+k. To uncomment select multiple commented out lines and press ctrl+l.
The script already has support for various types of programming language as mentioned. Support for other languages can be easily added.

ColorComment

Commented out text in gvim by default appears as blue, which IMHO grabs too much attention. So I wrote the ColorComment plugin. Grab it from here. After downloading it rename and copy it in the $HOME/.vim/plugin/ directory. Now if you open any programming file the commented out text appears in gray color. If you want to (un)highlight your comments, press Esc and then press ctrl+g. Ctrl+g is a toggle switch via which you can toggle between highlighted and un-highlighted comments.

Other useful scripts

There are couple of other scripts, written by other vim users, which I not only find very useful, but also use them on a daily basis.

editexisting

Ever tried to re-open the same file twice in gvim and seen this message?

What gvim should should have done is jump to the already opened file. With gvim version 7 one can do just that with the plugin ``editexisting.vim". Copy the script ``editexisting.vim" to your local plugin directory.
cp /usr/share/vim/addons/plugin/editexisting.vim "$HOME"/.vim/plugin/
Now if you try to re-open a file which is already open in gvim, that particular gvim window will come into focus without showing you the above message. This however only works with gvim, but not console based vim.

snipMate

Often while programming or say writing in latex we reuse the same commands. If we could write these commands via a few keyboard shortcuts then, we could type a bit faster. This is possible via the plugin snipMate. Download the snipMate plugin and follow the instructions given in the website to install it. This plugin has support for lot of programming languages. Now say we want to add support for latex. Open the file $HOME/.vim/snippets/tex.snippets. This file already has a couple of snippets, but adding custom code snippets is quite easy. An example will make this clear. Let' say we type the following code in the tex.snippets file.
# Begin equation
snippet beq
 %
 \begin{equation}
 ${1}
 \end{equation}
 %
Now open any tex file and type ``beq" and press ``tab". ``beq" will get expanded to
%
\begin{equation}

\end{equation}
%
with the cursor blinking in-between \begin{equation} and \end{equation}. This is but a small example of what you can do with snipMate. Use the command
:help snipmate
in gvim to learn more.

LatexParFormat

Before I started using this script, I used to often find myself trying to manually resize paragraphs in latex after adding some text in-between paragraphs, so that the tex file would look nicely formatted. Meaning the lines would break off at column 72. Now with LatexParFormat, I can do just that with a shortcut key.
Follow the install instructions in the above website. Open any tex file in gvim and scroll to any paragraph. Press ``Esc" and then ``ctrl+j". The paragraph becomes nicely formatted.

That's all for now. If I write any more vim plugins or find other useful scripts, then I will definitely share them with you.

 In my next post I plan to talk about synctex using GVim and Okular. If you are wondering what synctex is all about, then a video in this blog post makes it clear.

Friday, September 16, 2011

Vim configurations: Part 1

Installation of (g)vim

Vim or it's graphical version (gvim) is one of the best text editors. (Emacs is another great editor - just mentioned it because I don't want to start a flame war here. ;))
However, some of it's features are hidden and can be activated by playing with it's config files a bit. In ubuntu vim-tiny is installed by default. To get more out of it one should install the vim-gtk package.
sudo apt-get install vim-gtk
Before we start create the following directory structure.
mkdir -p ~/.vim/plugin
mkdir -p ~/.vim/ftplugin
Notice the dot at the beginning of ``vim" which makes the directory hidden.

Vim configs

My personal vimrc is available here. Copy my vimrc or create the file $HOME/.vimrc. As before notice the dot in front of vimrc which makes the file hidden.
Even though my vimrc is heavily commented, for the sake of completeness let me point out the benefits of couple of the options.

" Set vim to be nocompatible, so as not to be compatible with vi
" Highly recommended: Has to be the first line.
set nocompatible

" To set status line, so as to highlight the status bar below.
set laststatus=2

" When editing a file, always jump to the last known cursor
" position so that when you reopen the file you don't have 
" to scroll to your edit position.
autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal g`\"" |
    \ endif

" For recognizing specific file types
let file_ext = bufname("%")

" Wraps line at the 5th column from the right margin and an 
" <EOL> is inserted 
if file_ext =~ '\.txt$' || file_ext =~ '\.tex$' 
    set wrapmargin=5
endif

" To wrap long lines
set wrap

" Allow backspacing over everything in insert mode
set backspace=indent,eol,start

" To getrid of annoying ~ files
set nobackup

" To show current mode
set showmode

" Show line number,column number always
set ruler

" Show report when N lines were changed
" report=0 means "show all changes"!
set report=0

" Show title of file
set title

" Ignore filename with the following suffixes when using
" :edit or :sp  
set suffixes=.aux,.log,.pdf,.ps,.tar,.gz,.tgz,.dvi,.bbl,.blg,
    \.eps,.out,.png,.los,.lof,.lot,.dat,.sty,.xml,.toc,
    \.latexmain,.bm,.idx,.ilg,.ind,.fdb_latexmk

" Auto indent
set autoindent
" Smart indent
set smartindent

" To toggle between set number and set nonumber
nmap <C-N> :set number! <CR>

" To ignore case in search pattern
set ignorecase
set smartcase

" To incrementally search pattern
set incsearch

" No tabs in the source file
" All tab characters are of 4 space characters
set softtabstop=4
set tabstop=4
set shiftwidth=4
set expandtab

" Fortran tabs won't be colored red (Have to loaded before
" 'syntax' command)
let fortran_have_tabs=1

" Switch on syntax highlighting if it wasn't on yet.
if !exists("syntax_on")
  syntax on
endif

" Allow switching buffers, which have unsaved changes
set hidden

" Switch on search pattern highlighting.
set hlsearch

" For filetype plugins
filetype plugin on

" To handle common typos in commands
command! Q  quit
command! W  write
command! Wq wq
command! WQ wq

" Spelling auto correction: Auto correcting typos
iabbr adn and
iabbr nad and
iabbr teh the
iabbr hte the
iabbr alos also
iabbr aslo also
iabbr qed QED
iabbr qcd QCD
Okay, I discussed almost all my vimrc configs :).  

GVim configs

GVim is the GUI version of vim. Some configurations are specific to gvim. You can download my personal gvimrc config file from here. Again as before copy my gvimrc or create the file $HOME/.gvimrc.
" Auto Change Directory
set acd

" For shortcut keys as in Windows
" Block selection "Ctrl+v" gets mapped to "Ctrl+q"
source $VIMRUNTIME/mswin.vim
The line source $VIMRUNTIME/mswin.vim would be most useful for people who are used to shortcut keys.

In my next posting I will discuss some (g)vim plugins that I have written along with a couple of third party plugins which I find very useful.