13
votes

Is it possible to specify the \section \subsection \subsubsection etc. level relative to the previous level? What 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

3
Well the "Code and Preformatted Text" section advice in editing-help didn't work, I meant the example to be in a <pre><code> block with indents. Sorry.Liam
It sounds like, instead of trying to write all this code, that you should make an outline, and the use a top-down approach, rather than inside-out. I've written many documents using the inside-out things, and they always need much more editing & re-structuring than one written with a top-down approach & an outline. I know this doesn't answer your question directly, but there is a lot to be said for a more structured approach to document construction.Mica
Mica: I understand what you're saying, but the problem is a little more complicated than I stated. I am writing a talk using beamer and I've already written the paper, so I have an outline. I know I have more material than I can fit into a twenty minute talk, but I don't know how much I can fit. If I can fit two top-level topics, those will be the "sections" and everything under them the "subsections" etc. If I can only fit one top-level topic, than that's the whole subject of the talk, and the parts of that are the sections.Liam
ahhhhh. I see. God speed, then. :DMica

3 Answers

9
votes

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

7
votes

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

1
votes

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}