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
#'TeX-source-correlate-handle-TeX-region)
(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)))
(current-buffer))
(pdf-sync-forward-search-custom))
(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."
(interactive)
(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)
(pdf-util-assert-pdf-window)
(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)
(pdf-util-assert-pdf-window)
(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)))))
(vscroll
(pdf-util-required-vscroll pos))
(tooltip-frame-parameters
`((border-width . 0)
(internal-border-width . 0)
,@tooltip-frame-parameters))
(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)
(frame-char-height))))
(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))))
(pdf-util-tooltip-in-window
(propertize
" " 'display (propertize
"\u2192" ;;right arrow
'display '(height 2)
'face `(:foreground
"orange red"
:background
,(if (bound-and-true-p pdf-view-midnight-minor-mode)
(cdr pdf-view-midnight-colors)
"white"))))
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)
pdf-tools
(latest from MELPA), when I "View" the TeX file (C-c C-v
), thenpdf-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?