63

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?

lockstep
  • 250,273
Jan Hlavacek
  • 19,242
  • 1
    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 Santilli OurBigBook.com Sep 24 '12 at 14:23
  • 1
    [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 06:10

4 Answers4

38

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.

27

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>.

Ulrike Fischer
  • 327,261
20

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.

Aditya
  • 62,301
2

One explanation could be to avoid the Pyramid of Doom when writing long documents with lots of sections and paragraphs.

Compare this:

\begin{section}{Section One}
    ...
    \begin{subsection}{Subsection One}
        ...
        \begin{subsubsection}{Subsubsection One}
            ...
            \begin{paragraph}{This is First Paragraph}
                ...
                \begin{figure}[h]
                    \centering
                    \includegraphics[...]{...}
                    \caption{...}
                    \label{fig:...}
                \end{figure}
                ...
            \end{paragraph}
        \end{subsubsection}
    \end{subsection}
\end{section}

To this :

\section{Section One}
...
\subsection{Subsection One}
...
\subsubsection{Subsubsection One}
...
\paragraph{This is First Paragraph}
...
\begin{figure}[h]
    \centering
    \includegraphics[...]{...}
    \caption{...}
    \label{fig:...}
\end{figure}
...

In a really long article with lot of text in paragraphs and many figures the second form is way more readable. Even if you omit indentation for the first form it is still longer and less readable than the first form.

Louis Lac
  • 172