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.
-
Try the specialist latex stackexchange.– MarcinJan 31, 2012 at 16:22
-
1@Marcin Since this is an Emacs question it might still make sense here.– N.N.Jan 31, 2012 at 16:24
-
@N.N. Sure, but OP might get more joy there.– MarcinJan 31, 2012 at 16:26
-
1@Marcin It is more (formally) on topic there but my experience is that it is easier to get help with Emacs on SO and SU than on TeX.SX.– N.N.Jan 31, 2012 at 16:28
2 Answers
AUCTeX
If you use auctex:
- Mark the block of text you want to enclose in an environment.
- Press C-c C-e.
- 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"
# --
\begin{$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\nsEnvironment 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\nsMacro: ")
(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.
-
Thank you for your answer but I'm not using auctex. I'm using cdlatex-mode, org-cdlatex-mode and org-mode. Jan 31, 2012 at 17:50
-
@DavidGagnon Would you be interested in a yasnippet solution? I think that you easily can make yasnippets similar to the above commands. There might however be conflicts between Org-mode and yasnippet, orgmode.org/manual/Conflicts.html#Conflicts.– N.N.Jan 31, 2012 at 17:57
-
1Thank you! I have installed yasnippets and created a simple snippet that does exactly what I was looking for. To enclose the marked text within the environment, I used
# expand-env: ((yas/wrap-around-region 't))in the snippet's header. Jan 31, 2012 at 21:46 -
@DavidGagnon That is good to hear. I have added a snippet to my answer for completeness but I used
yas/selected-textinstead. I guess it leads to similar behavior though.– N.N.Feb 1, 2012 at 8:17
Following the suggestion in an answer here by Tikhon Jelvis, I had a look at the latex mode documentation (C-h m) and found mention of the function
latex-insert-block
which seems to do exactly what you want.
The shortcut key is C-c C-t (whenever you are in latex mode).