简体   繁体   English

如何使用Emacs + AUCTeX创建(自动插入)LaTeX模板?

[英]How do you create (automatically inserted) LaTeX template with Emacs+AUCTeX?

I use Emacs+AUCTex for writing LaTeX documents. 我使用Emacs + AUCTex编写LaTeX文档。 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 Cx Cw to write a new file from this template. 今天,我有一个仅包含此序言的.tex文件(因此是模板),我使用Cx Cw从该模板写入新文件。 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? 那么,有没有一种方法可以以另一种(较短的)方式在Emacs中调用LaTeX模板?

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. 编辑:自动插入模式提供了一种实现我想要的方法,但是当我创建.tex文件时,它不会自动放置我的模板(LaTeX前言)。 I have to launch Mx auto-insert. 我必须启动Mx自动插入。 How can I automatize this based on the file extension? 如何根据文件扩展名自动执行此操作?

At the end of VirTeX-common-initialization (essentially) TeX-master-file is added to find-file-hooks . VirTeX-common-initialization的末尾(本质上), TeX-master-file已添加到find-file-hooks This is the source for the %%% Local Variables: %%% mode: latex %%% TeX-master: t %%% End: stuff. 这是%%% Local Variables: %%% mode: latex %%% TeX-master: t %%% End:的来源%%% Local Variables: %%% mode: latex %%% TeX-master: t %%% End:东西。 (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 .) (注意, VirTeX-common-initialization是在第一件事LaTeX-common-initialization被称为在TeX-latex-mode是用于一个别名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. 但是,添加TeX-master-file过程非常困难,没有用户选项。 So, it seems to me that you have no other chance. 因此,在我看来,您没有其他机会。

After that correction the auto-insert stuff works automagically. 修正之后, auto-insert东西会自动工作。 (At least for me.) (至少对我来说。)

But, I have replaced the entries in auto-insert-alist . 但是,我替换了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. 我现在必须回家,无法对此进行进一步调查。

You're probably looking for auto-insert-mode . 您可能正在寻找auto-insert-mode This is orthogonal to AUCTeX - for instance, I use it to insert a class-template for .java files. 这与AUCTeX是正交的-例如,我使用它为.java文件插入类模板。

Put the following in your .emacs file: 将以下内容放入您的.emacs文件:

(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 eg to insert different preambles depending on the working directory. 当然,你可以把作为第一个参数的正则表达式define-auto-insert更复杂的如插入取决于工作目录不同的前导。

I adapted this code from an example from the EmacsWiki where you can also find additional information. 我从EmacsWiki的示例中改编了此代码,您还可以在其中找到其他信息。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM