Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question led to a new package:
coseoul

Is it possible to create headings in LaTeX where you don't specify what level of the heading you want, but rather if you want to go up, down, stay or start over in the hierarchy that the heading is going to be placed within?

That is, instead of writing

\section{Level 1}
\subsection{Level 2}
\subsection{Level 2}
\subsubsection{Level 3}
\subsection{Level 2}
\section{Level 1}

write something like

\sectiontop{Level 1}
\sectiondown{Level 2}
\sectionstay{Level 2}
\sectiondown{Level 3}
\sectionup{Level 2}
\sectiontop{Level 1}

If you would use something along these lines, it would be possible to make quick changes. For example, if you suddenly realized that you wanted to place the headings on row 2-5 under a certain heading, you could easily do this by placing a \sectiondown before them. If you instead was using the system where you had to specify the level of every heading you would have to make individual changes to all 4 rows.

share|improve this question
5  
There's a related question here: Is there a program that allows to move sections and automatically adjusts the level of subsections?. Bruno Le Floch's answer seems to be what you are looking for. –  Alan Munn Aug 20 '11 at 16:13
    
Yes, that's it. Thanks! –  Speldosa Aug 20 '11 at 16:51

1 Answer 1

up vote 19 down vote accepted

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:

context sensive headings

share|improve this answer
    
This is really great! Something like this should really be included as a default package. But thanks! I'll try to edit it to suit my exact needs. –  Speldosa Aug 20 '11 at 19:56
    
You might want to turn this into a package and put it on CTAN. Here are some links about that: How can I contribute to CTAN?; What is good practice when preparing a package for CTAN:; Also, if you're unfamiliar with how packages are constructed texdoc clsguide will give you a good overview of their basic structure, and useful commands. –  Alan Munn Aug 24 '11 at 13:33
    
@Alan Munn: Yes, I thought about it too, thanks for the links! –  Tom Bombadil Aug 24 '11 at 14:25
2  
I wrote a package to incorporate this functionality, I uploaded it on CTAN, and now it is available there –  Tom Bombadil Sep 27 '11 at 12:00
    
@TomBombadil Cryptical names are hard to remember. While reading, names come to my mind: relheadings, relsectioning, or simply levels matching your \level commands. –  Stefan Kottwitz Oct 27 '11 at 16:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.