如何编辑以下文档,使得章节、节和小节的上方和下方都有相等的垂直间距,如模拟照片(右)所示?
\documentclass[a4paper,12pt]{scrreprt}
\usepackage{lipsum}
\newlength{\secnumwidth}
\setlength{\secnumwidth}{5em}
\makeatletter
\def\@makechapterhead#1{%
\vspace*{50\p@}%
{\interlinepenalty\@M
\parindent \z@ \raggedright \sffamily
\huge\bfseries
\ifnum \c@secnumdepth >\m@ne
\makebox[\secnumwidth][l]{\thechapter}%
\fi
#1\par\nobreak
\vskip 40\p@
}}
\renewcommand{\@seccntformat}[1]{\makebox[\secnumwidth][l]{\csname the#1\endcsname}}
\makeatother
\begin{document}
\chapter{A chapter}
\lipsum[2]
\section{A section}
\lipsum[2]
\subsection{A subsection}
\lipsum[2]
\bigskip
\end{document}
这是以下其中一个答案的结果,但正如您所见,其间距始终不一样。
答案1
1)推荐方式:
\documentclass[a4paper,12pt]{scrreprt}
\usepackage{lipsum}
\usepackage{tikz}%Just to check spaces
\newlength{\secnumwidth}
\setlength{\secnumwidth}{5em}
\RedeclareSectionCommand[beforeskip=4ex,
afterskip=4ex]{chapter}
\RedeclareSectionCommand[beforeskip=4ex,
afterskip=4ex]{section}
\RedeclareSectionCommand[beforeskip=4ex,
afterskip=4ex]{subsection}
\makeatletter
\def\@makechapterhead#1{%
\vspace*{2ex}%
{\interlinepenalty\@M
\parindent \z@ \raggedright \sffamily
\huge\bfseries
\ifnum \c@secnumdepth >\m@ne
\makebox[\secnumwidth][l]{\thechapter}%
\fi
#1\par\nobreak
\vskip 2ex
}}
\renewcommand{\@seccntformat}[1]{\makebox[\secnumwidth][l]{\csname the#1\endcsname}}
\makeatother
\begin{document}
\chapter{A chapter}
\lipsum[2]
\section{A section}
\lipsum[2]
\subsection{A subsection}
\lipsum[2]
\bigskip
%Just to check spaces
\begin{tikzpicture}[overlay,remember picture]
\draw ([shift={(-8cm,10.40cm)}]current page.center) rectangle ([shift={(-1cm,9.55cm)}]current page.center);
\draw ([shift={(-8cm,6.00cm)}]current page.center) rectangle ([shift={(-1cm,5.15cm)}]current page.center);
\draw ([shift={(-8cm,4.65cm)}]current page.center) rectangle ([shift={(-1cm,3.8cm)}]current page.center);
\draw ([shift={(-8cm,-1.05cm)}]current page.center) rectangle ([shift={(-1cm,-1.9cm)}]current page.center);
\draw ([shift={(-8cm,.25cm)}]current page.center) rectangle ([shift={(-1cm,-.6cm)}]current page.center);
\end{tikzpicture}
\end{document}
2)由于您的文档类不喜欢titlesec
包,因此不推荐,但在其他文档类中推荐:
\documentclass[a4paper,12pt]{scrreprt}
\usepackage{lipsum}
\newlength{\secnumwidth}
\setlength{\secnumwidth}{5em}
\usepackage{titlesec}
\usepackage{lipsum}% just to generate text for the example
\usepackage{titlesec}
\usepackage{lipsum}% just to generate text for the example
\titlespacing*{\chapter}
{0pt}{4.ex plus 0.1ex minus .1ex}{4.ex plus .1ex minus .1ex}
\titlespacing*{\section}
{0pt}{4.ex plus .1ex minus .1ex}{4.ex plus .1ex minus .1ex}
\titlespacing*{\subsection}
{0pt}{4.ex plus .1ex minus .1ex}{4.ex plus .1ex minus .1ex}
%\makeatletter
%\def\@makechapterhead#1{%
%\vspace*{50\p@}%
%{\interlinepenalty\@M
%\parindent \z@ \raggedright \sffamily
%\huge\bfseries
%\ifnum \c@secnumdepth >\m@ne
% \makebox[\secnumwidth][l]{\thechapter}%
%\fi
% #1\par\nobreak
%\vskip 40\p@
%}}
%\renewcommand{\@seccntformat}[1]{\makebox[\secnumwidth][l]{\csname the#1\endcsname}}
%\makeatother
\begin{document}
\chapter{A chapter}
\lipsum[2]
\section{A section}
\lipsum[2]
\subsection{A subsection}
\lipsum[2]
\bigskip
\end{document}