Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I use Emacs and AucTeX as my writing environment. Pleasantly, I can pick an engine (PDFTeX, LuaTeX, XeTeX) and have it execute as default for all my compilations.

However, I have one project where I need a different engine than what I use as a default. I tried setting this the Emacs way, with a local variable

%%% TeX-engine: "default"

line along with the lines that specify master file for the project, but this seems to not set the appropriate variable, and Emacs locks up completely if I try to compile without a decent value set for TeX-engine.

Is there some way to signal to XeTeX that the incoming file should be compiled with PDFTeX instead? Or some good way to signal to Emacs that this particular file should use a different program for the compilation?

share|improve this question
    
I'm doing this all the time and it works out of the box. Could you post a minimal example? –  Philipp Oct 7 '10 at 12:06
4  
But try to remove the quotes, the engine is a symbol, not a string. –  Philipp Oct 7 '10 at 12:07
    
Remove the quotes was the key. THANK YOU Philipp!! –  Mikael Vejdemo-Johansson Oct 7 '10 at 15:02
    
If you could post this as an answer, I'll accept it. –  Mikael Vejdemo-Johansson Oct 7 '10 at 15:02
add comment

2 Answers

I doubt that this is going to win me any friends either in the Emacs community or the TeX one, but I have a hack that achieves this. The only caveat (for your situation) is that I don't use AucTeX so have no idea how it would work with that. When I edit a .tex, I load in the file below. This allows me to set some options depending on what output/engine I want to use.

There's a few bits of junk in there as well, but most of it is focussed on this issue. It works by rewriting the commands tex-file and tex-view-file so that they can be modified according to options set by choosing an output type (which is actually more refined than just the output type) for the file.

So I just do M-x set-tex-output-type xe and I get my file compiled using xelatex.

;(server-start)
;(load "xdvi-search.el")

(setq latex-block-names '("theorem" "corollary" "proof" "lemma" "defn" "thm"))
;(setq tex-dvi-view-command
;          (if (eq window-system 'x) "xdvi" nil))
;(setq tex-dvi-print-command "dvips -Pmirkti *")
(setq tex-alt-dvi-print-command "dvips * -ta4 -o")

(defcustom tex-output-type "pdf"
  "*Default output type for TeX jobs."
  :type '(radio (const :tag "Postscript" "ps")
        (const :tag "Postscript (rv)" "psrv")
        (const :tag "PDF" "pdf")
        (const :tag "PDF (rv)" "pdfrv")
        (const :tag "Dvi" "dvi")
        (const :tag "Dvi (draft)" "dvid")
        (const :tag "XeLaTeX" "xe")
        (const :tab "LuaLaTeX" "lua")
        (const :tag "HTML" "html")
        (const :tag "XHTML" "xhtml")
        (const :tag "MathML" "xml")
        (const :tag "OpenOffice" "sxw")
        (const :tag "maruku" "maruku"))
  :group 'tex-run)

(defcustom tex-ext-alist
  '(("ps" . "ps")
    ("psrv" . "ps")
    ("pdf" . "pdf")
    ("pdfrv" . "pdf")
    ("dvi" . "dvi")
    ("dvid" . "dvi")
    ("xe" . "pdf")
    ("lua" . "pdf")
    ("html" . "html")
    ("xhtml" . "xhtml")
    ("xml" . "xml")
    ("sxw" . "sxw")
    ("maruku" . "xhtml"))
  "*alist of eventual file extensions")

(defcustom tex-command-alist
  '(("pdf" . "pdflatex")
    ("pdfrv" . "pdflatex")
    ("ps" . "latex")
    ("psrv" . "latex")
    ("dvi" . "latex")
    ("dvid" . "latex")
    ("xe" . "xelatex")
    ("lua" . "lualatex")
    ("html" . "mk4ht")
    ("xhtml" . "mk4ht")
    ("xml" . "mk4ht")
    ("sxw" . "mk4ht")
    ("maruku" . "maruku")
    )
  "*alist for processing file into various types of output")

(defcustom tex-start-options-alist
  '(("pdf" . "\\\\nonstopmode\\\\input")
    ("pdfrv" . "\\\\nonstopmode\\\\input")
    ("ps" . "\\\\nonstopmode\\\\input")
    ("psrv" . "\\\\nonstopmode\\\\input")
    ("dvi" . "\\\\nonstopmode\\\\input")
    ("dvid" . "-src-specials \\\\nonstopmode\\\\input")
    ("xe" . "\\\\nonstopmode\\\\input")
    ("lua" . "\\\\nonstopmode\\\\input")
    ("html" . "htlatex")
    ("xhtml" . "xhlatex")
    ("xml" . "mzlatex")
    ("sxw" . "oolatex")
    ("maruku" . "-m itex2mml")
    )
  "*alist for options that preceed the filename")


(defcustom tex-end-options-alist
  '(("pdf" . "")
    ("pdfrv" . "")
    ("ps" . "")
    ("psrv" . "")
    ("dvi" . "")
    ("dvid" . "")
    ("xe" . "")
    ("lua" . "")
    ("html" . "\"html,3,frames\"")
    ("xhtml" . "\"html,3,frames\"")
    ("xml" . "\"html,3,frames\"")
    ("sxw" . "\"html,3,frames\"")
    ("maruku" . "")
    )
  "*alist for options that postceed the filename")

(defcustom tex-post-command-alist
  '(("pdf" . "")
    ("pdfrv" . "")
    ("ps" . "dvips * -ta4 -o 2> /dev/null")
    ("psrv" . "dvips * -ta4 -o 2> /dev/null")
    ("dvi" . "")
    ("dvid" . "")
    ("xe" . "")
    ("lua" . "")
    ("html" . "")
    ("xhtml" . "")
    ("xml" . "")
    ("sxw" . "")
    ("maruku" . "")
    )
  "*alist for post-processing commands")

(defcustom tex-mid-ext-alist
  '(("ps" . "dvi")
    ("psrv" . "dvi")
    ("pdf" . "")
    ("pdfrv" . "")
    ("dvi" . "")
    ("dvid" . "")
    ("xe" . "")
    ("lua" . "")
    ("html" . "")
    ("xhtml" . "")
    ("xml" . "")
    ("sxw" . "")
    ("maruku" . "")
    )
  "*alist of mid-point file extensions if we do a two-stage process")


(defcustom tex-view-commands-alist
;  (cond 
;    ((eq window-system 'x)
     '(("pdf" .  "xpdf")
       ("pdfrv" .  "xpdf")
     ("ps" .  "gv -ad=~/.gv_spartan_right.dat")
     ("psrv" .  "gv -ad=~/.gv_spartan_right_rv.dat")
     ("dvi" . "xdvi")
     ("dvid" . "xdvi")
     ("xe" .  "xpdf")
     ("lua" .  "xpdf")
     ("html" . "firefox")
     ("xhtml". "firefox")
     ("xml". "firefox")
     ("sxw". "ooffice2")
     ("maruku" . "firefox")
     )
;    (t
;     '(("pdf" . "pdftotext * | cat -s")
;     ("ps" . "ps2ascii * | cat -s")
;     ("dvi" . "dvips * | ps2ascii | cat -s")
;     ("html". "lynx")
;     ("xhtml". "lynx")
;     ("xml". "lynx")
;     ("sxw". "false"))
;     )))
  "*alist for viewing the various types of output")

;(defcustom tex-dvi-to-ps-command
;  "dvips * -ta4 -o 2> /dev/null"
;  "*Command for conversion from dvi to ps."
;  :type 'string
;  :group 'tex-run)

(defun tex-font-lock-type-alist
  '(("pdf" . "")
    ("pdfrv" . "")
    ("ps" . "")
    ("psrv" . "")
    ("dvi" . "")
    ("dvid" . "")
    ("xe" . "")
    ("lua" . "")
    ("html" . "")
    ("xhtml" . "")
    ("xml" . "")
    ("sxw" . "")
    ("maruku" . "markdown-mode-font-lock-keywords")
    )
  "*alist for modifying font-lock defaults")


(modify-syntax-entry ?\\ "." tex-mode-syntax-table)

(defun set-tex-main-file ()
  "Set 'tex-main-file' to something appropriate from the current buffer."
  (interactive)
  (make-variable-buffer-local 'tex-main-file)
  (setq tex-main-file (expand-file-name (car (find-file-read-args
                          "(La)TeX main file name for buffer: " nil))))
)

(defun set-tex-output-type ()
  "Set the 'tex-output-type'."
  (interactive)
   (setq newtype
      (completing-read "TeX Output Type: " tex-ext-alist nil t)
      )
  (if (not (equal newtype "")) 
      (setq tex-output-type newtype)
    )
  )

(defun template ()
  "Insert template.tex at the appropriate place."
  (interactive)
  (insert-file-contents "~/tex/papers/useful/template.tex")
)

(defun end-document ()
  "Insert end-of-document commands at the point."
  (interactive)
  (insert "\\bibliography{arxiv,articles,books,misc}\n\n\\end{document}\n\n")
)

(defun find-end-document ()
  "Skips to the end of the current document."
  (interactive)
;  (goto-char (point-min))
  (search-forward "\\end{document}")
)

(defun my-tex-file ()
  "Modify \\[tex-file] to take into account the desired output type."
  (interactive)
  (let ((tex-command (concat
              (cdr (assoc tex-output-type tex-command-alist))
              " "
              (cdr (assoc tex-output-type tex-start-options-alist))
              " \* "
              (cdr (assoc tex-output-type tex-end-options-alist)))))
  (tex-file)
  )
  (if (< 0 (length (cdr (assoc tex-output-type tex-post-command-alist))))
      (let ((tex-mid-file (tex-append tex-print-file (concat "." (cdr (assoc tex-output-type tex-mid-ext-alist))))))
    (if (not (file-exists-p tex-mid-file))
        (error "No appropriate file could be found")
      (tex-send-command (cdr (assoc tex-output-type tex-post-command-alist)) tex-mid-file)
      )
    )
    )
  )


(defun my-tex-view ()
  "Modify \\[tex-view] to take into account the desired output type."
  (interactive)
  (let* ((tex-suffix (concat "." (cdr (assoc tex-output-type tex-ext-alist))))
     (tex-view-command (cdr (assoc tex-output-type tex-view-commands-alist)))
     (view-file-name (tex-append tex-print-file tex-suffix))
        test-name)
    (if (and (not (equal (current-buffer) tex-last-buffer-texed))
             (buffer-file-name)
             ;; Check that this buffer's printed file is up to date.
             (file-newer-than-file-p
              (setq test-name (tex-append (buffer-file-name) (concat "." (cdr (assoc tex-output-type tex-ext-alist)))))
              (buffer-file-name)))
        (setq view-file-name test-name))
    (if (not (file-exists-p view-file-name))
        (error "No appropriate file could be found to view.")
      (if (tex-shell-running)
          (tex-kill-job)
        (tex-start-shell))
      (tex-send-command
       tex-view-command view-file-name
       t))))

(define-key tex-mode-map "\C-c\C-n" 'find-end-document)
(define-key tex-mode-map "\C-c\C-d" 'end-document)

(define-key tex-mode-map [remap tex-file] 'my-tex-file)
(define-key tex-mode-map [remap tex-view] 'my-tex-view)

(define-key tex-shell-map [remap tex-file] 'my-tex-file)
(define-key tex-shell-map [remap tex-view] 'my-tex-view)

(defadvice server-visit-files (around save-buffers last activate)
    "Try to emulate gnuclient behaviour with emacsclient.
Works only for visiting one buffer at a time."
    (let* ((filen (car (car (ad-get-arg 0))))
           (buf (get-file-buffer filen))
           (this-buf-modified-p nil))
      ;;; the following is copied from server-visit-files, with
      ;;; a modification for the `verify-visited-file-modtime' test
      (if (and buf (set-buffer buf))
          (if (file-exists-p filen)
              ;;; if the file has changed on disk, reload it
              ;;; using `find-file-noselect'
              (if (not (verify-visited-file-modtime buf))
                  (progn
                    (find-file-noselect filen)
                    ;;; if user answered `no', reset modtime anyway
                    ;;; so that server-visit-files doesn't realize
                    ;;; the difference:
                    (set-visited-file-modtime)))
            ;;; if file exists no longer, we let server-visit-files
            ;;; deal with that
            t)
        (setq buf (find-file-noselect filen)))
      (setq this-buf-modified-p (buffer-modified-p buf))
      (set-buffer buf)
      (set-buffer-modified-p nil)
      ad-do-it
      (set-buffer-modified-p this-buf-modified-p)))


(require 'markdown-mode)

(defun markdown-font-locks ()
  "Set the font lock to that of markdown mode."
  (interactive)
  (set (make-local-variable 'font-lock-defaults)
       '(markdown-mode-font-lock-keywords))
  (set (make-local-variable 'font-lock-multiline) t)
  (setq font-lock-mode-major-mode nil)
  (font-lock-fontify-buffer)
  )
share|improve this answer
add comment
up vote 6 down vote accepted

As Philipp pointed out in a comment, but never posted as an answer, my problem was that TeX-engine expects a symbol not a string. Thus, instead of what I wrote, I needed to write

%%% TeX-engine: default

to make it all work.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.