Учитывая следующий рекурсивный макрос для генерации строки несколько раз:
\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%
}%
и глобальный счетчик, который скажет, сколько «sub» нам нужно добавить:
\newcounter{section-level}
\setcounter{section-level}{0}
тогда у вас есть макросы:
\def\pushsection{\addtocounter{section-level}{1}}
\def\popsection{\addtocounter{section-level}{-1}}
\def\thissection#1{%
\csname\Repeat{\value{section-level}}{sub}section\endcsname%
}%
Тем не менее, я бы также рассмотрел определение среды, в которой выполняются нажатия и выталкивания:
\newenvironment{asection}[2][\defopt]{% #1=toc entry (optional), #2=heading
\def\defopt{#2}%
\thissection[#1]{#2}%
\pushsection%
}{%
\popsection%
}%
и перепишите ваш пример на:
\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}