loading...
Cover image for Vim to the rescue: PDF Preview

Vim to the rescue: PDF Preview

l04db4l4nc3r profile image Angad Sharma ・2 min read

Vim to the rescue (9 Part Series)

1) Vim to the rescue: Prologue 2) Vim to the rescue: Tree to Markdown 3 ... 7 3) Vim to the rescue: Repetition Made Easy 4) Vim to the rescue: Human Readable Processes 5) Vim to the rescue: Multiple Cursors 6) Vim to the rescue: Sessions 7) Vim to the rescue: Attached Terminal 8) Vim to the rescue: Subduing the Shell 9) 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

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

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:

Vim to the rescue (9 Part Series)

1) Vim to the rescue: Prologue 2) Vim to the rescue: Tree to Markdown 3 ... 7 3) Vim to the rescue: Repetition Made Easy 4) Vim to the rescue: Human Readable Processes 5) Vim to the rescue: Multiple Cursors 6) Vim to the rescue: Sessions 7) Vim to the rescue: Attached Terminal 8) Vim to the rescue: Subduing the Shell 9) Vim to the rescue: PDF Preview

Posted on Jun 1 by:

l04db4l4nc3r profile

Angad Sharma

@l04db4l4nc3r

Backend Developer and DevOps SysAdmin

Discussion

markdown guide