Wednesday, September 28, 2011

Synctex in Kile and Okular

In my last post I had discussed about configuring gvim and okular with synctex. In this post I will discuss configuring kile and okular with synctex.

Configuration of Kile for pdflatex

Add the option -synctex=1 for building via pdflatex.

Kile > Settings > Configure Kile > Tools > Build 
            Command: pdflatex
            Options: -interaction=nonstopmode -synctex=1 '%source'
One can do the same configuration for latex as opposed to pdflatex. The ForwardPDF build config should already be present. If not then do the following:
Kile > Settings > Configure Kile > Tools > Build 
            Command: okular
            Options: --unique '%absolute_target'
            Run Outside of Kile
            Source extension: pdf
            Target extension: pdf
            State: Editor
            Add tool to Build menu: View
Finally configure the Quick build option.
Kile > Settings > Configure Kile > Tools > Build 
    Quick Build

Configuration of Okular

Okular > Settings > Configure Okular > 
    General > Program Features > Uncheck 
    "Reload document on file change"
    Editor > Editor: Kile


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

Other kile configs

Couple of other kile configs which I found useful.
Kile > Settings > Configure Kile > Editor > Editing > General 
    # Tabs configuration
    Insert spaces instead of tabulators
    Highlight tabulators
    Tab Width 4 characters
    # For word wrap (static) 
    # To avoid a long line and also to justify text
    Enable static word wrap
    Show static word wrap marker (if applicable)
    Word wraps at 80 characters
This post however doesn't deal with multi-document projects (Eg: thesis). In my next post I will talk about how to implement handling multi-document projects in Kile + Okular with synctex.
This file was generated via LATEX2html.

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.


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 to your $PATH.
mkdir -p $HOME/bin/             ## Creating $HOME/bin/ directory.
chmod +x             ## To make the script executable.
cp $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


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
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.


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.


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.


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.


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
Now open any tex file and type ``beq" and press ``tab". ``beq" will get expanded to

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.


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

" 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,

" 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

" 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.

Thursday, September 15, 2011

Accelerate download of apt-get via aria2 in Ubuntu

Need for download accelerator

In ubuntu software installation is done via apt-get or synaptic. Both of them download the required packages from the repositories via wget. While wget is great, it doesn't accelerate the download. After a bit of research (read googling) I found the script apt-fast script written by Matt Parnell and the apt-axel script written by freshmeat user jespino. Both of these scripts use axel (a command line download accelerator) to accelerate the download of apt-get.
Unfortunately I couldn't configure axel for proxy support. Since I was behind proxy, this was a major deal killer. Finally after a bit of googling I found that aria2, another command line download accelerator supported proxy. So I re-wrote the apt-fast script to use aria2 instead of axel and named it apt-aria2 (Not very original name I admit).


You off-course need to install aria2
sudo apt-get install aria2


Installation is pretty easy. Just download the apt-aria2 script, make it executable and put it in your $PATH.
chmod +x apt-quick
sudo cp apt-quick /usr/local/bin/
Note that putting the apt-aria2 script in $HOME/bin will not work even though it is in the $PATH. As a user you will be able to access it. However using the sudo command you won't be able to use the script. This is probably because $HOME/bin is not in the $PATH of the superuser, but only the user.


Proxy settings

Proxy setting is same as proxy setting for apt-get. Open or create the apt.conf file
sudo gedit /etc/apt/apt.conf
and copy the following lines
## Proxy settings
Acquire::http::proxy "http://username:password@your.proxy:port/";
Acquire::ftp::proxy "ftp://username:password@your.proxy:port/";
Acquire::https::proxy "https://username:password@your.proxy:port/";
where you should replace the username, password, proxy and port by your own username, password, proxy and port respectively. Duh! In case of couple of softwares (eg.: flashplugin-installer and ttf-mscorefonts-installer) download of the actual software is always via wget. In that case one should also configure the wget proxy. Open the wgetrc file
sudo gedit /etc/wgetrc
Scroll down to the proxy lines and replace them by
https_proxy = http://username:password@your.proxy:port/
http_proxy = http://username:password@your.proxy:port/
ftp_proxy = http://username:password@your.proxy:port/
use_proxy = on


Usage is pretty much the same as apt-get. A few examples are as follows:
sudo apt-aria2 install package1 package2
sudo apt-aria2 update
sudo apt-aria2 upgrade
sudo apt-aria2 remove package1
sudo apt-aria2 purge package1
In case of update, it directly uses apt-get. In other cases where one has to download the software (Eg.: install, upgrade, dist-upgrade) it downloads the software via aria2 and installs in via apt-get.


Multiple simultaneous downloads

Apt-get always locks the process, so at any given time one can only use one instance of apt-get. However using the ``-d" (download-only, no installation) option of apt-aria2, one can use multiple instance of apt-aria2. Once download is complete for all packages, one then has to use apt-aria2 again without the ``-d" option, to install the same packages. Eg:
## In terminal 1
sudo apt-aria2 -d install package1 package2
## In terminal 2
sudo apt-aria2 -d install package3
## After all downloads are complete
sudo apt-aria2 install package1 package2 package3
However, one should use the ``-d" option with caution. The packages being installed simultaneously
  • shouldn't have the same dependencies
  • shouldn't conflict with each other.

Known bug

If more than one packages are being installed, then once apt-aria2 starts, its difficult to stop the download by pressing ``Ctrl+c". This is because, as soon as one presses ``Ctrl+c", it stops the current download and starts downloading the next in line. I can't seem to write the script which would stop it once and for all. If someone knows the solution to this let me know.
This document was generated via LATEX2html.

Tuesday, September 13, 2011

Linux man pages to pdf

Anybody who has ever handled a unix type machine, at some point or other has used the command

man command-name

to  check the manual of the said command. Ever wanted to convert the man page to pdf so that you could search through it or even print it for that matter?
There is an easy solution for this. Run the command

man -t command-name | ps2pdf - > filename.pdf

And your man page of the command get converted to a pdf file.

man -t gnuplot | ps2pdf - > gnuplot_man.pdf

You can even write a small shell-script for this. Fire up your favorite text editor
and paste the following code

man -t "$Cmd" | ps2pdf - > "$Code"_man.pdf

Save it as man2pdf and make it executable.

chmod +x man2pdf

Now all you have to do is run the command like this:

./man2pdf command-name

You could even move it to your local bin directory if you want.

Try it. :)