2

I'm generating some TeX code with XSLT that will be included in a main document. I've have no prior knowledge of the place where it will be included: it could be within a section or a subsection. How can I create a section just like a set of nested UL in html

<ul>
   <li>Hello</li>
   <li><ul>
      <li>World</li>
   </ul></li>
</ul>

I tried something like:

\begin{section}
Hello
\begin{section}
World
\end{section}
\end{section>

but it didn't work: they all have the same depth.

Pierre
  • 253

1 Answers1

1

The following implements a mysection environment that can be nested, automatically promoting and demoting section types to accommodate the nesting:

enter image description here

\documentclass{article}
\makeatletter
% Section
\@namedef{section@name@1}{section}
\expandafter\newlength\csname section@indent@1\endcsname\expandafter\setlength\csname section@indent@1\endcsname{\z@}
\expandafter\newlength\csname section@beforeskip@1\endcsname\expandafter\setlength\csname section@beforeskip@1\endcsname{-3.5ex \@plus -1ex \@minus -.2ex}
\expandafter\newlength\csname section@afterskip@1\endcsname\expandafter\setlength\csname section@afterskip@1\endcsname{2.3ex \@plus.2ex}
\@namedef{section@style@1}{\normalfont\Large\bfseries}
% Subsection
\@namedef{section@name@2}{subsection}
\expandafter\newlength\csname section@indent@2\endcsname\expandafter\setlength\csname section@indent@2\endcsname{\z@}
\expandafter\newlength\csname section@beforeskip@2\endcsname\expandafter\setlength\csname section@beforeskip@2\endcsname{-3.25ex\@plus -1ex \@minus -.2ex}
\expandafter\newlength\csname section@afterskip@2\endcsname\expandafter\setlength\csname section@afterskip@2\endcsname{1.5ex \@plus .2ex}
\@namedef{section@style@2}{\normalfont\large\bfseries}
% Subsubsection
\@namedef{section@name@3}{subsubsection}
\expandafter\newlength\csname section@indent@3\endcsname\expandafter\setlength\csname section@indent@3\endcsname{\z@}
\expandafter\newlength\csname section@beforeskip@3\endcsname\expandafter\setlength\csname section@beforeskip@3\endcsname{-3.25ex\@plus -1ex \@minus -.2ex}
\expandafter\newlength\csname section@afterskip@3\endcsname\expandafter\setlength\csname section@afterskip@3\endcsname{1.5ex \@plus .2ex}
\@namedef{section@style@3}{\normalfont\normalsize\bfseries}
% Paragraph
\@namedef{section@name@4}{subsubsection}
\expandafter\newlength\csname section@indent@4\endcsname\expandafter\setlength\csname section@indent@4\endcsname{\z@}
\expandafter\newlength\csname section@beforeskip@4\endcsname\expandafter\setlength\csname section@beforeskip@4\endcsname{3.25ex \@plus1ex \@minus.2ex}
\expandafter\newlength\csname section@afterskip@4\endcsname\expandafter\setlength\csname section@afterskip@4\endcsname{-1em}
\@namedef{section@style@4}{\normalfont\normalsize\bfseries}
% Subparagraph
\@namedef{section@name@5}{subsubsection}
\expandafter\newlength\csname section@indent@5\endcsname\expandafter\setlength\csname section@indent@5\endcsname{\parindent}
\expandafter\newlength\csname section@beforeskip@5\endcsname\expandafter\setlength\csname section@beforeskip@5\endcsname{3.25ex \@plus1ex \@minus .2ex}
\expandafter\newlength\csname section@afterskip@5\endcsname\expandafter\setlength\csname section@afterskip@5\endcsname{-1em}
\@namedef{section@style@5}{\normalfont\normalsize\bfseries}

\newcounter{section@level}
\newcommand{\unstepcounter}[1]{\addtocounter{#1}{-1}}
\newenvironment{mysection}
  {\stepcounter{section@level}%
   \@startsection{\@nameuse{section@name@\thesection@level}}
     {\thesection@level}
     {\@nameuse{section@indent@\thesection@level}}
     {\@nameuse{section@beforeskip@\thesection@level}}
     {\@nameuse{section@afterskip@\thesection@level}}
     {\@nameuse{section@style@\thesection@level}}}
  {\unstepcounter{section@level}}

\setcounter{secnumdepth}{5}
\makeatother
\begin{document}
\begin{mysection}{A section}
  Some text.
  \begin{mysection}{A subsection}
    Some text.
    \begin{mysection}{A subsubsection}
      Some text.
    \end{mysection}
    \begin{mysection}{A subsubsection}
      Some text.
      \begin{mysection}{A paragraph}
        Some text.
      \end{mysection}
      \begin{mysection}{A paragraph}
        Some text.
        \begin{mysection}{A subparagraph}
          Some text.
        \end{mysection}
      \end{mysection}
    \end{mysection}
  \end{mysection}
  \begin{mysection}{A subsection}
    Some text.
  \end{mysection}
\end{mysection}
\end{document}

The sectional definitions are taken from article.cls:

\newcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}
\newcommand\subsection{\@startsection{subsection}{2}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\normalfont\large\bfseries}}
\newcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\normalfont\normalsize\bfseries}}
\newcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
                                    {3.25ex \@plus1ex \@minus.2ex}%
                                    {-1em}%
                                    {\normalfont\normalsize\bfseries}}
\newcommand\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
                                       {3.25ex \@plus1ex \@minus .2ex}%
                                       {-1em}%
                                      {\normalfont\normalsize\bfseries}}

and broken down according to each section's name, level, indent, beforeskip, afterskip and style (see Where can I find help files or documentation for commands like \@startsection for LaTeX?).

With every call to the mysection environment, section@level is stepped to demote to the next lowest sectioning unit, and "unstepped" at the end of the environment. No testing for end-of-depth is made, but that's possible.

Werner
  • 603,163