# latex dynamic subsection nesting

I need to find a command/environment that allows me to nest a [sub]section one level deeper than the level of the parent [sub]section.

For example, given this syntax,

\section{Foo}
\createanestedsection{Bar}
\subsection{Zoo}
\createanestedsection{Bar}


I'd like to obtain this output:

1. ...... Foo
1.1 ..... Bar
1.2 ..... Zoo
1.2.1 ... Bar


Is there anything like that available ?

Regards, R

• Welcome to StackOverflow! (La)TeX-related questions are better asked at TeX - LaTeX. Your question has been flagged so that it gets migrated there. – Alexis Pigeon Aug 21 '14 at 9:48
• Interesting, but to be useful you need a command to enter a nested level and another one to leave the nested level. – alfC Aug 21 '14 at 19:08
• You may use the answer here tex.stackexchange.com/a/31010/17424 and play with the index of the \level command (+1 when nested). Or am I off-topic ? – user17424 Dec 19 '14 at 22:02
• @alfc I understand your intent, but a \section et al. are not environments, but merely macros. Thus, there is no need to "leave" anything, but merely to issue the macro one level down from the current level. – Steven B. Segletes Dec 19 '14 at 22:15

I came up with this:

\renewcommand\thesubsection{\arabic{subsection}}

\newcommand{\nestasection}[1]{
\ifnum \thesubsection >0
\subsubsection{#1}
\else
\ifnum \thesection >0
\subsection{#1}
\else
\section{#1}
\fi
\fi
}


The various section commands in LaTeX are designed to do just what you ask for.

\documentclass{article}
\begin{document}

\section{Foo}
\subsection{Bar}

\section{Zoo}
\subsection{Bar}
\subsubsection{Baz}
\paragraph{Moo}

\section{Loo}
\subsection{Boo}

\end{document}