0

Question:

I'm running auctex and pdf-tools to view pdf's within emacs. My problem is that I'd like to scale the pdf 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
        (my/pdf-view-keys)
      ;; dark mode
        (setq pdf-view-midnight-colors '("#f6f3e8" . "#242424"))
        (pdf-view-midnight-minor-mode)
      ;; 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)
        (fit-window-to-buffer)
        ))

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 test-after ()
  (fit-window-to-buffer)
  (fit-window-to-buffer)
  )

(add-hook 'pdf-view-after-change-page-hook 
      'test-after)

The fit-window-to-buffer call has to be made several times, but it doesn't seem to completely fit correctly. After scrolling through to the next page, however, it calls it again and scales it to the right size.

Thanks to NickD for the suggestion.

2
  • 1
    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 '20 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 '20 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.