# Section indentation

I'd like to have each of my sections, subsections, etc indented similar to how code is normally nested:

int func() {
if (x) {
do this stuff
}
// more code here
}


I'm not trying to format code, I just visually really like using indentation to understand where a section ends. What's the best way to implement this? I've tried using adjustwidth but it won't support nested subsections. I'd like to stay away from nested \items because I'd like all the support that accompanies sectioning (table of contents, etc).

-

This is a very quick solution, and has a number of various limitations (especially when you start thinking about lists and different justification modes (centering, etc.)). But it may be enough to get you started. The trick is to adjust \leftskip, which is the glue inserted before each line of a paragraph.

In the code attached, you can always re-do the \setindents command if necessary if the indentation is messed up by something else.

\documentclass[a4paper]{article}
\usepackage{lipsum}

\newlength\secindent
\newlength\origleftskip
\setlength\secindent{4em}
\setlength\origleftskip{\leftskip}

\def\setindents#1{%
\leftskip=\dimexpr\origleftskip+#1\secindent\relax
}

\def\mysec{\setindents{0}\section}
\def\mysubsec{\setindents{1}\subsection}
\def\mysubsubsec{\setindents{2}\subsubsection}

\begin{document}

\mysec{foo}
\lipsum[2-3]

\mysubsec{bar}
\lipsum[4-5]

\mysec{baz}
\lipsum[6-7]

\mysubsubsec{blah}
\lipsum[8-9]

\end{document}


If necessary, you could write something like

\let\origsection\section
\def\section{\setindents{0}\origsection}


and so on to get the behaviour you're after without changing your current text.

A totally different approach might be something like the framed package, but then you lose the ability to have floats (and again, nesting might be tricky).

There's no real reason why adjustwidth from changepage couldn't nest. I've added it to the issue tracker.

These sorts of things are all rather tricky in regular LaTeX. (I'm hoping the new galley in LaTeX3 makes it trivial.)

-