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 \begin{} 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.
I’m using emacs with cdlatex-mode to edit LaTeX files. I would like to know
Share
AUCTeX
If you use auctex:
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):
If you want a snippet for macros too you can use something like the following:
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:
And for macros if you want that too:
To use them put them in your .emacs and do M-x
ltx-environmentorltx-macrorespectively.