简体   繁体   中英

emacs auctex commandto compile and view

I use EMACS/AucTeX. In order to compile I do Cc Cc, then it asks

"Command: (default LaTeX)"

I press RET and it is compiled. To view the compiled document I do Cc Cv.

I would like to have a simple shortcut, like pressing F1 or some other key combination to compile and then view the document. There is any simple command/function that can be inserted in .emacs to do that?

Thanks Pietro

Cc Ca(TeX-command-run-all)将在AUCTeX 11.89中完成工作。

I don't think there's anything that will work out of the box, and the naive approach of just calling the two commands in sequence won't work because you need the compilation process to finish before you can view the output.

Here's a quick and dirty solution that might work for you:

(defun my/TeX-view-once (doc)
  "View TeX output and clean up after `my/TeX-compile-and-view'.

Call `TeX-view' to display TeX output, and remove this function
from `TeX-after-TeX-LaTeX-command-finished-hook', where it may
have been placed by `my/TeX-compile-and-view'."
  (TeX-view)
  (remove-hook 'TeX-after-TeX-LaTeX-command-finished-hook #'my/TeX-view-once))


(defun my/TeX-compile-and-view ()
  "Compile current master file using LaTeX then view output.

Run the \"LaTeX\" command on the master file for active buffer.
When compilation is complete, view output with default
viewer (using `TeX-view').
  (interactive)
  (TeX-command "LaTeX" 'TeX-master-file)
  (add-hook 'TeX-after-TeX-LaTeX-command-finished-hook #'my/TeX-view-once))

You may want to tinker with the TeX-command line in my/TeX-compile-and-view , since it hard-codes a lot of things that Cc Cc ( TeX-command-master ) does not. In particular, I'm not sure what it will do if there is no master file set, and it will recompile even if it doesn't need to.

EDIT: After some tinkering, it looks like everything runs fine without a master file, so long as you have this line in your .emacs:

(setq-default TeX-master nil)

I'm not sure why this is the case, since this says AUCTeX should query you for a master file if it's not already set, and this command does no querying even in that case. If you don't want to use this line, it shouldn't be too hard to make the above function work on the buffer instead.

You could bind F1 to one function, TeX-master-command, since Cc Cc will set the viewer if you use it just after compiling with Cc Cc. Here is a quote from auctex manual

Once you started the command selection with Cc Cc, Cc Cs or Cc Cb you will be prompted for the type of command. AUCTeX will try to guess which command is appropriate in the given situation and propose it as default. Usually this is a processor like 'TeX' or 'LaTeX' if the document was changed or a viewer if the document was just typeset.

To set this function to F1, you should try something like:

(add-hook 'LaTeX-mode-hook
     '(lambda()
     (local-set-key (kbd "<F1>") 'TeX-master-command)
     ))

You still will be prompted and have to press RET after F1. Binding F1 to Aaron Haaris TeX-compile-and-view might spare this RET.

Since you are tired of those Cc Cc, you should try latexmk. All you have is to launch it once, and then it will compile your .tex after every new save. You can set the viewer, how often latexmk checks if your .tex file as changed and, many, many other things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM