# How to prevent LaTeX verbatim environments to be subject to LaTeX-fill-region

With LaTeX-verbatim-environments-local, it is possible to declare environments that AUCTeX should consider as verbatim ones.

\documentclass{article}
\begin{document}
\begin{myverbatimenv}
First verbatim line.
Second verbatim line.
\end{myverbatimenv}
\end{document}

% Local Variables:
% LaTeX-verbatim-environments-local: ("myverbatimenv")
% End:


The problem is, such environments are subject to LaTeX-fill-region (C-c C-q C-r). Hence, if the whole buffer is selected (C-x h) and is filled (C-c C-q C-r), the verbatim environment becomes:

\begin{myverbatimenv}
First verbatim line.  Second verbatim line.
\end{myverbatimenv}


whereas I would like:

\begin{myverbatimenv}
First verbatim line.
Second verbatim line.
\end{myverbatimenv}


How to prevent these LaTeX verbatim environments to be subject to LaTeX-fill-region?

• How do you define myverbatimenv? Do you use a LaTeX package like fancyvrb etc.? – Arash Esbati Mar 4 at 20:45
• @ArashEsbati For instance thanks to fancyvrb: \DefineVerbatimEnvironment{myverbatimenv}{Verbatim}{fontsize=\normalsize}. Another use case: make use of alltt environment from the eponymous package, the latter being loaded, not directly, but by a third party package loaded in the document. – Denis Bitouzé Mar 4 at 21:20

Add your self-defined verbatim environment to LaTeX-indent-environment-list.

Your modified example would look like this:

\documentclass{article}
\usepackage{verbatim}
\begin{document}
This text
just shows
that LaTeX-fill-region does something.
\begin{myverbatimenv}
First verbatim line.
Second verbatim line.
\end{myverbatimenv}
\end{document}

% Local Variables:
% LaTeX-verbatim-environments-local: ("myverbatimenv")
% eval: (setq-local LaTeX-indent-environment-list (cons '("myverbatimenv" current-indentation) (default-value 'LaTeX-indent-environment-list)))
% End:


Note the third paragraph in the doc string of LaTeX-indent-environment-list:

LaTeX-indent-environment-list is a variable defined in ‘latex.el’. Its value is shown below.

Documentation: Alist of environments with special indentation. The second element in each entry is the function to calculate the indentation level in columns.

Environments present in this list are not filled by filling functions, see LaTeX-fill-region-as-paragraph.

• Thanks Tobias. Indeed, I missed this LaTeX-indent-environment-list but it is not so obvious it has to do with LaTeX-fill-region :) I guess there is an extra \documentclass{article} in your example code. – Denis Bitouzé Mar 5 at 7:19
• Could you please add in your answer what is the syntax to use in the .emacs to add several environments to the LaTeX-indent-environment-list? I tried (add-to-list 'LaTeX-indent-environment-list '("myverbatimenv" current-indentation)) but that returns "Symbol’s value as variable is void: LaTeX-indent-environment-list". – Denis Bitouzé Mar 9 at 14:49

In case you're using fancyvrb.sty to define your custom environment, you only need to hit C-c C-n and let AUCTeX parse your document. AUCTeX has a sophisticated style file for fancyvrb.sty which takes care of adding your environment to various internal variables. After that, you can do C-c C-e myverTAB RET to include it in your document. AUCTeX will support the key-value's provided by fancyvrb.sty for your environment as well. This is the file I tested:

\documentclass{article}

\usepackage{fancyvrb}
\DefineVerbatimEnvironment{myverbatimenv}{Verbatim}{%
fontsize=\normalsize
}

\begin{document}

\begin{myverbatimenv}[numbers=right]
First verbatim line.
Second verbatim line.
\end{myverbatimenv}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:

• Some of my custom verbatim environments don't come from fancyvrb (e.g. alltt from the eponymous package). In fact, the document I'm working on is the French translation of the "The TeX Live Guide---2020". – Denis Bitouzé Mar 9 at 10:46