This is an extension of my previous question auctex and pdf-tools in 2 separate frames which was nicely answered by Tobias. I use auctex in one frame and pdftools in another frame. Forward and inverse search work perfectly between the two frames. The question I have is this: how to make forward and inverse search work when I split each frame into 2 buffers? That is, I have the latex source of a file in one frame split into 2 buffers (with C-x 2); lets call these buffers latex1 and latex2. Similarly I have the corresponding pdf frame split into two buffers - lets call them pdf1 and pdf2. My questions is: how to setup forward and inverse search between latex1 and pdf1, and also between latex2 and pdf2? This helps working on two parts of a large latex document.I need the above setup of two separate frames, one frame for latex (with 2 buffers) and one frame for pdf (with 2 buffers), due to eyesight problems.

  • The question seems unclear. Is this about search? Is it about switching focus among frames? What's the question? – Drew Feb 11 at 16:18
  • I have revised the question and context. Hopefully it is understandable now. – Vikram Feb 11 at 22:52
  • Just to clarify, do latex1 and latex2 point to same long latex file? If yes, then are you asking how to keep searches in latex1 buffer in sync with pdf1 buffer while separately keeping independent searches in latex2 buffer in sync with pdf2 buffer? Is the issue that searches in latex1+pdf1 effect the display of searches in latex2+pdf2? – Melioratus Feb 21 at 14:53

The following code generalizes the answer to your other question about showing the pdf in another frame.

It requires a bit more interaction from your side.

I propose you set up your frame and window layout beforehand - two frames with two windows each. The windows do not need to show the right buffers yet. You certainly need the two windows with the LaTeX buffer since you need some starting point.

Select one of the LaTeX windows and call TeX-View, e.g., via C-c C-a.

The first time you are prompted with Associate window. Click in the pdf-window which you want to associate with the LaTeX window.

Do the same for the other TeX window.

Afterwards forwards and inverse search should just work as you want it.

(defun framesMenus-read-window (&optional prompt)
  "Prompt with PROMPT and read a window."
    (let ((frame (selected-frame))
          window ev)
      (while (progn
               (setq ev (read-event prompt))
               (eq (event-basic-type ev) 'switch-frame))
        (setq frame (cadr ev)))
      (with-selected-frame frame
          (setq window (posn-window (event-start ev))))

(defvar-local framesMenus-associated-other-window nil
  "Alist of other windows associated with
selected windows for a buffer.")

(defun framesMenus-associated-other-window (&optional prompt force mutual)
  "Return the other window associated with the current buffer.
PROMPT is forwarded to `framesMenus-read-window' when called.
When FORCE is non-nil always prompt for the associated window.
If MUTUAL is non-nil also associate other window with this one."
  (let* ((this-window (selected-window))
         (assoc-window (assoc this-window framesMenus-associated-other-window))
         (other-window (cdr assoc-window)))
         (null force)
         (window-live-p other-window)
       (cons (selected-window)
             (setq other-window (framesMenus-read-window prompt)))
       :key #'car
    (when mutual
      (with-selected-window other-window
         (cons other-window
         :key #'car)))

(defun framesMenus-display-in-associated-window (buffer _action-list)
  "Display BUFFER in associated window."
  (let ((window (framesMenus-associated-other-window "Associate window." nil t)))
    (with-selected-window window
      (switch-to-buffer buffer))

(defun framesMenus-display-buffer-use-associated-window (fun &rest args)
  "Use `display-buffer-use-some-frame' as `display-buffer-overriding-action'.
Then run FUN with ARGS."
  (let ((display-buffer-overriding-action '(framesMenus-display-in-associated-window)))
    (apply fun args)))

(advice-add 'TeX-pdf-tools-sync-view :around #'framesMenus-display-buffer-use-associated-window)
(advice-add 'pdf-sync-backward-search-mouse :around #'framesMenus-display-buffer-use-associated-window)
| improve this answer | |
  • Thank you Tobias. It works very nicely! You sir, are a genius!! – Vikram Feb 12 at 10:50

Your Answer

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

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