Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Feb 9 '12 at 22:11

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

add comment

2 Answers

up vote 7 down vote accepted

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.

share|improve this answer
add comment

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

%\begin{}
\usepackage{foo}
%\end{}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.