[Tex/LaTex] Is it a bad idea to use \begin{section} … \end{section} in LaTeX

environmentssectioning

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?

Best Answer

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.

Related Question