开发者

Relative section specification

Is it possible to specify the \section \subsection \subsubsection etc. level relative to the previous level? Wha开发者_Go百科t I'm thinking of is something like

\thissection The top level  
   \pushsection  
   \thissection The next level down  
   \thissection One more  
      \pushsection   
      \thissection Deeper  
   \popsection  
   \thissection At the same level and follows "one more"  

etc. The idea is that I'm writing a document from the inside out, i.e., starting at a deeper levels, and I don't know how many layers will be on top of it. This will avoid the need to do a massive re-leveling by renaming \subsection to \subsubsection etc.

BTW, a Google search for latex and "relative section" results in hits that almost exclusively involve misuse of the word "relative"; the authors meant to say "relevant section".

Thank you for any ideas.

Liam


You could implement your \pushsection, \popsection, and \thissection using a counter and if-then-else logic:

\usepackage{ifthen}
\newcounter{section-level}
\setcounter{section-level}{0}
\newcommand{\pushsection}{\addtocounter{section-level}{1}}
\newcommand{\popsection}{\addtocounter{section-level}{-1}}
\newcommand{\thissection}[1]
{
    \ifthenelse{\equal{\value{section-level}}{0}}{\section{#1}}{}
    \ifthenelse{\equal{\value{section-level}}{1}}{\subsection{#1}}{}
    \ifthenelse{\equal{\value{section-level}}{2}}{\subsubsection{#1}}{}
}

This will work exactly as you show above, for 3 levels of section. Of course, you should probably do something to handle out-of-range nesting levels (such as crashing the TeX build and printing a warning).


I've made a package out of the suggestions in the other answers.

Instead of the stack metaphor, the commands are \leveldown, \levelup and \dynsection.

The asection environment is present and I added a \gotochapterlevel.

\leveldown and \levelup take optional arguments to jump multiple levels at once.

I tried to respect the minimum and maximum levels of different document classes and their names but that's a mess, really. So for now you better just hack your favorite sectioning hierarchy into the thing which should be easy.

I hope someone will find this useful or can even improve on it (shouldn't be that hard, really):

Relative Sectioning Package


Given the following recursive macro to generate a string a number of times:

\newcommand{\Repeat}[2]{% #1=number of times, #2=what to repeat
  \ifnum\the\numexpr#1\relax>0%
    #2%
    \expandafter\Repeat\expandafter{\the\numexpr#1-1\relax}{#2}%
  \fi%
}%

and a global counter that will say how many "sub"s we need to prepend:

\newcounter{section-level}
\setcounter{section-level}{0}

then you have the macros:

\def\pushsection{\addtocounter{section-level}{1}}
\def\popsection{\addtocounter{section-level}{-1}}

\def\thissection#1{%
    \csname\Repeat{\value{section-level}}{sub}section\endcsname%
}%

However, I would also have considered defining an environment which does the pushing and popping:

\newenvironment{asection}[2][\defopt]{% #1=toc entry (optional), #2=heading
  \def\defopt{#2}%
  \thissection[#1]{#2}%
  \pushsection%
}{%
  \popsection%
}%

and rewrite your example into:

\begin{asection}{The top level}
  \begin{asection}{The next level down}\end{asection}
  \begin{asection}{One more}
    \begin{asection}{Deeper}\end{asection}
  \end{asection}
  \begin{asection}{At the same level and follows "one more"}\end{asection}
\end{asection}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜