I have bound TeX-command to a key f7 to compile my document with latexmk. When I press it, and it needs to be compiled, it compiles it without asking. If not, views it, without asking (that's because of the -1 option):

(global-set-key (kbd "<f7>") 'my-run-latex)

(defun my-run-latex ()
  (if (equal current-buffer-hash
         (setq current-buffer-hash (secure-hash 'md5 (current-buffer))))
    (setq TeX-save-query nil)
    (TeX-save-document (TeX-master-file))
    (TeX-command "Latexmk" 'TeX-master-file -1)))

Now this applies to the tex master file. How can I make it apply to the file in the current buffer?

Note that I also want to have this command bound to another key for compiling the master file. So it wouldn't be a solution to set the current buffer as master file.

I tried to replace (TeX-command "Latexmk" 'TeX-master-file -1) by (TeX-command-buffer "Latexmk") which works but doesn't compile without asking. Then I tried to replace it with (TeX-command "Latexmk" 'TeX-master-file -1) with returns an error message "Wrong number of arguments buffer-file-name, 2".

  • 2
    Do you now that you can compile the document with C-c C-a (bound to TeX-command-run-all) that is akin to latexmk?
    – giordano
    Jun 5, 2016 at 13:05
  • @giordano C-c C-a is bound to nothing and Tex-command-run-all doesn't exist in my emacs. Do you mean C-c C-b for the current buffer and C-c C-c for the master file?
    – student
    Jun 11, 2016 at 10:24
  • No, I meant exactly C-c C-a. You have an outdated version of AUCTeX, upgrade to version 11.89 and you'll enjoy this new feature.
    – giordano
    Jun 11, 2016 at 11:21
  • If you want to override the default behavior you set a local var in the buffer. Then undo that to revert to default behavior. Make your function switch back and forth as there is not command that will do both automatically. See my answer below for a solution.
    – Emacs User
    Jun 11, 2016 at 14:31

1 Answer 1


How can I make it apply to the file in the current buffer?

In two ways. Either insert these lines at the bottom of the current file:

%%% Local Variables:
%%% TeX-master: "thisFileName"
%%% End:

Or set the tex-master in your my-run-latex function by adding this line:

(setq-default TeX-master "thisFileName")

Where "thisFileName" is the current buffer's file name. When you want to revert back to "master", run C-c _ as described in the AucTeX manual.

  • 1
    That doesn't work, because then I couldn't compile the "real" master file anymore.
    – student
    Jun 5, 2016 at 8:33
  • I made the answer clear now: you can switch back and forth.
    – Emacs User
    Jun 5, 2016 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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