如何使标题缩进为段落的第一行?

如何使标题缩进为段落的第一行?

我希望章节和(子)节标题的缩进量与段落第一行的缩进量一样多(\parindent值)。

默认情况下,使用 koma-script 我使它们与页面的左边界对齐: 在此处输入图片描述

期望的效果如下: 在此处输入图片描述

我使用的 MWE 如下:

\documentclass{scrreprt}

\usepackage[english]{babel}
\usepackage{indentfirst}
\usepackage{lipsum}

\setlength{\parindent}{1cm} 

\begin{document}

\addchap{Chapter without number}
\addsec{Section without number}
\lipsum[1-4]

\chapter{Chapter with number}
\section{Section with number}
\lipsum[1-4]

\end{document}

答案1

需要对一些内部命令进行重新定义:

\documentclass[openany]{scrreprt}
\usepackage[a5paper]{geometry}
\usepackage[english]{babel}
\usepackage{indentfirst}
\usepackage{linegoal}
\usepackage{lipsum}

\newlength\MyIndent
\setlength\MyIndent{1cm}

\setlength\parindent{\MyIndent} 

\makeatletter
\renewcommand\section{\@startsection{section}{1}{\MyIndent}%
  {-3.5ex \@plus -1ex \@minus -.2ex}%
  {2.3ex \@plus.2ex}%
  {\ifnum \scr@compatibility>\@nameuse{scr@[email protected]}\relax
    \setlength{\parfillskip}{\z@ plus 1fil}\fi
    \raggedsection\normalfont\sectfont\nobreak\size@section}%
}
\renewcommand\subsection{\@startsection{subsection}{2}{\MyIndent}%
  {-3.25ex\@plus -1ex \@minus -.2ex}%
  {1.5ex \@plus .2ex}%
  {\ifnum \scr@compatibility>\@nameuse{scr@[email protected]}\relax
    \setlength{\parfillskip}{\z@ plus 1fil}\fi
    \raggedsection\normalfont\sectfont\nobreak\size@subsection
  }%
}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\MyIndent}%
  {-3.25ex\@plus -1ex \@minus -.2ex}%
  {1.5ex \@plus .2ex}%
  {\ifnum \scr@compatibility>\@nameuse{scr@[email protected]}\relax
    \setlength{\parfillskip}{\z@ plus 1fil}\fi
    \raggedsection\normalfont\sectfont\nobreak\size@subsubsection
  }%
}
\renewcommand\paragraph{\@startsection{paragraph}{4}{\MyIndent}%
  {3.25ex \@plus1ex \@minus.2ex}%
  {-1em}%
  {\raggedsection\normalfont\sectfont\nobreak\size@paragraph}%
}
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\MyIndent}%
  {3.25ex \@plus1ex \@minus .2ex}%
  {-1em}%
  {\raggedsection\normalfont\sectfont\nobreak\size@subparagraph}%
}
\renewcommand*{\@@makeschapterhead}[1]{%
  \chapterheadstartvskip%
  {\normalfont\sectfont\nobreak\size@chapter{}\hspace*{0cm}%
    \setlength{\parindent}{3cm}\setlength{\parfillskip}{\fill}%
    \raggedsection \interlinepenalty \@M\size@chapter{%
      \parbox[t]{\dimexpr\linewidth-\MyIndent\relax}{#1}}\par}%
  \nobreak\chapterheadendvskip%
}
\renewcommand*{\@@makechapterhead}[1]{\chapterheadstartvskip
  {%
    \hspace*{0pt}\setlength{\parindent}{\MyIndent}\setlength{\parfillskip}{\fill}%
    \normalfont\sectfont\nobreak\size@chapter{}%
    \if@chapterprefix
      \let\@tempa\raggedsection
    \else
      \let\@tempa\@hangfrom
    \fi
    \@tempa{\ifnum \c@secnumdepth >\m@ne%
          \if@chapterprefix
            \expandafter\size@chapterprefix
          \else
            \expandafter\size@chapter
          \fi
          {\chapterformat}%
          \if@chapterprefix
            \size@chapterprefix{}\endgraf\nobreak\vskip.5\baselineskip
          \fi
      \fi
    }%
    {\raggedsection \interlinepenalty \@M \size@chapter{%
      \parbox[t]{\linegoal}{#1}}\par}}%
  \nobreak\chapterheadendvskip
}
\makeatother

\setcounter{secnumdepth}{3}

\begin{document}

\addchap{Chapter without number}
\addsec{Section without number}
\subsection*{Subsection without number}
\subsubsection*{Subsubsection without number}
\lipsum[2]\lipsum[4]

\chapter{Chapter with number}
\section{Section with number}
\subsection{Subsection with number}
\subsubsection{Subsubsection with number}
\lipsum[2]\lipsum[4]

\end{document}

在此处输入图片描述

答案2

自 v3.15 起(最新版本为 v3.16)KOMA-Script 提供宏

  • \DeclareSectionCommand\RedeclareSectionCommand\DeclareNewSectionCommand\ProvideSectionCommand
  • \DeclareSectionCommands\RedeclareSectionCommands\DeclareNewSectionCommands\ProvideSectionCommands

其可用于通过键值接口改变分段命令的布局。贡萨洛的回答可以缩短如下:

\documentclass{scrreprt}

\usepackage[english]{babel}
\usepackage{indentfirst}
\usepackage{lipsum}
\usepackage{etoolbox}

\setlength{\parindent}{1cm} 

% a hack for the chapter commands:
\makeatletter
\patchcmd\@@makeschapterhead{\setlength{\parindent}{\z@}}{\hspace*{0pt}}{}{}
\patchcmd\@@makechapterhead{\setlength{\parindent}{\z@}}{\hspace*{0pt}}{}{}
\makeatother

% all other sectioning commands:
\RedeclareSectionCommands[indent=\the\parindent]{section,subsection,subsubsection,paragraph}

\begin{document}

\tableofcontents

\addchap{Chapter without number}
\addsec{Section without number}
\subsection*{Subsection without number}
\subsubsection*{Subsubsection without number}
\paragraph*{Paragraph without number}
\lipsum[2]\lipsum[4]

\chapter{Chapter with number}
\section{Section with number}
\subsection{Subsection with number}
\subsubsection{Subsubsection with number}
\paragraph{Paragraph with number}
\lipsum[2]\lipsum[4]

\end{document}

相关内容