I would like have Emacs copy, by default, the contents of some defined file in the buffer of any non-existent .tex file I visit.

I am using AUCTeX and I believe that would be feasible by editing the file or variable that is responsible for appending the lines %%% Local Variables:, etc. in new .tex files, but I would prefer a solution by way of altering the .emacs file, for convenient portability and customizability.

In trying to look into it, I did encounter such Emacs hooks as find-file[-noselect]-hook, but I am unsure of how to specify that a hook will only be run for empty (non-existent) .tex files, or which Lisp function copies a file's contents in the current buffer.

Note: Use of the \input{...} command is not a work-around for what I intend to use this scheme for. To give a small example, I am trying to have the default .tex "skeleton" look somewhat like this:


\input{$HOME/defaults} %$
\title{ enter title here }


% document body %

\bibliography{ specify bib file here }



Thanks to Tom, I've found that I can get the above default contents (and in an actually better way, too) by using Emacs skeletons along with auto-insert-mode. Hence, I would put the following in my .emacs file:

(define-skeleton latex-skeleton
  "Default LaTeX file initial contents."
  "Title: "
  "\\input{\$HOME/defaults} %\$\n"
  "\\title{ "str | " ENTER TITLE HERE " "}\n\n"
  "\\bibliography{" (skeleton-read "BIB-file: ") | " SPECIFY BIB FILE HERE " "}\n\n"

(require 'autoinsert)
(add-hook 'find-file-hooks 'auto-insert)
(setq auto-insert-query nil)
(setq auto-insert-alist 
      '(("\\.tex$" . latex-skeleton)))

However, this does not work for .tex files, nor does it work for latex-mode (specified by changing the last line above to '((latex-mode . latex-skeleton))))!

If, instead, I use any other file extension or Emacs major mode it works fine. Therefore, I believe this to be caused by the AUCTeX-defined default actions on visiting a new .tex file, such as prompting the user to set the master file, appending lines like %%% Local Variables:, etc.

How could it be possible that I set AUCTeX to incorporate my customizations to its defaults? Or would there be some other work-around?

  • 1
    Your customization works for me: if I start with emacs -Q -l /path/to/auctex, and then evaluate your snippet, and open a new tex file the skeleton does run as expected. So there must be something else in your .emacs that is causing it to not work. If you can track it down by doing a binary search of your .emacs then we can find a work around. Mar 10 '12 at 11:21
  • @IvanAndrus's suggestion also works for me. I'm having a similar problem. Mar 11 '12 at 20:42

Do you know about auto insert mode?

  • Thanks! auto-insert-mode seems to be just the thing, particularly if used in conjunction with skeletons. Unfortunately, however, it seems that the default actions defined by AucTeX (e.g. prompting for a master file, and appending %%% Local Variables: , and so forth) suppress the auto-insert actions I define in my .emacs file... (I've updated my question as well.)
    – Sweeters
    Mar 6 '12 at 20:29

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.