This question is the continuation of an earlier discussion here How can I run additonal linux commands after compiling a latex file.

I have a opened tex file called let's say filename.tex, which also has its compiled pdf file called filename.pdf.

While the current buffer is filename.tex, I want open pdf-file using (call-process "open" nil 0 nil "filename.pdf") , please note that I am on Mac. I can get the file name using buffer-file-name but I was not able to replace .tex into .pdf in order to open its pdf file.

How can I open corresponding pdf of a tex file, where its extension is replaced from .tex into .pdf?

  • file-name-with-extension. You can get the answer by simply Google "emacs filename extension" and read the manual. Jul 21, 2022 at 3:09
  • @TianshuWang I know but the given answer is much complicated than just using file-name-with-extension
    – alper
    Jul 21, 2022 at 8:25
  • 1
    auctex also has some functions like TeX-view to open the corresponding pdf. You should clarify what your needs are different. Jul 21, 2022 at 8:46
  • You are right TeX-view does the job; I am sorry for not clarify my needs clearly
    – alper
    Jul 21, 2022 at 9:15

2 Answers 2

  1. Install this emacs-lisp code
(defvar related-extns-alist
  '(("org" "pdf" "html" "odt")          ; When I open a `org' file
                                        ; with `C-u M-x browse-file',
                                        ; I really want to open any of
                                        ; the related `pdf', `html',
                                        ; or `odt' files
    ("tex" "pdf"))                      ; When I open a `tex' file
                                        ; with `C-u M-x browse-file',
                                        ; I really want to open any of
                                        ; `pdf' file

(defun browse-file (file-name &optional related-file)
  "Open FILE-NAME using the default Desktop Application.
With a prefix argument, open a related file.  See
`related-extns-alist' for more information."
    (read-file-name "File name: "
                    nil                 ; dir
                    nil                 ; default-file-name
                    t                   ; mustmatch
  (if-let* ((userp (called-interactively-p 'interactive))
            (browse-related-file current-prefix-arg)
              (file-name-extension file-name) related-extns-alist))
              (lambda (extn)
                (let ((other-file (file-name-with-extension
                                   file-name extn)))
                  (when (file-readable-p other-file)
              ((= 1 (length other-readable-extns))
               (car other-readable-extns))
              (t (completing-read "Extn: " other-readable-extns)))))
      (browse-url-default-browser (browse-url-file-url
                                    file-name actual-extension)))
    (browse-url-default-browser (browse-url-file-url file-name))
    ;; (find-file file-name)

(global-set-key (kbd "C-x C-S-f") #'browse-file) ; This is C-x C-F

  1. When you are in a .tex file, do C-u C-x C-S-F filename.tex or C-u M-x browse-file RET filename.tex. It will open the pdf file in the app your choice.

If you visiting a .org file, you will be offered a choice of which of the related files .html, .pdf, or .odt you can use.

Since you are on a Mac, browse-url-default-browser will internally use open. (I am on Linux, and it uses xdg-open)

  • (concat (file-name-sans-extension file-name) "." actual-extension) is equivalent to (file-name-with-extension file-name actual-extension) as @TianshuWang points out in a comment.
    – NickD
    Jul 21, 2022 at 4:29
  • Ok. Recipe updated. The question is not an API question, but a recipe question. In fact it is the continuation of an earlier discussion here emacs.stackexchange.com/a/72538/31220. My comment answers this specific user's needs.
    – user31220
    Jul 21, 2022 at 4:39
  • I don't think you need the (concat "." ...): just the extension is enough.
    – NickD
    Jul 21, 2022 at 5:41
  • Ok. Made the requested change.
    – user31220
    Jul 21, 2022 at 5:45
  • 1
    Try the command first ... If you re in test.tex, and if you do C-u M-x browse-file you will get a first prompt that says test.tex in minibuffer, just press RET--no need to alter it---and then it will ask for the extensions to use (if there are more than one). In tex case there is only a pdf extension. In org case there are three extensions pdf, html, odt etc. Pick the extension you want to open. If you are calling non-interactively, then you have to give the .pdf file as input. (The function behaves differently based on interactive and non-interactive use)
    – user31220
    Jul 21, 2022 at 8:28

You can use M-x TeX-view to open the .pdf file that corresponds to the .tex that you are viewing. It should be bound to C-C C-v.

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.