Here you go. It works, but so far only for \chapter, \section, \subsection and \subsubsection. It works by defining \currrentlevel, which is empty initially, so you need to use one of the new section commands (\newchapter, \newsection, \newsubsection and \newsubsubsection). After that you can use \levleup, \levelstay and \leveldown.
You can expand it to \part, \paragraphs and such, but remember a few things:
\levelup moves exactly one level up, you cant go from \subsubsection (e.g. 1.2.3.4) to a \section (e.g. 1.3) as that is two levels difference. Either you define a \twolevelup, or use \newsection (but that would break relativity)
- the order of definitions is important. It is done via
\ifthenelse constructs, so the \levleup definition needs to be top-down (c-s-ss-sss) and \levledown needs to be down-up (sss-ss-s-c)
- remember what class you use.
article does not know \chapters, so trying \levleup after a section will probably cause an error
- remember that
\currentsection is not initialized at the beginning, so initialize it by using e.g. \newchapter
\documentclass{report}
\usepackage{ifthen}
\newcommand{\currentlevel}{}
\newcommand{\levelup}[1]%
{ \ifthenelse{\equal{\currentlevel}{c}}%
{\chapter{#1}\renewcommand{\currentlevel}{c}}{}%
\ifthenelse{\equal{\currentlevel}{s}}%
{\chapter{#1}\renewcommand{\currentlevel}{c}}{}%
\ifthenelse{\equal{\currentlevel}{ss}}%
{\section{#1}\renewcommand{\currentlevel}{s}}{}%
\ifthenelse{\equal{\currentlevel}{sss}}%
{\subsection{#1}\renewcommand{\currentlevel}{ss}}{}%
}
\newcommand{\leveldown}[1]%
{ \ifthenelse{\equal{\currentlevel}{sss}}%
{\subsubsection{#1}\renewcommand{\currentlevel}{sss}}{}%
\ifthenelse{\equal{\currentlevel}{ss}}%
{\subsubsection{#1}\renewcommand{\currentlevel}{sss}}{}%
\ifthenelse{\equal{\currentlevel}{s}}%
{\subsection{#1}\renewcommand{\currentlevel}{ss}}{}%
\ifthenelse{\equal{\currentlevel}{c}}%
{\section{#1}\renewcommand{\currentlevel}{s}}{}%
}
\newcommand{\levelstay}[1]%
{ \ifthenelse{\equal{\currentlevel}{c}}%
{\chapter{#1}}{}%
\ifthenelse{\equal{\currentlevel}{s}}%
{\section{#1}}{}%
\ifthenelse{\equal{\currentlevel}{ss}}%
{\subsection{#1}}{}%
\ifthenelse{\equal{\currentlevel}{sss}}%
{\subsubsection{#1}}{}%
}
\newcommand{\newchapter}[1]{\chapter{#1}\renewcommand{\currentlevel}{c}}
\newcommand{\newsection}[1]{\section{#1}\renewcommand{\currentlevel}{s}}
\newcommand{\newssubection}[1]{\subsection{#1}\renewcommand{\currentlevel}{ss}}
\newcommand{\newssubsubection}[1]{\subsubsection{#1}\renewcommand{\currentlevel}{sss}}
\setcounter{tocdepth}{5}
\begin{document}
\tableofcontents
\newpage
\newchapter{chapter 1}
\leveldown{ 1.1}
\levelstay{ 1.2}
\levelstay{ 1.3}
\leveldown{ 1.3.1}
\levelstay{ 1.3.2}
\levelup{ 1.4}
\levelstay{ 1.5}
\leveldown{ 1.5.1}
\levelstay{ 1.5.2}
\leveldown{ 1.5.2.1}
\levelstay{ 1.5.2.2}
\levelstay{ 1.5.2.3}
\levelstay{ 1.5.2.4}
\newchapter{chapter 2}
\leveldown{ 2.1}
\levelstay{ 2.2}
\levelstay{ 2.3}
\leveldown{ 2.3.1}
\levelstay{ 2.3.2}
\levelup{ 2.4}
\levelstay{ 2.5}
\leveldown{ 2.5.1}
\end{document}
Edit 1: I totally to overhauled it.
New features:
- checks for existance of
\chapter, skips it if it does not
- now includes
\part, \paragraph and \subparagraph
- levels going to high / low are replaced by highest / lowest available
- includes
\levelmultiup, to go up multiple levels, e.g. from \subsubsection to \chapter via \levlemultiup{chapter heading}{3}
But there are a few things that remain to be done:
- specify maximum / minimum level to be used (if you e.g. don't like
\parts
- optional warning of some kind when levels are too low or high so they are changed (and therefore are no longer in the required hierarchy)
- somehow expand the individual outline elements so they set
\currentlevel, e.g. are better way of initialization than \setcounter{currentlevel}{7}
And here's the code:
\documentclass{report}
\usepackage{ifthen}
\usepackage[margin=10mm]{geometry}
\usepackage{multicol}
\newcommand{\chex}{}
\ifthenelse{\isundefined{\chapter}}{\renewcommand{\chex}{N}}{\renewcommand{\chex}{Y}}
\newcounter{currentlevel}
% part = 7
% chapter = 6
% section = 5
% subsection = 4
% subsubsection = 3
% paragraph = 2
% subparagraph = 1
\newcommand{\findnewlevel}[1]% uppity (-1 for level down, 0 for stay, 1 for up, 2-6 for multiup
{ \addtocounter{currentlevel}{#1}%
\ifthenelse{\equal{\chex}{N}}%
{ \ifthenelse{\value{currentlevel} = 6}%
{ \ifthenelse{#1 > 1}{\addtocounter{currentlevel}{1}}{\addtocounter{currentlevel}{-1}}}{}}{}%
\ifthenelse{\value{currentlevel} < 1}{\setcounter{currentlevel}{1}}{}%
\ifthenelse{\value{currentlevel} > 7}{\setcounter{currentlevel}{7}}{}%
}
\newcommand{\levelchange}[2]% title, uppity
{ \findnewlevel{#2}%
\ifthenelse{\value{currentlevel} = 1}{\subparagraph{#1}}{}%
\ifthenelse{\value{currentlevel} = 2}{\paragraph{#1}}{}%
\ifthenelse{\value{currentlevel} = 3}{\subsubsection{#1}}{}%
\ifthenelse{\value{currentlevel} = 4}{\subsection{#1}}{}%
\ifthenelse{\value{currentlevel} = 5}{\section{#1}}{}%
\ifthenelse{\value{currentlevel} = 6}{\chapter{#1}}{}%
\ifthenelse{\value{currentlevel} = 7}{\part{#1}}{}%
}
\newcommand{\levelup}[1]{\levelchange{#1}{1}}
\newcommand{\leveldown}[1]{\levelchange{#1}{-1}}
\newcommand{\levelstay}[1]{\levelchange{#1}{0}}
\newcommand{\levelmultiup}[2]{\levelchange{#1}{#2}} %title, uppity
\setcounter{tocdepth}{5}
\setcounter{secnumdepth}{5}
\begin{document}
\pagestyle{plain}
\begin{multicols}{2}
\tableofcontents
\end{multicols}
\newpage
\setcounter{currentlevel}{7}
\levelstay{part}
\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\leveldown{subsubsec}
\leveldown{par}
\leveldown{subpar}
\levelstay{subpar}
\levelup{par}
\levelstay{par}
\levelup{subsubsec}
\levelstay{subsubsec}
\levelup{subsec}
\levelstay{subsec}
\levelup{sec}
\levelstay{sec}
\levelup{chap}
\levelstay{chap}
\levelup{part}
\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\leveldown{subsubsec}
\leveldown{par}
\leveldown{subpar}
\levelmultiup{part}{6}
\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\leveldown{subsubsec}
\leveldown{par}
\levelmultiup{part}{5}
\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\leveldown{subsubsec}
\levelmultiup{part}{4}
\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\levelmultiup{part}{3}
\leveldown{chap}
\leveldown{sec}
\levelmultiup{part}{2}
\end{document}
Which results in the following:
