如何在 emacs 中的文本块周围插入 LaTeX 环境? [英] How to insert a LaTeX environment around a block of text in emacs?

查看:24
本文介绍了如何在 emacs 中的文本块周围插入 LaTeX 环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 cdlatex-mode 的 emacs 来编辑 LaTeX 文件.我想知道如何在已编写的文本块周围插入 LaTeX 环境,以便 egin{} 位于所选文本之前,而 end{} 位于所选文本之后.我尝试使用 cdlatex-environment 功能,但这样做会删除选定的文本.

解决方案

AUCTeX

如果您使用 :

  1. 标记要包含在环境中的文本块.
  2. C-c C-e.
  3. 输入您选择的环境类型(您只能键入一些字符并使用制表符完成),然后按 Enter.

详见手册.p>

请注意,有一种类似的方法可以将标记的文本包含在宏中.按照 1-3 的方式执行,但改为按 C-c C-eC-c Enter.有关详细信息,请参阅手册.

YASnippet

如果您使用 YASnippet,您可以创建与上述行为类似的代码段.例如,您可以使用以下内容(您已将键绑定"替换为正确的键绑定):

# -*- 模式:片段 -*-# 名称:LaTeX 环境# 键:键绑定"# --开始{$1}`yas/选定文本`$0结束{$1}

如果您也想要宏的片段,您可以使用类似以下的内容:

# -*- 模式:片段 -*-# 名称:LaTeX 宏# 键:键绑定"# --$1{`yas/选定文本`$0}

省略号

即使我推荐上述方法,也可能在某些情况下您希望使用一些简单的 elisp 函数.以下只是粗略的东西,功能远不如上述方法:

(defun ltx-environment (start end env)插入 LaTeX 环境."(交互式r
s环境类型:")(保存游览(如果(区域-活动-p)(预测(转到字符结束)(新队)(插入\end{"环境}")(转到字符开始)(插入 "\begin{" 环境 "}") (换行符))(插入 "\begin{" 环境 "}") (换行) (换行)(插入 "\end{" 环境 "} "))))

如果你也想要宏的话:

(defun ltx-macro (start end env)插入 LaTeX 宏."(交互式r
sMacro:")(保存游览(如果(区域-活动-p)(预测(goto-char end) (插入 "}")(goto-char start) (插入 "\" 环境 "{"))(插入 "\" 环境 "{}"))))

要使用它们,请将它们放入您的 .emacs 中并分别执行 M-x ltx-environmentltx-macro.

I'm using emacs with cdlatex-mode to edit LaTeX files. I would like to know how to insert a LaTeX environment around a block of text that is already written so that the egin{} goes before the selected text and the end{} goes after the selected text. I've tried to use cdlatex-environment function but doing so erases the selected text.

解决方案

AUCTeX

If you use :

  1. Mark the block of text you want to enclose in an environment.
  2. Press C-c C-e.
  3. Enter an environment type of your choice (you can only type some characters and use tab completion) and press Enter.

See the manual for details.

Note that there is a similar method to enclose marked text in macros. Do as 1–3 but instead press C-c C-e or C-c Enter instead. See the manual for details.

YASnippet

If you use YASnippet you can create a snippet with similar behavior as above. For example you can use the following (you have replace "keybinding" with a proper keybinding):

# -*- mode: snippet -*-
# name: LaTeX environment
# key: "keybinding"
# --
egin{$1}
  `yas/selected-text`$0
end{$1}

If you want a snippet for macros too you can use something like the following:

# -*- mode: snippet -*-
# name: LaTeX macro
# key: "keybinding"
# --
$1{`yas/selected-text`$0}

Elisp

Even if I recommend the above approaches there might be situations where you want instead to use some simple elisp function. The following is just something rough which has far less functionality than the above approaches:

(defun ltx-environment (start end env)
  "Insert LaTeX environment."
  (interactive "r
sEnvironment type: ")
  (save-excursion
    (if (region-active-p)
    (progn
      (goto-char end)
      (newline)
      (insert "\end{" env "}")
      (goto-char start)
      (insert "\begin{" env "}") (newline))
      (insert "\begin{" env "}") (newline) (newline)
      (insert "\end{" env "} "))))

And for macros if you want that too:

(defun ltx-macro (start end env)
  "Insert LaTeX macro."
  (interactive "r
sMacro: ")
  (save-excursion
    (if (region-active-p)
    (progn
      (goto-char end) (insert "}")
      (goto-char start) (insert "\" env "{"))
      (insert "\" env "{}"))))

To use them put them in your .emacs and do M-x ltx-environment or ltx-macro respectively.

这篇关于如何在 emacs 中的文本块周围插入 LaTeX 环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆