It seems that TeX-command-run-all shifts focus to the pdf window, which is quite annoying. Is there a way to prevent that?

  • I think that this is not the standard behaviour with pdf-tools. At least on my system with emacs 27.0.5, latest AUCTeX and pdf-tools from melpa, the focus is kept in the window with the tex file. I have tested it with emacs -Q and minimal config.
    – andrej
    May 20, 2019 at 16:37

3 Answers 3


I use xdotool to shift focus back to Emacs after Tex-command-run-all. Works on an X system (Linux/Unix) with xdotool installed. I use Okular as pdf-viewer and the following settings:

(setq TeX-view-program-selection '((output-pdf "Okular")))

(setq TeX-view-program-list 
'(("Okular" "emacswinno=`xdotool getwindowfocus` \; okular --unique %o\#src:%n`pwd`/./%b \; sleep 0.1\; xdotool windowactivate $emacswinno")))

The xdotool solution will work with any pdf viewer, but it is actually an overkill if you use Okular. I just realized you can call Okular with the option --noraise to prevent it from taking focus. So if you use Okular, the following setting will work:

(setq TeX-view-program-list
  '(("Okular" ("okular --noraise --unique %o"  (mode-io-correlate "#src:%n%a")) "okular")))
  • If that is necessary, I feel there is something wrong with AUCTeX.
    – Toothrot
    May 20, 2019 at 15:58
  • There is a user option: TeX-view-evince-keep-focus When this option is non-nil and the viewer is compatible with evince, the focus is pulled back to Emacs immediately after the viewer is invoked or refreshed from within AUCTeX. from here gnu.org/software/auctex/manual/auctex/Starting-Viewers.html This option as well as xdotools should not affect pdf-tools.
    – andrej
    May 20, 2019 at 16:49
  • The option TeX-view-evince-keep-focus doesn't work with Okular unfortunately.
    – StarBug
    May 21, 2019 at 9:05


(add-hook 'TeX-after-compilation-finished-functions

do the trick ?


I had the exact same problem. It turned out that adding the -g option to the Skim command invoked in TeX-view-program-list solved it for me for Skim+MacOS. Maybe there are similar options for other PDF viewers.

;; DOOM-EMACS snippet
(after! tex
  (dolist (viewer (reverse +latex-viewers))
  (pcase viewer
          (and IS-MAC
               (file-exists-p! (or "/Applications/Skim.app"
       (add-to-list 'TeX-view-program-selection '(output-pdf "Skim"))
       (add-to-list 'TeX-view-program-list
                    (list "Skim" (format "%s/Contents/SharedSupport/displayline -r -b -g %%n %%o %%b"

Note: I edited the code from https://github.com/doomemacs/doomemacs/blob/master/modules/lang/latex/%2Bviewers.el to ensure that it only affects Skim if Skim was selected in the doom config.

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.