# Is it a bad idea to use \begin{section} … \end{section} in LaTeX?

In several discussions about relatives merits of LaTeX v. xml, a xml supporter complained that in LaTeX sections, subsections etc are not properly closed, so it is hard to tell where a section ends. Of course it is typically possible to recognize an end of a section, but there is actually a number of ways how a section can end: start of another section, start of the next chapter or part, start of \endmatter, bibliograpy, index, etc, or the end of document. That makes it hard if you want to write a script that would in some way manipulate sections (change their order, write each of them into a separate file, ...). The same is of course true for subsections, paragraphs etc.

One answer to that is that it is actually possible in LaTeX2e to use sectioning commands as environments, and write

\begin{section}{Blah blah}
...
\end{section}


and because of the way environments are handled in LaTeX2e, it will work. I he recently seen several comments discouraging such use of regular commands as environments, and it seems that in LaTeX3 it will no longer be possible (on the other hand, it seems that you will be able to define a section environment without causing a conflict with the \section command).

I wanted to know what people think about using sectioning commands as environments. Is it a good idea, is it OK, or should it not be done?

-
 Nested style would be an advantage only if it could do relative nesting as intended by the package that @Will Robertson wanted to write mentioned in one of his answers. If relative nesting were possible, the major a advantage of using the nested style which I haven't seen mentioned here is that if either: * you decide to change the section structure of you document * your code is open source and someone decides to take a single section out of your document and insert it into theirs no renaming of section to subsection... etc will be necessary. [ctd] – ciro Sep 24 '12 at 14:23 [Rest of answer-to-comment by ciro] Since \begin{section}...\end{section} does not currently do this, I wouldn't recommend using it. – Joseph Wright♦ Sep 25 '12 at 6:10

Generally it's okay to do so. I once started writing a package to support this type of usage, with automatically nested sections and so on. So you could write

\begin{relsec}{This is a section}
blah blah
\begin{relsec}{This is a sub section}
meep meep
\end{relsec}
\begin{relsec}{This is a sub section}
meep meep
\end{relsec}
\end{relsec}


In the end, I found it was more difficult to write LaTeX documents using this syntax (although perhaps that was just me) so I never finished the project (it wasn't a big package). I occasionally think about dusting off the code again, though — it always seems like something that people (think they might) find useful.

One cautionary note: when you use an environment form, a group is created! If some package authors haven't anticipated this, you might end up with strangely behaving cross referencing or numbering or similar.

-
In fact, Jan, if you like I can send you the code and if you find it useful I would allow you to complete the package for me :) Email me if you're interested. – Will Robertson Feb 25 '11 at 7:30
In fact, I put the code here: <github.com/wspr/newsec>;. Use at your own risk :) – Will Robertson Feb 25 '11 at 7:40
to avoid creating a group, you can \def\relsec{\endgroup <your definition>}, and \def\endrelsec{<your definition>\begingroup\def \@currenvir {relsec}}, basically reproducing what is done for the document environment. – Bruno Le Floch Feb 25 '11 at 12:55
@Bruno that is a very good idea. – Will Robertson Feb 25 '11 at 17:29

I wouldn't do it. You add a lot unnecessary groups with unpredictable side-effects without gaining much. If you really want to mark the begin/end of components, I suggest to use markers in comment lines like e.g. the one in dtx-files %<sec1> ... %</sec1>.

-

ConTeXt now supports \startsection ... \stopsection environments. This allows for a more key-value driven syntax:

\startsection
[ title={Section title},
reference=sec:test,
marking={Page mark},
bookmark={Bookmark text},
]

....

\stopsection


It is also possible to add unused keys, say author={Chapter Author} and then use that value while typesetting the chapter heading.

-