How can I have Emacs call the Latex command every time after I save my .tex file?


A solution: while playing with ELisp and looking around, I added this to .emacs and it seems to work quite well (just don't press C-x C-s too much frequently, say every 5 secs):

(defun run-latex ()
    (let ((process (TeX-active-process))) (if process (delete-process process)))
    (let ((TeX-save-query nil)) (TeX-save-document ""))
    (TeX-command-menu "LaTeX"))
(add-hook 'LaTeX-mode-hook (lambda () (local-set-key (kbd "C-x C-s") #'run-latex)))

The advantage of this solution over latexmk is that it is quicker to put working because I don't have to customize anything (and risking making mistakes). Also, in case I want to remove it, I only have to comment the last line (add-hook ... in your .emacs.

Anyway, latexmk seems also another interesting path I will try when I have more time.

Thanks for your answers and help.

  • I don't see why you can't just call the latex command which in turn causes a save of the file. Isn't that the same thing? – Vivi Jul 27 '12 at 12:59
  • In that case you have to always type "y" when asked for to save the file before the latex command runs. But, your solution would be good if I knew how to configure Emacs to save the file every time before the latex command is run. Do you know how? – mljrg Jul 27 '12 at 13:29
  • I hit "save" quite frequently when I'm typing my documents (and of course don't want to run LaTeX each time). Better safe than sorry. :) – egreg Jul 27 '12 at 14:02
  • 1
    @mljrg If you add (setq TeX-save-query nil) to your .emacs file then C-c C-c will not ask for confirmation before saving the file. It will save automatically on compile. – Vivi Jul 27 '12 at 19:45

Please search this forum for latexmk, which does exactly what you ask.

EDIT: See here: Compile using latexmk in emacs

There are more threads in this forum, if you search for 'latexmk' and 'emacs'.

| improve this answer | |
  • That seems to be one way, but I have everything already configured in Emacs (that is, I have source links, direct and inverse search working, etc), so I would prefer a simple solution inside Emacs. Is there one? – mljrg Jul 27 '12 at 12:21
  • 1
    @mljrg: Run Latexmk in preview-continuous mode instead of LaTeX. If you are using AUCTeX it’s not difficult to add a new command to the list inspected by C-c C-c. – mhp Jul 27 '12 at 14:41
  • @mhp But in that case I still have to do C-x C-s then C-c C-cdon't I? I just wanted to do C-x C-sand have the file saved and Latex run. – mljrg Jul 27 '12 at 15:16
  • @mljrg: No, once you’ve started Latexmk in preview-continuous mode it recompiles your document every time you press C-x C-s. – mhp Jul 27 '12 at 15:47
  • @mhp Can you tell me how to do that? I changed the entry AUCTeX\Tex Command Group\Latex Command to "latexmk -pvc" but it did not worked. Thanks – mljrg Jul 27 '12 at 16:15

Since my elisp skills are rather poor, I can only suggest a general Idea. Using TeX-sace-query and/or TeX-sace-document you can re-bind C-x C-s to first save the file(s) and then run your TeX-run-TeX with the appropriate parameter.

As a side remark, I have the following in my .emacs:

(require 'tex-buf)
(defun run-latexmk ()
  (let ((TeX-save-query nil)
        (TeX-process-asynchronous nil)
        (master-file (TeX-master-file)))
    (TeX-save-document "")
    (TeX-run-TeX "latexmk"
         (TeX-command-expand "latexmk -pdf %t" 'TeX-master-file)
    (if (plist-get TeX-error-report-switches (intern master-file))
        (TeX-next-error t)
      (minibuffer-message "latexmk done"))))

(add-hook 'LaTeX-mode-hook
          (lambda () (local-set-key (kbd "C-0") #'run-latexmk)))

which binds C-0 to SAVE and then run latexmk.

| improve this answer | |
  • I have added this to .emacs (defun my-run-latex () (interactive) (TeX-command "LaTeX" 'TeX-master-file -1)) (add-hook 'after-save-hook 'my-run-latex), it is working, but I am getting an error "Wrong type argument: stringp, nil" and I don't know why. Also I would like to check if the file extension is .tex, and only in this case the hook would call latex on the file. – mljrg Jul 27 '12 at 12:52
  • Your solution runs synchronously and blocks if I save the file two times in a row. – mljrg Jul 27 '12 at 16:35
  • Your solution was helpful to reach mine above. Thanks! – mljrg Jul 27 '12 at 16:56
  • @mljrg: Usually I'm using the "preview" option of latexmk, and just save the file. BTW, with latexmk you hardly have to customize anything... – Dror Jul 29 '12 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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