10

I need help using the View command in AUCTeX running in Emacs. This is essential for viewing PDFs and without it I can not make TeX documents. In my .emacs file I have the following (which was created by M-x customize-variable):

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(TeX-view-program-list (quote (("Preview" "\"open -a Preview.app %o\""))))

Now I open up a .tex document and enter C-c C-c (I have already compiled):

Emacs gives: Command: (default View)

I hit enter

Emacs gives this default command: View command: dvi2tty -q -w 132

I hit enter and Emacs says: View: done. but displays nothing in a PDF reader!

dvi2tty is intended for text only devices, and the command actually creates a new buffer in Emacs which attempts to display the PDF file (it is very messy). Anyway, if I replace dvi2tty -q -w 132 with `open -a Preview.app %o' (I am running Mac os X 10.8.4 and Preview is the name of the default PDF reader) then Preview opens the PDF!

  1. How do I change the default View command from dvi2tty -q -w 132 to open -a Preview.app %o?

All help is greatly appreciated!

9
  • 1
    Have you tried hitting C-c C-v?
    – dustin
    Commented Jun 18, 2013 at 0:31
  • I wonder if your issue has something to do with the escaped \". I checked my .emacs file, and the only difference I see (besides my pdf viewer) is that I don't have the escaped quotes.
    – Anthony
    Commented Jun 18, 2013 at 0:32
  • @dustin C-c C-v runs the View command without showing what is actually being executed. Commented Jun 18, 2013 at 0:34
  • Does open the pdf for you though?
    – dustin
    Commented Jun 18, 2013 at 0:37
  • @Anthony there are a few other variables that are all related. I did C-h v RET TeX- TAB to get a list of possible completions and then searched for view. Commented Jun 18, 2013 at 0:40

3 Answers 3

7

I got lucky and found a solution!

Follow these steps

  1. M-x customize-variable
  2. Emacs gives: Customize variable: Enter: TeX-command-list
  3. Search for View (enter C-s and then type `View)
  4. Emacs will take you to the view command where you can enter text.

It will look something like this (after Command there will be a place for text entry):

[INS] [DEL] Name: View                                                                                 
            Command: open -a Preview.app %s.pdf                                                        
            How: [Value Menu] TeX-run-command                                                          
            Create a process for NAME using COMMAND to process FILE. More                              
            Prompt: [Toggle]  on (non-nil)                                                             
            Modes: [Value Menu] All                                                                    
            Menu elements:                                                                             
            [INS] [DEL] Lisp expression: :help                                                         
            [INS] [DEL] Lisp expression: "Run Text viewer"

Change the text after Command: to whatever you need. This will automatically change your ~/.emacs file.

3
  • 3
    The last sentence is not true. Such changes are placed in the customizations.el file and invoked on start up. Commented Jul 10, 2014 at 7:19
  • 1
    @AndrewSwann When I used the above mentioned sequence of commands and finished with C-x C-s, emacs said it is saving to .emacs
    – Yair Daon
    Commented Nov 23, 2015 at 15:38
  • 1
    @YairDaon Indeed, this is dependent on your emacs implementation and set-up. I now see my remark was based on Aquamacs, which I was using at the time. The general save location is the value of custom-file, which may or may not be your init file, which in turn may or may not be .emacs. Commented Nov 23, 2015 at 16:44
5

The following examples include AUCTeX and non-AUCTeX methods for generating a *.pdf document on Windows and OSX, and displaying the *.pdf file in a viewer defined by the user.

;;;;;;;;;;;;;;;;;;;;;;;;;;;; LATEXMK and AUCTEX ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; http://tex.stackexchange.com/a/156617/26911

;; Sample `latexmkrc` for OSX that copies the *.pdf file from the `/tmp` directory
;; to the working directory:
;;    $pdflatex = 'pdflatex -file-line-error -synctex=1 %O %S && (cp "%D" "%R.pdf")';
;;    $pdf_mode = 1;
;;    $out_dir = '/tmp';"

;; Skim's displayline is used for forward search (from .tex to .pdf)
;; option -b highlights the current line
;; option -g opens Skim in the background
;; option -o open Skim in the foreground with full application focus.

;; Skim -- turn on auto-refresh by typing the following into the terminal:
;; defaults write -app Skim SKAutoReloadFileUpdate -boolean true

;; TexLive on OSX:
;; (setenv "PATH" (concat (getenv "PATH") ":/usr/texbin"))

(eval-after-load "tex" '(progn

  (setenv "PATH" (concat (getenv "PATH") ":/Users/HOME/.0.data/.0.emacs/.0.texlive/texlive/2012/bin/x86_64-darwin"))

  (add-to-list 'TeX-view-predicate-list-builtin '(output-pdf t))

  (add-to-list 'TeX-expand-list '("%(tex-file-name)" (lambda ()
    (concat "\"" (with-current-buffer TeX-command-buffer buffer-file-name) "\""))))

  (add-to-list 'TeX-expand-list '("%(pdf-file-name)" (lambda ()
    (concat
      "\"" (car (split-string (with-current-buffer TeX-command-buffer buffer-file-name) "\\.tex"))
      ".pdf" "\""))))

  (add-to-list 'TeX-expand-list '("%(line-number)" (lambda ()
    (format "%d" (line-number-at-pos)))))

  (cond

    ((eq system-type 'darwin)

      (add-to-list 'TeX-expand-list '("%(latexmkrc-osx)" (lambda () "/Users/HOME/.0.data/.0.emacs/.latexmkrc")))

      (add-to-list 'TeX-command-list '("latexmk-osx" "latexmk -r %(latexmkrc-osx) %s" TeX-run-TeX nil t))

      (add-to-list 'TeX-expand-list '("%(skim)" (lambda () "/Users/HOME/.0.data/.0.applications/Skim.app/Contents/SharedSupport/displayline")))

      (add-to-list 'TeX-command-list '("Skim" "%(skim) -g %(line-number) %(pdf-file-name) %(tex-file-name)" TeX-run-TeX nil t))

      (add-to-list 'TeX-view-program-list '("skim-viewer" "%(skim) -g %(line-number) %(pdf-file-name) %(tex-file-name)"))

      (setq TeX-view-program-selection '((output-pdf "skim-viewer"))))

    ((eq system-type 'windows-nt)

      (add-to-list 'TeX-expand-list '("%(latexmkrc-nt)" (lambda () "y:/.0.data/.0.emacs/.latexmkrc-nt")))

      (add-to-list 'TeX-command-list '("latexmk-nt" "latexmk -r %(latexmkrc-nt) %s" TeX-run-TeX nil t))

      (add-to-list 'TeX-expand-list '("%(sumatra)" (lambda () "\"c:/Program Files/SumatraPDF/SumatraPDF.exe\"")))

      (add-to-list 'TeX-command-list '("SumatraPDF" "%(sumatra) -reuse-instance -forward-search %(tex-file-name) %(line-number) %(pdf-file-name)" TeX-run-TeX nil t))

      (add-to-list 'TeX-view-program-list '("sumatra-viewer" "%(sumatra) -reuse-instance -forward-search %(tex-file-name) %(line-number) %(pdf-file-name)")) 

      (setq TeX-view-program-selection '((output-pdf "sumatra-viewer")))))  ))

(defun auctex-latexmk ()
"Compile, view *.pdf, and clean (maybe)."
(interactive)
  (require 'tex)
  (require 'latex)
  (let ((TeX-PDF-mode t)
        (TeX-source-correlate-mode t)
        (TeX-source-correlate-method 'synctex) 
        (TeX-source-correlate-start-server nil)
        (TeX-clean-confirm t))
    (when (buffer-modified-p)
      (save-buffer))
    (set-process-sentinel
      (TeX-command "LaTeX" 'TeX-master-file)
        (lambda (p e)
          (when (not (= 0 (process-exit-status p)))
            (TeX-next-error t) )
          (when (= 0 (process-exit-status p))
            (delete-other-windows)
            (TeX-command "View" 'TeX-active-master 0)
            ;; `set-process-sentinel` cannot be used on Windows XP for post-view cleanup,
            ;; because Emacs treats SumatraPDF as an active process until SumatraPDF exits.
            (let ((major-mode 'latex-mode))
              (with-current-buffer TeX-command-buffer
                (TeX-command "Clean" 'TeX-master-file))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; LATEXMK START-PROCESS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun combined-latexmk-function ()
(interactive)
  (cond
    ((eq system-type 'darwin)
      (latexmk))
    ((eq system-type 'windows-nt)
      (xp-latexmk) )))

(defun xp-latexmk ()
".latexmkrc contains the following entries:
  $pdflatex = 'pdflatex -file-line-error -synctex=1 %O %S';
  $pdf_mode = 1;
  $recorder = 0;
  $clean_ext = 'synctex.gz synctex.gz(busy) aux fdb_latexmk log';"
(interactive)
  (lexical-let* (
      (latexmk-tex-file buffer-file-name)
      (latexmk-base-file (car (split-string buffer-file-name "\\.tex")))
      (latexmk-w32-tex-file (concat "\"" buffer-file-name "\""))
      (latexmk-w32-pdf-file (concat "\"" latexmk-base-file ".pdf" "\""))
      (latexmk-line (format "%d" (line-number-at-pos)))
      (latexmk-sumatra "c:/Program Files/SumatraPDF/SumatraPDF.exe")
      (latexmk-w32-document (concat
        "-reuse-instance"
          " "
        "-forward-search"
          " "
        latexmk-w32-tex-file
          " "
        latexmk-line
          " "
        latexmk-w32-pdf-file))
      (latexmk-tex-output (concat "*" (file-name-nondirectory buffer-file-name) "*") )
      (latexmk-pl "c:/texlive/2013/bin/win32/latexmk.exe")
      (latexmk-latexmkrc "y:/.0.data/.0.emacs/.latexmkrc-nt") )
    (if (buffer-modified-p)
      (save-buffer))
    (delete-other-windows)
    (set-window-buffer (split-window-horizontally) (get-buffer-create latexmk-tex-output))
    (with-current-buffer latexmk-tex-output (erase-buffer))
    ;; `-reuse-instance` seems to be working very nicely, so no need to kill prior instance.
    ;; (start-process "tskill" nil "c:/WINDOWS/system32/tskill.exe" "SumatraPDF")
    (set-process-sentinel 
      (start-process "deep-clean" nil latexmk-pl "-C" "-r" latexmk-latexmkrc latexmk-tex-file)
      (lambda (p e) (when (= 0 (process-exit-status p))
        (set-process-sentinel 
          (start-process "compile" latexmk-tex-output latexmk-pl "-r" latexmk-latexmkrc latexmk-tex-file)
          (lambda (p e) (when (= 0 (process-exit-status p))
            (when (get-buffer-process (get-buffer latexmk-tex-output))
              (process-kill-without-query (get-buffer-process
                (get-buffer latexmk-tex-output))))
            (kill-buffer latexmk-tex-output)
            (delete-other-windows)
            (switch-to-buffer (get-file-buffer latexmk-tex-file))
            (w32-shell-execute "open" latexmk-sumatra latexmk-w32-document)
            (sit-for 1)
            (start-process "clean" nil latexmk-pl "-c" "-r" latexmk-latexmkrc latexmk-tex-file)))))))))

(defun latexmk-process-filter (proc string)
  (let ((inhibit-read-only t))
    (with-current-buffer (messages-buffer)
      (goto-char (point-max))
      (when (not (bolp))
        (insert "\n"))
      (insert string)
      (when (not (bolp))
        (insert "\n")))
    (when (string-match "?" string)
      (display-buffer (messages-buffer))
      (with-current-buffer (messages-buffer)
        (goto-char (point-max))
        (walk-windows
          (lambda (window)
            (when (string-equal (buffer-name (window-buffer window)) "*Messages*")
              (set-window-point window (point-max))))
          nil t)))))

(defun latexmk ()
  ".latexmkrc contains the following entries (WITHOUT the four backslashes):
  $pdflatex = 'pdflatex -file-line-error -synctex=1 %O %S && (cp \"%D\" \"%R.pdf\")';
  $pdf_mode = 1;
  $out_dir = '/tmp';"
(interactive)
  (lexical-let* (
      (latexmk-tex-file buffer-file-name)
      (latexmk-pdf-file (concat "/tmp/"
        (car (split-string (file-name-nondirectory buffer-file-name) "\\.tex")) ".pdf"))
      (latexmk-line (format "%d" (line-number-at-pos)))
      (latexmk-skim "/Users/HOME/.0.data/.0.applications/Skim.app/Contents/SharedSupport/displayline")
      (latexmk-pl "/usr/local/texlive/2012/texmf-dist/scripts/latexmk/latexmk.pl")
      (latexmk-latexmkrc "/Users/HOME/.0.data/.0.emacs/.latexmkrc")
      (latexmk-close-skim-window (concat
        "set myWindowTitle to \"" (file-name-nondirectory latexmk-pdf-file) "\"\n"
        "tell application \"System Events\"\n"
        "tell application \"Skim\" to close (every window whose name contains myWindowTitle)\n"
        "end tell")) )
    (when (buffer-modified-p)
      (save-buffer))
    (when (not (one-window-p t))
      (delete-other-windows))
    (set-process-sentinel
      (start-process "close-window" nil "osascript" "-e" latexmk-close-skim-window)
      (lambda (p e) (when (= 0 (process-exit-status p))
        (set-process-sentinel
          (start-process "deep-clean" nil latexmk-pl "-C" "-r" latexmk-latexmkrc latexmk-tex-file)
          (lambda (p e) (when (= 0 (process-exit-status p))
            (when (get-process "compile")
              (delete-process (get-process "compile")))
            (start-process "compile" nil latexmk-pl "-r" latexmk-latexmkrc latexmk-tex-file)
            (set-process-filter (get-process "compile") 'latexmk-process-filter)
            (set-process-sentinel
              (get-process "compile")
              (lambda (p e) (when (= 0 (process-exit-status p))
                (set-process-sentinel
                  (start-process "displayline" nil latexmk-skim latexmk-line latexmk-pdf-file latexmk-tex-file)
                  (lambda (p e) (when (= 0 (process-exit-status p))
                    (when (not (one-window-p t))
                      (delete-other-windows))))))))))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
  • Please don't keep an edit list, simply update your question to reflect the best content. An edit list isn't very readable and there is no need for it because there is an automatic history for every post. Also note, that if you edit your own answer to ofter (15 times I think) it will turn into a Community Wiki automatically. Then everyone can edit it. Commented Feb 5, 2015 at 18:04
2

I don't recall where I found the following code, but: To make auctex call pdflatex by default and to preview pdf files using okular, I put the following at the end of my ~/.xemacs/init.el file:

(setq TeX-PDF-mode t)

(defun pdfevince ()
   (add-to-list 'TeX-output-view-style
                 '("^pdf$" "." "evince %o %(outpage)")))

(defun pdfokular ()
   (add-to-list 'TeX-output-view-style
                 '("^pdf$" "." "okular %o %(outpage)")))

;(add-hook  'LaTeX-mode-hook  'pdfevince  t) ; AUCTeX LaTeX mode
(add-hook  'LaTeX-mode-hook  'pdfokular  t) ; AUCTeX LaTeX mode

If you prefer evince to okular, uncomment the second to last line and comment out the last line. That's for xemacs; for emacs, I suppose you put it into your .emacs file.

You must log in to answer this question.

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