I'd like to find a way to organize and access my (Latex) templates within Emacs and so far, I didn't find a satisfying solution. (This post is related, but doesn't solve my question.)

Let's say that I want to typeset some article. Optimally, I would be able to create a template for a folder that contains several relevant files (and folders) and specify which of them should be opened in new buffers once I create a new project from this template. It might also be desirable that the generated file names follow a specified naming scheme relative to my project name (e.g. creating a project "x" should result in a new folder "x" containing files "x_main.tex", "x_coverpage.tex", "x_options.tex", ... and folders "images/", ... ).

Is there any plugin that suits my purposes? What about other solutions?

  • Are you using org-mode for LaTeX exports? Then please have a look for their capture templates gnu.org/software/emacs/manual/html_node/org/…. Pro: you are able to do nifty reproducible research as well. Con: learning another layer between TeX and you. I'm using the capture stuff for engineering reports and letters ;-). – Dieter.Wilhelm Apr 21 '16 at 4:21

I don't know how much elisp you know, so I'll post this for whoever can use it. I have not tested this code, but here's another defun (modelled after the one above) that will copy N of your favorite templates into a new project directory, creating xxx_main.tex, xxx_coverpage.tex, etc. There are more flexible and sophisticated ways of doing this, but I just made up this code to be simple, to get someone started. Code for handling templates often reflects the conceptual models of template organization and use, and that varies from person to person. I hope this code is useful enough to get someone started... :-)

(defun copy-templates ()
  "Copy a few templates into a new project directory."
  (let (prompt template-dir destination-dir)
    (setq prompt "Template directory to use: (c=code, l=checklist, p=process): ")
    (setq template-dir (read-from-minibuffer prompt))
    (setq prompt "Destination directory to use: ")
    (setq destination-dir (read-from-minibuffer prompt))
    (setq prompt "Base name of new project xxx: ")
    (setq base (read-from-minibuffer prompt))
    ;; copy and rename the template files you want to use
    (copy-file (concat (file-name-directory template-dir) "template1.tex")
               (concat (file-name-directory destination-dir)
                       (concat base "_main.tex")))
    (copy-file (concat (file-name-directory template-dir) "template2.tex")
               (concat (file-name-directory destination-dir)
                       (concat base "_whatever.tex")))
  • Thank you, again. I'm very new Emacs and elisp and therefore my ability to write a suitable code is severely limited. It is however sufficient to understand your code and tinker with it - which is ideal. – Stefan Mesken Apr 26 '16 at 3:38

It seems to me that you could do all that with some elisp programming. It doesn't sound too hard to me (mostly some keys bound to defuns that will copy / open / insert / write-out the files that you are talking about. Maybe with a comint call to the operating system, if need be.

For example, here's my code that I use for fetching / inserting various code, checklist, or process templates in my own work. It might be a starting point for you. I bind the defun to a key, then it prompts me for a template type (c/l/p), then prompts for a file read using the appropriate directory for the type of template that I want to work with.

(defun insert-snippet-at-point ()
  "Prompt user for a snippet name, and insert it at the point."
  (let ((prompt nil) (type nil) (dir nil))
    (setq prompt "Type to insert (c=code, l=checklist, p=process): ")
    (setq type (read-from-minibuffer prompt))
    (cond ((equal type "c")                ;code
           (setq dir ctkb-v-code-dir))
          ((equal type "k")
           (setq dir ctkb-v-checklist-dir))
          ((equal type "p")                ;lisp process frags
           (setq dir ctkb-v-procs-dir))
           (setq dir ctkb-v-code-dir)))
    (setq fn (read-file-name "Insert filename: " dir))
    (insert-string (file-get-contents fn))))
  • Thank you. This seems indeed to be applicable to my situation. – Stefan Mesken Apr 26 '16 at 3:10

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.