0

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?

alper
  • 1,238
  • 11
  • 30
  • [`file-name-with-extension`](https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Name-Components.html#index-file_002dname_002dwith_002dextension). You can get the answer by simply Google "emacs filename extension" and read the manual. – Tianshu Wang Jul 21 '22 at 03:09
  • @TianshuWang I know but the given answer is much complicated than just using `file-name-with-extension` – alper Jul 21 '22 at 08:25
  • 1
    `auctex` also has some functions like `TeX-view` to open the corresponding pdf. You should clarify what your needs are different. – Tianshu Wang Jul 21 '22 at 08:46
  • You are right `TeX-view` does the job; I am sorry for not clarify my needs clearly – alper Jul 21 '22 at 09:15

2 Answers2

1
  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."
  (interactive
   (list
    (read-file-name "File name: "
                    nil                 ; dir
                    nil                 ; default-file-name
                    t                   ; mustmatch
                    (buffer-file-name))
    current-prefix-arg))
  (if-let* ((userp (called-interactively-p 'interactive))
            (browse-related-file current-prefix-arg)
            (related-extns
             (assoc-default
              (file-name-extension file-name) related-extns-alist))
            (other-readable-extns
             (seq-filter
              (lambda (extn)
                (let ((other-file (file-name-with-extension
                                   file-name extn)))
                  (when (file-readable-p other-file)
                    other-file)))
              related-extns))
            (actual-extension
             (cond
              ((= 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-with-extension
                                    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 '22 at 04: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 https://emacs.stackexchange.com/a/72538/31220. My comment answers this specific user's needs. –  Jul 21 '22 at 04:39
  • I don't think you need the `(concat "." ...)`: just the extension is enough. – NickD Jul 21 '22 at 05:41
  • Ok. Made the requested change. –  Jul 21 '22 at 05:45
  • @whitetrillium Do I need the provide file name (like `filename.tex`) as input or could it also be automatically taken from the opened buffer without entering it manually? – alper Jul 21 '22 at 08:26
  • 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) –  Jul 21 '22 at 08:28
  • You can also change the file-name at the minibuffer prompt (just like as you do for `C-x C-f`) –  Jul 21 '22 at 08:33
  • @whitetrillium thank you I appreciated your well explained solution – alper Jul 21 '22 at 09:22
0

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.

Blender
  • 25
  • 6