[Tex/LaTex] How to align chapter, section, subsection with parindent

horizontal alignmentsectioning

Is there a way to solve that?

?

Thanks a lot!

Best Answer

Yes, it's possible; the section number is typeset by the macro \@seccntformat and it's just a matter of giving it a suitable redefinition: typeset the number in a fixed width box.

\documentclass{article}
\usepackage{indentfirst}
\usepackage{lipsum}

\makeatletter
\renewcommand{\@seccntformat}[1]{%
  \protect\makebox[\parindent][l]{\csname the#1\endcsname}%
}
\makeatother

\setlength\parindent{2.5pc}

\begin{document}

\section{Lorem ipsum}

\subsection{Aliquam vestibulum}

\lipsum[2]

\subsubsection{Aliquam vestibulum}

\lipsum[2]

\end{document}

enter image description here


When minitoc is loaded (which I don't recommend, I find it distracting and not really useful), a bizarre thing happens: the package unfortunately redefines a key macro without using \@seccntformat, which is utterly wrong. You can patch it, though, restoring the correct behavior.

\documentclass{article}
\usepackage{indentfirst}
\usepackage{minitoc}
\usepackage{etoolbox} % for \patchcmd
\usepackage{lipsum}

\makeatletter
\renewcommand{\@seccntformat}[1]{%
  \protect\makebox[2.5pc][l]{\csname the#1\endcsname}%
}
% patch the wrong definition of `\stc@sect`
\patchcmd{\stc@sect}
  {\edef\@svsec{\csname the#1\endcsname\hskip1em}}
  {\protected@edef\@svsec{\@seccntformat{#1}\relax}}
  {}{}
\makeatother

\setlength\parindent{2.5pc}

\begin{document}

\section{Lorem ipsum}

\subsection{Aliquam vestibulum}

\lipsum[2]

\subsubsection{Aliquam vestibulum}

\lipsum[2]

\end{document}