emacs auctex commandto compile and view
Asked Answered
K

3

5

I use EMACS/AucTeX. In order to compile I do C-c C-c, then it asks

"Command: (default LaTeX)"

I press RET and it is compiled. To view the compiled document I do C-c C-v.

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

Kenny answered 2/10, 2015 at 10:4 Comment(0)
H
15

C-c C-a (TeX-command-run-all) will do the job in AUCTeX 11.89.

Hypophyge answered 14/7, 2016 at 15:1 Comment(1)
This is a killer suggestion! Thanks, I have 11.87 with Ubuntu 14.04 though.Kenny
C
0

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 C-c C-c (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.

Conference answered 2/10, 2015 at 18:41 Comment(2)
Hi Aaron, thanks for this. I couldn't immagine it was so complicated. It doesn't work when there is no masterfile. Anyway do you known how to avoid that when I do C-c C-c it asks "Command: (default LaTeX)"? At least is it possible to simply compile without having to confirm the latex command? Thanks for all this, PietroKenny
@PiCo I took a look into the master-file-less situation and updated my answer with the result. To just bypass the confirmation, take a look at this answer. That's rather more involved, because it's replicating a lot of AUCTeX's decision-making machinery; if you'd rather just hardcode "LaTeX", just remove the add-hook form from my/TeX-compile-and-view above (and maybe rename the function).Conference
C
0

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

Once you started the command selection with C-c C-c, C-c C-s or C-c C-b 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 C-c C-c, 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.

Cayman answered 3/10, 2015 at 16:35 Comment(1)
You may want to assign another function key than F1, even in LaTeX mode. F1 is widely used for help in emacs. F5 to F9 are free to use natively if I remember well. But it's emacs, you can do as you want of course :)Semiskilled

© 2022 - 2024 — McMap. All rights reserved.