Alberto Álvarez
11 Apr 2020

pdf-tools as the default PDF viewer in Emacs

I got tired of Emacs opening a .pdf file using an external viewer program. To solve this, I decided to go with PDF Tools for Emacs, which renders the file in a buffer giving you more control and keeping everything inside the editor. To do this, I added the following lines to my .emacs file:

(use-package pdf-tools
   :pin manual
   :config
   (pdf-tools-install)
   (setq-default pdf-view-display-size 'fit-width)
   (define-key pdf-view-mode-map (kbd "C-s") 'isearch-forward)
   :custom
   (pdf-annot-activate-created-annotations t "automatically annotate highlights"))

This would just make PDF Tools available for Emacs but wouldn't make it a default. To do this, I added a few more lines of code:

(setq TeX-view-program-selection '((output-pdf "PDF Tools"))
      TeX-view-program-list '(("PDF Tools" TeX-pdf-tools-sync-view))
      TeX-source-correlate-start-server t)

(add-hook 'TeX-after-compilation-finished-functions
          #'TeX-revert-document-buffer)

PDF Tools is an Emacs support library for PDF files and it has a lot of features like annotations, I just use it for .pdf visualisation but it has a bunch of features that are worth exploring.

Something important is that this library doesn't play well with Emacs linum-mode. The following lines of code will deactivate this mode when rendering the .pdf:

(add-hook 'pdf-view-mode-hook (lambda() (linum-mode -1)))

This simple change allows me to open all my PDFs in emacs while doing research. This plays really well with Emacs extensions like org-ref and org-roam.

Tags: latex emacs
alberto.am by Alberto Álvarez is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.