[Tex/LaTex] program that allows to move sections and automatically adjusts the level of subsections

sectioning

How would one go about to move a [sub..]section to a different position in the document structure?
The sectioning commands would have to be changed according to the new parent level.
Is there a program that does that? An editor via drag and drop?
A cli call like this should do it (given that the section numbering is default):

texsectionmove main.tex 3.2 root

Best Answer

As I interpret it, None is asking for something that looks like a directory structure: we can move folders (sections/subsections...) around in an arbitrary way, and LaTeX should know whether to put \section, \subsection, or whatever else. A partial solution, that works well with copying and pasting within the document is to use a section-like environment instead of just a macro.

\documentclass{article}

\makeatletter
\newcounter{section@depth}
\setcounter{section@depth}{0}

\newenvironment{deepsection}{%
  \addtocounter{section@depth}{1}%
  \ifcase\c@section@depth 
  \expandafter \part
  \or \expandafter \section
  \or \expandafter \subsection
  \or \expandafter \subsubsection
  \or \expandafter \paragraph
  \or \expandafter \subparagraph
  \else 
  \PackageError{deepsections}{%
    Sections are too deeply nested.%
  }{%
    Trying to recover with \string\subparagraph%
  }%
  \expandafter \subparagraph
  \fi
}{%
  \addtocounter{section@depth}{-1}%
}

\makeatother

\begin{document}
\tableofcontents
\begin{deepsection}{First section's title}  
  This text is inside the first section
  \begin{deepsection}[Short title]{And the title for a subsection}
    Some more text.
    \begin{deepsection}*{Unnumbered subsubsection}
      Yet some more text
    \end{deepsection}
    \begin{deepsection}{Other subsubsection}
      This subsubsection is numbered now.
    \end{deepsection}
  \end{deepsection}
\end{deepsection}

\begin{deepsection}{Anoter topic}
  Text.
\end{deepsection}
\end{document}

The deepsection environment defined there takes exactly the same arguments as \section, and chooses the relevant level of sectioning for you. It will also complain if you are going deeper than you should.

Related Question