2
votes

I use Emacs+AUCTex for writing LaTeX documents. I have specific needs so my typical preamble is quite long. Today, I have a .tex file with only this preamble (a template so) and I use C-x C-w to write a new file from this template. It isn't the best solution because my template localization could be far away from the new file.

So is there a way to call LaTeX templates in Emacs in another (shorter) way?

EDIT : auto-insert-mode offers a way to achieve what I want but it doesn't put automatically my template (LaTeX preamble) when I create a .tex file. I have to launch M-x auto-insert. How can I automatize this based on the file extension?

3
I wrote a patch to add templates in AUCTeX but there wasn't a follow-up.giordano
I recommend a form file directory of templates, and call up whatever you need and use it, or insert it into whatever you have already. You could also use YASnippet for that purpose. As @juanleon, suggested the function you want is insert-file. If using OSX, then insert-file-from-osx-panel is available if Emacs was built --with-ns.lawlist

3 Answers

3
votes

At the end of VirTeX-common-initialization (essentially) TeX-master-file is added to find-file-hooks. This is the source for the %%% Local Variables: %%% mode: latex %%% TeX-master: t %%% End: stuff. (Note, that VirTeX-common-initialization is the first thing in LaTeX-common-initialization which is called in TeX-latex-mode being an alias for latex-mode.)

To get ride of the automagically added comments you can remove the hook:

(add-hook 'TeX-mode-hook '(lambda ()
  (remove-hook 'find-file-hooks (car find-file-hooks) 'local)))

That looks like a hack. But adding TeX-master-file is quite hard-coded without user-options. So, it seems to me that you have no other chance.

After that correction the auto-insert stuff works automagically. (At least for me.)

But, I have replaced the entries in auto-insert-alist. Meaning, instead of

(define-auto-insert "\\.tex$" "my-latex-template.tex")

I have something like that:

(let ((el (assoc 'latex-mode auto-insert-alist)))
  (if el
      (setcdr el "/c/temp/autoinsert.tex")
    (define-auto-insert "\\.tex$" "/c/temp/autoinsert.tex")))

Maybe, that is important, maybe not. I've got to get home now and I cannot further investigate that.

1
votes

You're probably looking for auto-insert-mode. This is orthogonal to AUCTeX - for instance, I use it to insert a class-template for .java files.

Put the following in your .emacs file:

(auto-insert-mode)
 ;; *NOTE* Trailing slash important
(setq auto-insert-directory "/path/to/template/directory/")
(setq auto-insert-query nil)
(define-auto-insert "\\.tex$" "my-latex-template.tex")

Of course, you could make the regular expression used as the first argument to define-auto-insert more complex e.g. to insert different preambles depending on the working directory.

I adapted this code from an example from the EmacsWiki where you can also find additional information.

0
votes

This is the simplest solution I can think of:

(defun insert-latex-template()
  (when (= (point-max) (point-min))
    (insert-file "/path/to/your/template/file")))

(add-hook 'latex-mode-hook 'insert-latex-template)