I'm running auctex and pdf-tools to view pdf's within emacs. My problem is that I'd like to scale the pdf's buffer window horizontally after running C-c C-c.

So far, I've created a hook to execute the settings I want in the buffer.

(add-hook 'pdf-view-mode-hook
        (lambda ()
      ;; key-bindings
      ;; dark mode
        (setq pdf-view-midnight-colors '("#f6f3e8" . "#242424"))
      ;; open pdfs scaled to fit page
        (setq-default pdf-view-display-size 'fit-page)
      ;; more fine-grained zooming
        (setq pdf-view-resize-factor 1.1)
      ;; disable line-numbering
        (display-line-numbers-mode 0)
      ;; resize horizontally
        (setq right-margin-width 0)
        (setq left-margin-width 0)
        (setq fit-window-to-buffer-horizontally 0)

Now, the issue is that the window does not scale correctly at first. If I run fit-window-to-buffer manually (a few times), it scales the window nicely, but I'd like to avoid having to manually do this every time.

I'm guessing that the problem is that it's trying to scale before the pdf has loaded? If so, is there a way to delay the call to fit the window? If not, what am I doing wrong?

Partial Solution:

It appears that the following partially works.

(defun my/pdf-fit ()
  "Calculates the difference between the pdf image and frame width
before resizing the frame by the difference"
  (let ((W (- (car (pdf-view-image-size)) (window-pixel-width))))
    (window-resize (get-buffer-window) W t nil t)

I added this to the pdf-view-after-change-page-hook. I have since moved over to doom-emacs and substantially simplified my configs (which I can highly recommend).

Thanks to phils and NickD for the suggestion.

  • 2
    I strongly recommend using a named function so that you can tweak the definition without having to firstly remove the old lambda from the hook variable, and then update the code, and then add the new/modified lambda to the hook variable. You might want to check C-h v pdf-view-mode-hook to verify that you haven't left multiple versions in there.
    – phils
    Sep 7, 2020 at 2:58
  • 1
    If you still have problems with pdf-view-mode-hook after you clean it up as @phils suggests, maybe a different hook would work better? Two likely ones appear to be pdf-view-change-page-hook and pdf-view-after-change-page-hook.
    – NickD
    Sep 7, 2020 at 16:59
  • In my experence doc-view-fit-window-to-page works, even with pdf-tools.
    – ruwey
    Oct 12, 2023 at 11:06

1 Answer 1


As you are mentioning that you have to call fit-window-to-buffer a few times, I am not sure if this is gonna work, but you might try to delay execution using a timer as follows:

(add-hook 'pdf-view-mode-hook (lambda () (run-with-timer 0.1 nil #'fit-window-to-buffer)))

or, optionally, you might use a more appropriate hook.

This trick solves a similar issue when trying to automatically activate pdf-view-roll-minor-mode (see image-roll)

If it does not work directly, then you could try to call fit-window-to-buffer multiple times by adding the following function to the hook instead:

(defun pdf-fit-window-hook-function ()
  (dotimes (_ 2) (fit-window-to-buffer)))

You might also have to set fit-window-to-buffer-horizontally to t, but I guess you know that already.

Finally, additionally you might want to comment out the following lines in pdf-view-display-image to remove the whitespace before the image:

        (overlay-put ol 'before-string
                     (when (> (window-width window)
                       (propertize " " 'display
                                   `(space :align-to
                                           ,(/ (- (window-width window)
                                                  displayed-width) 2)))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.