DEV Community

Cover image for Vim to the rescue: PDF Preview
Angad Sharma
Angad Sharma

Posted on

Vim to the rescue: PDF Preview

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 logo L04DB4L4NC3R / texgroff.vim

A plugin to open groff output pdf in preview

texgroff.vim

A plugin to open groff/latex/markdown live pdf preview


Requirements

  • groff with the groff_ms macro
  • pandoc for markdown and latex
  • zathura pdf viewer

Installation

  • Without vim package management system: Copy the main.vim file to your .vim/autoload

  • Pathogen:

git clone https://github.com/L04DB4L4NC3R/texgroff.vim ~/.vim/bundle/texgroff.vim
Enter fullscreen mode Exit fullscreen mode

Keybindings

Leader Key Action
/ p Open live PDF Preview
/ q Compile groff(.ms)/latex(.tex)/markdown(.md)

Demonstration

View the demo on Youtube

Made with ❤️ by Angad Sharma






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:

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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:

Oldest comments (0)