3

I have inverse search working great with single-file documents with Zathura. Unfortunately, things break down when I try to do inverse search when viewing a region of my document, or a buffer in the case of multifile documents.

AUCTeX compiles the desired region (C-c C-r) or buffer (C-c C-b) by copying it to a file called _region_.tex, along with the preamble of the main document. However, when I try to do inverse search on the resulting PDF, rather than getting sent to the original source file, I get sent to _region_.tex. I've been burned by accidentally editing that file instead of the desired source file, and then loosing those changes.

How can I get inverse search to work in this case? Here is the "View" command AUCTeX uses.

zathura --synctex-forward 482:0:source.tex -x "emacsclient +%{line} %{input}" _region_.pdf

I am using AUCTeX 12.1.2 under emacs 25.2.2.

1 Answer 1

0

The same issue annoyed me for a while too so I finally tried to resolve it by writing the following elisp codes. The function below lets us to go back to the original buffer from the region buffer matching the position.

(defun my-LaTeX-switch-from-region-buffer-to-original ()
  (interactive)
  (let ((line (line-number-at-pos))
        (col (current-column))
        number-of-preamble-lines
        offset)
    (save-excursion
      (when (re-search-backward "!offset(\\([---0-9]+\\))" nil t)
        (setq offset (string-to-number (match-string-no-properties 1)))
        (setq number-of-preamble-lines (+ (line-number-at-pos) 1))))
    (switch-to-buffer TeX-region-orig-buffer)
    (if (buffer-narrowed-p)
        (progn
          (beginning-of-buffer)
          (forward-line (- line number-of-preamble-lines))
          (move-to-column col))
      (goto-line (+ offset line))
      (move-to-column col))))

Then this function is added to the hook that runs after returning the focus to Emacs by the PDF viewer through the inverse-search functionality:

(add-function
 :after after-focus-change-function
 (lambda ()
   (if (and (frame-focus-state)
            (string-equal
             TeX-region
             (file-name-sans-extension
          (file-name-nondirectory (buffer-file-name)))))
       (my-LaTeX-switch-from-region-buffer-to-original))))

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .