I have setup Emacs pdf-tools with auctex on ubuntu and the forward and reverse search work fine. But in the forward search (latex file to corresponding position in pdf document), it is difficult to find the precise spot in the pdf file. Is it possible to have some sort of highlight color (or box or colored dot) in the pdf file which marks the point. For example, forward search with auctex and evince draws a red box around the corresponding portion in the pdf file. Something similar with pdf-tools would be great.

Another way of posing this question: forward search only jumps to the relevant page in the pdf file, not the precise location in the pdf file. Is there way of jumping to the precise location and marking this location in the pdf file?

  • With my installation of pdf-tools (latest from MELPA), when I "View" the TeX file (C-c C-v), then pdf-tools jumps to the relevant page and points to the focused paragraph with a transient (it disappears after several seconds), orange arrow in the left margin of the PDF. Is it the case, that for you, pdf-tools doesn't even do that, or that it does, but you'd prefer more precise highlighting? – aplaice Feb 2 at 14:02
  • 1
    Thanks so much. I see it now. When I press the left mouse with ctrl (and dont release the mouse), I see the red arrow. Previously I just left clicked (rather than dont release) with ctrl and the red arrow disappeared in a fraction of second. – Vikram Feb 2 at 18:59
  • Unfortunately for a two column format pdf file, forward search only jumps to the corresponding left hand column line; even though the relevant latex text appears on the right hand column. Is there a way to fix this? – Vikram Feb 3 at 12:49

Unfortunately for a two column format pdf file, forward search only jumps to the corresponding left hand column line; even though the relevant latex text appears on the right hand column. Is there a way to fix this?

I think that this would be better as a separate question (for organisational purposes), but here's a hacky solution.

I'm going to override the definition of TeX-pdf-tools-sync-view, with an "advice", to avoid having to play around with customizing TeX-view-program-list and TeX-view-program-selection. Since it's not used elsewhere, this should be harmless. I'm not going to override pdf-util-tooltip-arrow or pdf-sync-forward-search (and will instead define pdf-util-tooltip-arrow-custom and pdf-sync-forward-search-custom, based on them), in case they're referred to somewhere else.

(defun TeX-pdf-tools-sync-view-custom ()
  "Focus the focused page/paragraph in `pdf-view-mode'.
If `TeX-source-correlate-mode' is disabled, only find and pop to
the output PDF file.  Used by default for the PDF Tools viewer
entry in `TeX-view-program-list-builtin'."
  ;; Make sure `pdf-tools' is at least in the `load-path', but the user must
  ;; take care of properly loading and installing the package.  We used to test
  ;; "(featurep 'pdf-tools)", but that doesn't play well with deferred loading.
  (unless (fboundp 'pdf-tools-install)
    (error "PDF Tools are not available"))
  (unless TeX-PDF-mode
    (error "PDF Tools only work with PDF output"))
  (add-hook 'pdf-sync-backward-redirect-functions
  (if (and TeX-source-correlate-mode
       (fboundp 'pdf-sync-forward-search))
      (with-current-buffer (or (when TeX-current-process-region-p
                     (get-file-buffer (TeX-region-file t)))
    (let ((pdf (concat file "." (TeX-output-extension))))
      (pop-to-buffer (or (find-buffer-visiting pdf)
             (find-file-noselect pdf))))))

(advice-add 'TeX-pdf-tools-sync-view :override #'TeX-pdf-tools-sync-view-custom)

(defun pdf-sync-forward-search-custom (&optional line column)
  "Display the PDF location corresponding to LINE, COLUMN."
  (cl-destructuring-bind (pdf page x1 y1 _x2 _y2)
      (pdf-sync-forward-correlate line column)
    (let ((buffer (or (find-buffer-visiting pdf)
                      (find-file-noselect pdf))))
      (with-selected-window (display-buffer
                             buffer pdf-sync-forward-display-action)
        (when page
      (pdf-view-goto-page page)
      (when (and y1 x1)
        (let ((top (* y1 (cdr (pdf-view-image-size))))
          (right (* x1 (car (pdf-view-image-size)))))
          (pdf-util-tooltip-arrow-custom (round top) (round right))))))
      (with-current-buffer buffer
        (run-hooks 'pdf-sync-forward-hook)))))

(defun pdf-util-tooltip-arrow-custom (image-top image-right &optional timeout)
  (when (floatp image-top)
    (setq image-top
          (round (* image-top (cdr (pdf-view-image-size))))))
  (when (floatp image-right)
    (setq image-right
          (round (* image-right (car (pdf-view-image-size))))))
  (let* (x-gtk-use-system-tooltips ;allow for display property in tooltip
         (dx image-right)
         (dy image-top)
         (pos (list dx dy dx (+ dy (* 2 (frame-char-height)))))
          (pdf-util-required-vscroll pos))
          `((border-width . 0)
            (internal-border-width . 0)
         (tooltip-hide-delay (or timeout 3)))
    (when vscroll
      (image-set-window-vscroll vscroll))
    (setq dy (max 0 (- dy
                       (cdr (pdf-view-image-offset))
                       (window-vscroll nil t)
    (when (overlay-get (pdf-view-current-overlay) 'before-string)
      (let* ((e (window-inside-pixel-edges))
             (xw (pdf-util-with-edges (e) e-width)))
        (cl-incf dx (/ (- xw (car (pdf-view-image-size t))) 2))))
      " " 'display (propertize
                    "\u2192" ;;right arrow
                    'display '(height 2)
                    'face `(:foreground
                            "orange red"
                            ,(if (bound-and-true-p pdf-view-midnight-minor-mode)
                                 (cdr pdf-view-midnight-colors)
     dx dy)))

You can also obviously play around with these definitions to better suit you (the original code is quite comprehensible).

Here are the original definitions of the functions, for comparison (the changes I've made are minor):

pdf-util-tooltip-arrow, pdf-sync-forward-search, TeX-pdf-tools-sync-view.

You can disable the new behaviour by removing the "advice":

(advice-remove 'TeX-pdf-tools-sync-view 'TeX-pdf-tools-sync-view-custom)
| improve this answer | |
  • Perhaps you should submit this as a patch to pdf-tools. – Qudit Mar 5 at 8:21
  • Thanks for the suggestion, but I'm pretty sure that the choice of placing the arrow in the margin is deliberate, and I doubt whether a configuration option would be accepted, as it would add a considerable amount of complexity. – aplaice Mar 5 at 9:55

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.