In the same spirit as your first quoted answer and using etoolbox
's patchcmd
you can assume that \leftskip
is not used dor anything else, the magic is :
\patchcmd{\subsection}{-3.25ex\@plus -1ex \@minus -.2ex}{3.25ex\@plus -1ex \@minus -.2ex\setlength{\leftskip}{0cm}}{}{}
\patchcmd{\subsection}{1.5ex \@plus .2ex}{1.5ex \@plus .2ex\setlength{\leftskip}{2cm}}{}{}
\patchcmd{\section}{-3.5ex \@plus -1ex \@minus -.2ex}{-3.5ex \@plus -1ex \@minus -.2ex\setlength{\leftskip}{0cm}}{}{}
which switches off the shift at the beginning of the \section
and \subsection
, and switches it on when entering into \subsection
. Here is a MWE, in the which I changed the page size to fit the example on two pages:
\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage[margin=25mm]{geometry}
\makeatletter
\patchcmd{\section}{-3.5ex \@plus -1ex \@minus -.2ex}{-3.5ex \@plus -1ex \@minus -.2ex\setlength{\leftskip}{0cm}}{}{}
\patchcmd{\subsection}{-3.25ex\@plus -1ex \@minus -.2ex}{3.25ex\@plus -1ex \@minus -.2ex\setlength{\leftskip}{0cm}}{}{}
\patchcmd{\subsection}{1.5ex \@plus .2ex}{1.5ex \@plus .2ex\setlength{\leftskip}{2cm}}{}{}
\makeatother
\begin{document}
\setcounter{chapter}{3}
\section{Section Foo}
\lipsum[1]
\subsection{A subsection}\label{s:first}
\lipsum[2]
\subsection{Another subsection}
\lipsum[3-5]
\section{Section Bar}
\textbf{As seen in section \ref{s:first}}
\lipsum[5]
\subsection{Test again}
\lipsum[5]
\end{document}
with the result:

Note 1: this works also with hyperref
, provided you make the patch before loading it.
Note 2: This approach is likely not compatible with titlesec
. It assumes that the sectioning commands (below \chapter
) are based on the standard \@startsection
command. This is not an issue as you can also patch in a similar way the format of the title for getting italic
Note 3: To determine the value of the second argument of \patchcmd
(which depends on the class and ptsize) you have to look at the content of the sectionng commands. For this purpose I use a custom command :
\newcommand{\mymeaning}[1]{{\small\noindent{\bfseries \string #1} = \meaning #1\par\medskip}}
based on the standard command \meaning
with some cosmetic, and do e.g. \mymeaning{\subsection}
in the document.