I'm writing lots of LaTeX beamer slides with AUCTeX. Each slide starts as follows:



Is there a simple way to bind this template to a custom key command?

  • 1
    You could use yasnippet and insert stuff like that. Or, you could use: (defun hello-world () (interactive) (insert "\\begin{frame}\n\\frametitle{\\secname}\n\\framesubtitle{\\subsecname}\n\n\\end{frame}")) And any keyboard shortcut that floats your boat: (global-set-key [f5] 'hello-world) and you can use major-mode or minor-mode keyboard shortcuts or ... There are just a ton of possibilities ...
    – lawlist
    Oct 16 '17 at 4:32

I think it would be easier to add something like this to your .emacs and then go the usual C-c C-e frame RET route. The code below tweaks the definition of frame in beamer.el and gives you the chance to add a new frametitle and framesubtitle, otherwise it just adds \frametitle{\secname} and \framesubtitle{\subsecname} as requested.

(with-eval-after-load "beamer"
  (defun beamer-frame-env-with-subtitle ()
    (setq LaTeX-environment-list
          (assq-delete-all (car (assoc "frame" (LaTeX-environment-list)))
    (LaTeX-paragraph-commands-add-locally "framesubtitle")
     '("frame" (lambda (env &rest ignore)
                 (let ((title (TeX-read-string "(Optional) Title: " nil
                       (subtitle (TeX-read-string "(Optional) Subtitle: ")))
                   (LaTeX-insert-environment env)
                     (if (zerop (length title))
                         (insert "\\frametitle{\\secname}")
                       (insert (format "\\frametitle{%s}" title))
                       ;; This works because \frametitle is a
                       ;; paragraph command.
                   (if (zerop (length subtitle))
                       (insert "\\frametitle{\\subsecname}")
                     (insert (format "\\framesubtitle{%s}" subtitle))
    ;; Fontification
    (when (and (featurep 'font-latex)
               (eq TeX-install-font-lock 'font-latex-setup))
      (font-latex-add-keywords '(("framesubtitle" "<[{"))
      (font-latex-update-font-lock t)))
  (add-hook 'TeX-update-style-hook #'beamer-frame-env-with-subtitle t))

Note that you should have

(setq TeX-parse-self t)

in order to make this work.

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.