Introduction

Some time ago, I was writing my resume. People suggested writing it in LaTeX. So I started reading up on it. Then I stumbled upon a tool called groff and found it much easier, faster and less bloat than LaTeX.

Both of these tools are used to write documents that can be converted into PDFs. I wanted a solution in vim so that I could live preview these PDFs as I was writing them. So I decided to make a vim plugin that does so:

{% github L04DB4L4NC3R/texgroff.vim %}

It supports LaTeX, groff, as well as markdown.


What we are going to use

  • Zathura: It is a very minimalist PDF reader for vim users.
  • Groff: The GNU document parser. It is installed by default on most linux systems
  • Pandoc: It is the general purpose document conversion tool. See my blog on pandoc:

{% link https://dev.to/l04db4l4nc3r/presentable-dev-posts-with-pandoc-56pc %}

All of these tools are already available in most official linux repositories and can be downloaded using your vanilla packet manager:

sudo apt install groff pandoc zathura

On Arch based distros, zathura-pdf-poppler needs to be downloaded in addition.


What we want

We want that the \ + q keybinding to compile the document we are working on into a PDF and the \ + p keybinding to preview the PDF in zathura after compiling it.

# Compiling Markdown to PDF:
pandoc curr.md -s -o /tmp/op.pdf

# Compiling LaTeX to PDF: 
pandoc -f latex -t latex curr.tex -o /tmp/op.pdf

# Compiling Groff (ms macro) to PDF:
groff -ms curr.ms -T pdf > /tmp/op.pdf

Getting to the vim script

The following code divides the process into 2 functions, namely Compile and Preview. The former checks our current file type and applies the appropriate compilation command to it. The latter opens up the output PDF in zathura. Add the following code in your ~/.vimrc:

let mapleader="\\"

" Call compile
" Open the PDF from /tmp/
function! Preview()
		:call Compile()<CR><CR>
		execute "! zathura /tmp/op.pdf &"
endfunction

" [1] Get the extension of the file
" [2] Apply appropriate compilation command
" [3] Save PDF as /tmp/op.pdf
function! Compile()
		let extension = expand('%:e')
		if extension == "ms"
				execute "! groff -ms % -T pdf > /tmp/op.pdf"
		elseif extension == "tex"
				execute "! pandoc -f latex -t latex % -o /tmp/op.pdf"
		elseif extension == "md"
				execute "! pandoc % -s -o /tmp/op.pdf"
		endif
endfunction

" map \ + p to preview
noremap <leader>p :call Preview()<CR><CR><CR>

" map \ + q to compile
noremap <leader>q :call Compile()<CR><CR>

When we press preview, our zathura instance opens up. Now the best thing about zathura is that it watches the opened file. So after you press preview for the very first time, you don’t have to press it again. Simply compile to view the changes in the PDF. Here is what our extension looks like:

{% youtube s4gVmJafKf0 %}