Code folding in LaTeX

The editor that I use, Texmaker, provides code folding only for things in the language, like \section and \begin{foo} ... \end{foo}. I would like to be able to make my own folding sections, like in Visual C++ where I can write #pragma region foo ... #pragma endregion. Specifically, the first thing that I want to hide in any code is the includes at the beginning, in the case of LaTeX, the \usepackage{} commands. I had the idea of making a custom environment:

\newenvironment{folding}{}{}
\begin{folding}
\usepackage{geometry}
\end{folding}


However, this won't build. Can anybody explain why? Is there a way to define an environment such that this would work?

migrated from stackoverflow.comFeb 9 '12 at 22:11

This question came from our site for professional and enthusiast programmers.

• The preamble can be folded in TeXmaker by clicking the fold "-" button to the left of the \documentclass line. – Fang Jing Jul 13 '16 at 6:13
• If you want to fold your \usepackages then possibly you use too many packages, i.e., multiple mentions of the same package, packages that you don't actually use in the document, packages that are redundant because they are already loaded by higher-level packages, etc. Cleaning that up may make compiling faster and help with code readability without the need for code folding. – Marijn Mar 27 at 10:41

The reason for this is the way environments in LaTeX work.

A \begin{...} command starts a group, stores the environment name and then executes the environment start command. A \end{...} command checks that you are closing the right environment, executes the environment end command and closes a group.

A group has quite some effects, one of them being that non-global assignments to variables (and similar stuff like command definitions) inside it are revoked when the group is closed. Because of this, you usually don't want to load packages inside a group. (It might be that \usepackage actually checks this.)

If you don't really want a group but just folding, I propose you have a look at your editor's configuration to see if you can use some comment structure which doesn't influence the actual code which LaTeX sees.

Otherwise, you might be able to do something similar like the document environment does - in its start command, it starts with closing the group started by \begin again, so there is no superfluous group. But then you might need to manage the current environment variable yourself so \end{folding} doesn't give you an error. Have a look at source2e to see how the kernel does it.

Thanks to Paulo, I have a solution for this particular problem. In Texmaker, the following will fold:

%\begin{}
\usepackage{foo}
%\end{}


Since almost all LaTeX editors at least support folding \begin and \end, here's a "true" empty environment:

\makeatletter
\newenvironment{folding}{\endgroup}{\begingroup \def \@currenvir{folding}\edef \@currenvline{\on@line}}
\makeatother


On \begin{folding} it will just end the internal group, while \end{folding} simply recreates \begin.

Test:

\begin{folding}
\newcommand{\mycommand}{Hello World}
\end{folding}
\mycommand


will print Hello World.