Emacs AUCTeX View command

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.


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))))))))))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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.