2

Basically, I need to indent paragraphs after a subsection as shown here:

example

Basically, I need to indent the all the content after a subsection. I already have the proper formatting for the heading of the subsection as follows:

\documentclass[12pt]{report}
\usepackage{titlesec}    
\titleformat{\subsection}[block]{\bfseries\itshape\normalsize\hspace{2em}}{\thesubsection}{1em}{}
\begin{document}
\end{document}

I found a solution here: Indenting a whole paragraph

However, the problem is that in order to apply the solution I will need to manually add code before and after each subsection.

Another solution here: Indent every \subsubsection element

proposes using a new environment:

\documentclass{article}
\usepackage{changepage}

\usepackage{lipsum} 
\newenvironment{subs}
  {\adjustwidth{3em}{0pt}}
  {\endadjustwidth}
\begin{document}
\subsection{A subsection}


\lipsum[2]

\begin{subs}
\subsubsection{One}

\lipsum[3]

\subsubsection{Two}

\lipsum[4]

\end{subs}
\end{document}

However, this has the same problem: I would need to manually add \begin...\end of the environment for each subsection I have. Is there a better solution?

Mohammad
  • 123
  • 2
    Automation of this is hard to do because the sectioning commands are not environments (an unfortunate design choice in some ways). So as a result, you can't hook into the end of a section/subsection etc. which is what you would need to have this work. – Alan Munn Jan 29 '19 at 19:19

1 Answers1

3

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: enter image description here

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.

Jhor
  • 4,179
  • 1
    If you have chapters or part, ending a subsection you would have to apply to the corresponding command the same patch as for section, disabling the shift. I will soon provide the patch for the standard \chapter. An other option (for chaoter) would be to alter the clearpage command. – Jhor Jan 31 '19 at 07:48