# Is there a program that allows to move sections and automatically adjusts the level of subsections?

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

-
Sections only receive their numbers when the document is compiled, so there is no coherent sense of "section 3.2" in your source document. If you want to move a section, just move it and the number will change automatically. If you want a subsection to become a section, you just need to remove the 'sub' from the \subsection command. –  Alan Munn Jan 15 '11 at 16:30
I think what None wants is to move \subsection{blah} hjkj \subsubsection{bloh} hjkh \subsubsection{blih} to \section{blah} hjkj \subsection{bloh} hjkh \subsection{blih}. I would do that with some regular expression substitution. –  Jan Hlavacek Jan 15 '11 at 19:51
I can't remember, but I am pretty sure org-mode can do this. Maybe using it and only then exporting to LaTeX is a good idea? –  mbork Jul 1 '12 at 17:05

## 4 Answers

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.

-
Also see github.com/wspr/newsec . This type of thing comes up just often enough I think there's some merit in providing a maintained interface for it. That code is just experimentation, though. –  Will Robertson Jul 1 '12 at 14:00

I understand the original question as: how can I convert section A to a chapter and at the same time automatically convert all sub-sections of section A to sections, all subsubsections to subsections etc

In TexmakerX that should be possible, however I admit that I did not try it yet:

In the structure view which shows the sections of the document like a tree, you can use the context menu to indent or de-indent a section (and afaik) that should also "move" all the sub-sections of that section 1 level up or down.

-

The question, as it stands, could be asking two different things. One: "Let's say I want to make all my sections into subsections and knock every other structure command down a level too" This could be done with a little regex-fu, for example, with sed. The idea would be to just find and replace all instances of each structure command with the next lowest. You'd have to start at the bottom and go up. So let's say the lowest structure command you have is \subsubsection and you want to knock this down to \paragraph. So run each of these lines in a terminal:

sed s/'\\subsubsection{'/'\\paragraph{'/ file.tex
sed s/'\\subsection{'/'\\subsubsection{'/ file.tex


and so on. This could obviously be improved loads by running them all with one script, and the like, but you get the idea.

The other thing this question might be asking is "I want to move a whole section of text around within a document AND change its level. This isn't as easy. emacs auctex mode has a command for marking a whole section: C-c * and then you can cut it with C-w as per usual. Automatically having the section change its section level is harder, but then it's not clear it would be useful. Sometimes you want a section to stay a section when you move it, and sometimes you want it to become a subsection. So it's best to do that by hand. With emacs reftex you can do this by moving to the section of interest in the reftex menu and using C-< or C-> to move up or down a level respectively.

-

You can easily move and demote or promote a selection that includes sections and subsections in Lyx. The shortcut is Alt-P and then one of the arrow keys - left and right promote or demote and up and down to move the selection.

-