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:


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

  • The preamble can be folded in TeXmaker by clicking the fold "-" button to the left of the \documentclass line.
    – tvk
    Commented Jul 13, 2016 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
    Commented Mar 27, 2019 at 10:41

4 Answers 4


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:


This also works in:

  • Visual Studio Code (with the james-yu.latex-workshop extension)

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

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

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


\newcommand{\mycommand}{Hello World}

will print Hello World.


If you put the cursor directly before the first line of code, right where the # is (#\documentclass[@]{@}) in texmaker you are able to fold the whole preambel. Every \usepackage{@} will be folded.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .