Skip to main content

5.6.3 Adding Support for Environments

Adding support for environments is very much like adding support for TeX macros, except that each environment normally only takes one argument, an environment hook. The example is again a short version of β€˜latex.el’.

(TeX-add-style-hook  "latex"  (lambda ()    (LaTeX-add-environments     '("document" LaTeX-env-document)     '("enumerate" LaTeX-env-item)     '("itemize" LaTeX-env-item)     '("list" LaTeX-env-list)))) 

It is completely up to the environment hook to insert the environment, but the function LaTeX-insert-environment may be of some help. The hook will be called with the name of the environment as its first argument, and extra arguments can be provided by adding them to a list after the hook.

For simple environments with arguments, for example defined with β€˜\newenvironment’, you can make AUCTeX prompt for the arguments by giving the prompt strings in the call to LaTeX-add-environments. The fact that an argument is optional can be indicated by wrapping the prompt string in a vector.

For example, if you have defined a loop environment with the three arguments from, to, and step, you can add support for them in a style file.

%% loop.sty  \newenvironment{loop}[3]{...}{...} 
;; loop.el  (TeX-add-style-hook  "loop"  (lambda ()    (LaTeX-add-environments     '("loop" "From" "To" "Step")))) 

If an environment is defined multiple times, AUCTeX will choose the one with the longest definition. Thus, if you have an enumerate style file, and want it to replace the standard LaTeX enumerate hook above, you could define an β€˜enumerate.el’ file as follows, and place it in the appropriate style directory.

(TeX-add-style-hook  "latex"  (lambda ()    (LaTeX-add-environments     '("enumerate" LaTeX-env-enumerate foo))))  (defun LaTeX-env-enumerate (environment &optional ignore) ...) 

The symbol foo will be passed to LaTeX-env-enumerate as the second argument, but since we only added it to overwrite the definition in β€˜latex.el’ it is just ignored.

function LaTeX-add-environments* env …*​

Add each env to list of loaded environments.

function LaTeX-insert-environment* env [ extra ]*​

Insert environment of type env, with optional argument extra.

Following is a list of available hooks for LaTeX-add-environments:

LaTeX-env-item​

Insert the given environment and the first item.

LaTeX-env-figure​

Insert the given figure-like environment with a caption and a label.

LaTeX-env-array​

Insert the given array-like environment with position and column specifications.

LaTeX-env-label​

Insert the given environment with a label.

LaTeX-env-list​

Insert the given list-like environment, a specifier for the label and the first item.

LaTeX-env-minipage​

Insert the given minipage-like environment with position and width specifications.

LaTeX-env-tabular*​

Insert the given tabular*-like environment with width, position and column specifications.

LaTeX-env-picture​

Insert the given environment with width and height specifications.

LaTeX-env-bib​

Insert the given environment with a label for a bibitem.

LaTeX-env-contents​

Insert the given environment with a filename as its argument.

LaTeX-env-args​

Insert the given environment with arguments. You can use this as a hook in case you want to specify multiple complex arguments just like in elements of TeX-add-symbols. This is most useful if the specification of arguments to be prompted for with strings and strings wrapped in a vector as described above is too limited.

Here is an example from β€˜listings.el’ which calls a function with one argument in order to prompt for a key=value list to be inserted as an optional argument of the β€˜lstlisting’ environment:

(LaTeX-add-environments  '("lstlisting" LaTeX-env-args    [TeX-arg-key-val LaTeX-listings-key-val-options])) 

Some packages provide environments that are rarely useful to non-expert users. Those should be marked as expert environments using LaTeX-declare-expert-environments.

function LaTeX-declare-expert-environments* style ENVIRONMENTS...*​

Declare ENVIRONMENTS as expert environments of STYLE.

Expert environments are completed depending on β€˜TeX-complete-expert-commands’.