8

When working with large tex documents I often find myself using search/replace to restructure them (eg. make sections out of chapters or subsubsections out of subsections)

Is there any way to define my chapters/sections relatively?

So that instead of

\chapter{one}
\section{two}
\section{three}
\chapter{four}

it would be possible to write something like this:

\currentdepth{one}
\nextdepth{two}
\currentdepth{three}
\prevdepth{four}

This way I could simply move around my content in the document without having to touch all of the headings.

Thank you!

Ruben
  • 13,448
Simbi
  • 363
  • This isn't particularly useful, but I know that there is a package that does this...I just can't remember what it is called:( –  Jul 23 '15 at 12:04
  • This won't be useful either, but: If you want to do it by yourself, you can define a command to begin a section and one to end a section - or an environment. In the begin command (or environment begin), you take the section title as parameter. You increase a counter (initially 0). If the counter is now 1, you call \chapter{#1}, if it is 2, you do \section{#1}, etc. In the end command, you decrease the counter again. – Thomas Weise Jul 23 '15 at 12:22
  • 1
    The linked question is related, but not a duplicate. The respective solutions for each approach are very different -- the linked just shifts sectioning commands to 'fake' the result while this question would have to maintain some sort of state to keep track of the current level from call to call. Perhaps there is another duplicate around (this question seems like it would've come up), but the linked Q isn't it. – Sean Allred Jul 23 '15 at 13:57

2 Answers2

1

A preliminary solution, not finished so far!

\documentclass{book}

\usepackage{etoolbox}
\usepackage{xparse}

\newcounter{sectionlevel}

\makeatletter



\NewDocumentCommand{\currentdepth}{sO{}m}{%
  \IfBooleanTF{#1}{%
  }{
    \ifnum\value{sectionlevel} = -1
    \part[#2]{#3}%
    \else
    \ifcase\value{sectionlevel} 
    \chapter[#2]{#3}
    \or
    \section[#2]{#3}
    \or
    \subsection[#2]{#3}
    \or
    \subsubsection[#2]{#3}
    \or
    \paragraph[#2]{#3}
    \or
    \subparagraph[#2]{#3}
    \else
    Oopss!
    \fi
    \fi
  }
}

\NewDocumentCommand{\nextdepth}{sO{}m}{%
  \stepcounter{sectionlevel}
  \currentdepth[#2]{#3}
}

\NewDocumentCommand{\previousdepth}{sO{}m}{%
  \addtocounter{sectionlevel}{-1}
  \currentdepth[#2]{#3}
}

\makeatother   



\AtBeginDocument{%
  \setcounter{sectionlevel}{0} %
}


\begin{document}
\tableofcontents
\currentdepth{Hello World}
\nextdepth{Hello World - Section}
\currentdepth{Hello World - Section - Again}
\nextdepth{Hello World - Subsection}
\previousdepth{A section again}
\previousdepth{A new chapter}


\end{document}

Edit Improved version, with starred and optional versions working, and the \previousdepth<-3>{...} possitibility to jump back 3 levels (or whatever.

No real checking about allowed levels is done!

\documentclass{book}

\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{letltxmacro}

\newcounter{sectionlevel}



\makeatletter

\NewDocumentCommand{\currentdepth}{som}{%
  \LetLtxMacro\generic@@section\relax%
  \ifnum\value{sectionlevel} = -1
  \global\LetLtxMacro \generic@@section\part
  \else
  \ifcase\value{sectionlevel} 
  \global\LetLtxMacro\generic@@section\chapter
  \or
  \global\LetLtxMacro\generic@@section\section
  \or
  \global\LetLtxMacro\generic@@section\subsection
  \or
  \global\LetLtxMacro\generic@@section\subsubsection
  \or
  \global\LetLtxMacro\generic@@section\paragraph
  \or
  \global\LetLtxMacro\generic@@section\subparagraph
  \else
  Oopss!
  \fi
  \fi
  \IfBooleanTF{#1}{%  Is it the starred version?
    \generic@@section*{#3}%
  }{%
    \IfValueTF{#2}{%
      \generic@@section[#2]{#3}%
    }{%
      \generic@@section{#3}%
    }%
  }%
}


\NewDocumentCommand{\nextdepth}{som}{%
  \stepcounter{sectionlevel}%
  \IfBooleanTF{#1}{%
    \currentdepth*{#3}%
  }{%
    \IfValueTF{#2}{%
      \currentdepth[#2]{#3}%
    }{%
      \currentdepth{#3}%
    }%
  }%
}



\NewDocumentCommand{\previousdepth}{sD<>{-1}om}{%
  \addtocounter{sectionlevel}{#2}%
  \IfBooleanTF{#1}{%
    \currentdepth*{#4}%
  }{%
    \IfValueTF{#3}{%
      \currentdepth[#3]{#4}%
    }{%
      \currentdepth{#4}%
    }%
  }%
}

\AtBeginDocument{%
  \setcounter{sectionlevel}{0} %
}


\begin{document}
\tableofcontents
\currentdepth{Hello World}
\nextdepth{Hello World - Section}
\currentdepth{Hello World - Section - Again}
\currentdepth*{Hello World - Section - Again - but starred}
\nextdepth{Hello World - Subsection}
\previousdepth{A section again}
\previousdepth{A new chapter}

\nextdepth{Yet another section}
\nextdepth{Yet another subsection}
\nextdepth{Yet another subsubsection}
\previousdepth<-3>{And this is a ...?}


\end{document}

enter image description here

1

coseoul does this:

  • \levelstay{<title>} makes a new heading at the current level.

  • \leveldown{<title>} makes a new heading one level deeper.

  • \levelup{<title>} makes a new heading one level up.

  • \levelmultiup{<title>}{<levels>} Go up <levels> levels.

Actually, I've now published the modular package which builds on coseoul but supports true modularity.