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?
Best Answer
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.