3

Is there a way to make a shortcut key only by which I can compile a .tex file?

For LaTeX compilation, I'm using shortcut ^C ^C. It shows me 'latex' as default or 'view' sometimes. If it is set to 'latex', I just press Enter but set to 'view', then I have to type 'latex' to compile. Then I use ^C ^Vto view the pdf if needed.

This is the most frequent operation that I use but it's quite annoying to type 'latex' explicitly after ^C ^C. I'm a novice on Emacs and Aquamacs (have used it just for a week).

  • You can create a keyboard macro, save it, and then bind it to a key. (I can't find the specific function, either, but it could be traced.) – Sean Allred Apr 7 '13 at 11:31
  • 1
    Hint: you can just type l and press enter. ;) – Sean Allred Apr 7 '13 at 11:55
5

Just click on the LaTeX button in the tool bar. :)

enter image description here

Alternatively, do Command-O to open a file and navigate to

<HOME>/Library/Preferences/Aquamacs Emacs

and open Preferences.el (which is preferred in Aquamacs to the .emacs file). Add at the bottom

(add-hook
 'LaTeX-mode-hook
 (lambda ()
  (local-set-key
  (kbd "C-c c")
   (lambda nil
    (interactive)
    (progn
     (TeX-save-document
      (TeX-master-file)
     )
    (TeX-command "LaTeX" (quote TeX-master-file) -1))))))

that will bind the same function executed by clicking the icon to C-c c. You might want to add a similar action bound to C-c v for viewing

(add-hook
 'LaTeX-mode-hook
 (lambda ()
  (local-set-key
  (kbd "C-c v")
  (lambda nil (interactive)
   (TeX-command "View" (quote TeX-master-file) -1)))))
3

I am using Aquamacs and had the same question. The post from Sean Allred helped me find the answer I was looking for. The only thing I would add is that if you solely use Aquamacs it is better to edit the file

 Preference.el
located in
 ~/Library/Preferences/Aquamacs\ Emacs/ 
Once you add the code below in the file the command sequence
 C-c C-a 
will run the default compiler and then send the output to preview. This snippet of code was copied from http://www.emacswiki.org/emacs/TN#toc8 under the TEX-texify section.

    (require 'tex-buf)
(defun TeX-command-default (name)
  "Next TeX command to use. Most of the code is stolen from `TeX-command-query'."
  (cond ((if (string-equal name TeX-region)
                 (TeX-check-files (concat name "." (TeX-output-extension))
                          (list name)
                          TeX-file-extensions)
               (TeX-save-document (TeX-master-file)))
             TeX-command-default)
            ((and (memq major-mode '(doctex-mode latex-mode))
                  (TeX-check-files (concat name ".bbl")
                           (mapcar 'car
                               (LaTeX-bibliography-list))
                           BibTeX-file-extensions))
             ;; We should check for bst files here as well.
             TeX-command-BibTeX)
            ((TeX-process-get-variable name
                           'TeX-command-next
                           TeX-command-Show))
            (TeX-command-Show)))


(defcustom TeX-texify-Show t "Start view-command at end of TeX-texify?" :type 'boolean :group 'TeX-command)
(defcustom TeX-texify-max-runs-same-command 5 "Maximal run number of the same command" :type 'integer :group 'TeX-command)

(defun TeX-texify-sentinel (&optional proc sentinel)
  "Non-interactive! Call the standard-sentinel of the current LaTeX-process.
If there is still something left do do start the next latex-command."
  (set-buffer (process-buffer proc))
  (funcall TeX-texify-sentinel proc sentinel)
  (let ((case-fold-search nil))
    (when (string-match "\\(finished\\|exited\\)" sentinel)
      (set-buffer TeX-command-buffer)
      (unless (plist-get TeX-error-report-switches (intern (TeX-master-file)))
    (TeX-texify)))))

(defun TeX-texify ()
  "Get everything done."
  (interactive)
  (let ((nextCmd (TeX-command-default (TeX-master-file)))
    proc)
    (if (and (null TeX-texify-Show)
         (equal nextCmd TeX-command-Show))
    (when  (called-interactively-p 'any)
      (message "TeX-texify: Nothing to be done."))
      (TeX-command nextCmd 'TeX-master-file)
      (when (or (called-interactively-p 'any)
        (null (boundp 'TeX-texify-count-same-command))
        (null (boundp 'TeX-texify-last-command))
        (null (equal nextCmd TeX-texify-last-command)))
    (mapc 'make-local-variable '(TeX-texify-sentinel TeX-texify-count-same-command TeX-texify-last-command))
    (setq TeX-texify-count-same-command 1))
      (if (>= TeX-texify-count-same-command TeX-texify-max-runs-same-command)
      (message "TeX-texify: Did %S already %d times. Don't want to do it anymore." TeX-texify-last-command TeX-texify-count-same-command)
    (setq TeX-texify-count-same-command (1+ TeX-texify-count-same-command))
    (setq TeX-texify-last-command nextCmd)
    (and (null (equal nextCmd TeX-command-Show))
         (setq proc (get-buffer-process (current-buffer)))
         (setq TeX-texify-sentinel (process-sentinel proc))
         (set-process-sentinel proc 'TeX-texify-sentinel))))))

(add-hook 'LaTeX-mode-hook '(lambda () (local-set-key (kbd "C-c C-a") 'TeX-texify))) 
1

Barring any specific key sequence (which I know there is one, I just can't find it), this will work:

(add-hook
 'LaTeX-mode-hook
 (lambda ()
   (local-set-key
    (kbd "C-c c")
    (lambda (&optional arg)
      "Run the current document through LaTeX."
      (interactive "p")
      (kmacro-exec-ring-item
       (quote ([3 3 76 97 84 101 88 return] 0 "%d"))
       arg)))

This bind the key sequence C-c c to a keyboard macro that runs C-c C-c LaTeX RET.


If you were wondering, no I didn't make the important part of this myself. I used C-x ( to start a keyboard macro, then I typed C-c C-c LaTeX RET, and then C-c ) to finish the macro. I then used C-x C-k n to name the macro, and then ran M-x insert-kbd-macro while inside my .emacs (usually findable by C-x C-f ~/.emacs).

I bind the macro to a key sequence using local-set-key, and then add this to the LaTeX-mode hook to have it execute every time you enter LaTeX-mode.

0

Alternatively, you can place the following in your .emacs and have everything done automatically in succession, much like Quick Build in TeXMaker.

(require 'tex-buf)
(defun TeX-command-default (name)
  "Next TeX command to use. Most of the code is stolen from `TeX-command-query'."
  (cond ((if (string-equal name TeX-region)
                 (TeX-check-files (concat name "." (TeX-output-extension))
                          (list name)
                          TeX-file-extensions)
               (TeX-save-document (TeX-master-file)))
             TeX-command-default)
            ((and (memq major-mode '(doctex-mode latex-mode))
                  (TeX-check-files (concat name ".bbl")
                           (mapcar 'car
                               (LaTeX-bibliography-list))
                           BibTeX-file-extensions))
             ;; We should check for bst files here as well.
             TeX-command-BibTeX)
            ((TeX-process-get-variable name
                           'TeX-command-next
                           TeX-command-Show))
            (TeX-command-Show)))


(defcustom TeX-texify-Show t "Start view-command at end of TeX-texify?" :type 'boolean :group 'TeX-command)
(defcustom TeX-texify-max-runs-same-command 5 "Maximal run number of the same command" :type 'integer :group 'TeX-command)

(defun TeX-texify-sentinel (&optional proc sentinel)
  "Non-interactive! Call the standard-sentinel of the current LaTeX-process.
If there is still something left do do start the next latex-command."
  (set-buffer (process-buffer proc))
  (funcall TeX-texify-sentinel proc sentinel)
  (let ((case-fold-search nil))
    (when (string-match "\\(finished\\|exited\\)" sentinel)
      (set-buffer TeX-command-buffer)
      (unless (plist-get TeX-error-report-switches (intern (TeX-master-file)))
    (TeX-texify)))))

(defun TeX-texify ()
  "Get everything done."
  (interactive)
  (let ((nextCmd (TeX-command-default (TeX-master-file)))
    proc)
    (if (and (null TeX-texify-Show)
         (equal nextCmd TeX-command-Show))
    (when  (called-interactively-p 'any)
      (message "TeX-texify: Nothing to be done."))
      (TeX-command nextCmd 'TeX-master-file)
      (when (or (called-interactively-p 'any)
        (null (boundp 'TeX-texify-count-same-command))
        (null (boundp 'TeX-texify-last-command))
        (null (equal nextCmd TeX-texify-last-command)))
    (mapc 'make-local-variable '(TeX-texify-sentinel TeX-texify-count-same-command TeX-texify-last-command))
    (setq TeX-texify-count-same-command 1))
      (if (>= TeX-texify-count-same-command TeX-texify-max-runs-same-command)
      (message "TeX-texify: Did %S already %d times. Don't want to do it anymore." TeX-texify-last-command TeX-texify-count-same-command)
    (setq TeX-texify-count-same-command (1+ TeX-texify-count-same-command))
    (setq TeX-texify-last-command nextCmd)
    (and (null (equal nextCmd TeX-command-Show))
         (setq proc (get-buffer-process (current-buffer)))
         (setq TeX-texify-sentinel (process-sentinel proc))
         (set-process-sentinel proc 'TeX-texify-sentinel))))))

(add-hook 'LaTeX-mode-hook '(lambda () (local-set-key (kbd "C-c C-a") 'TeX-texify)))

This binds the key sequence C-c C-a to TeX-texify, which goes through all of the compilation steps necessary to produce the current document. Shamelessly stolen from the Emacs Wiki.

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.