2

How does one edit the following document so that there is equal vertical spacing both above and below chapter, sections and sub-sections, as indicated in the mock-up photograph (right)? enter image description here

\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}

Here is the outcome with one of the answers below, but, as you can see, the spacing is not the same throughout.

enter image description here

  • You are right, I didn't tried so much accuracy in first try, but you have to measure the distances correctly. The "p" of the word "Chapter" reduces the real space and this is the main problem of your measurement above. – koleygr Sep 10 '18 at 15:50
  • I just noticed that in your original picture you are taking in account only the numbers of the sections (in general) and not the content of the title. If you really want this I can fix it. (But think about a header with a function etc) – koleygr Sep 10 '18 at 16:08
1

1) Recommended way:

\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)Not recommended because of your documentclass that doesn't like titlesec package but recommanded in other document classes:

\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}

Output: enter image description here

  • Sorry, during tests I removed (or commented out) some commands you possibly want... But you can ignore this change – koleygr Sep 10 '18 at 14:32
  • RuixiZhang: titlesec can not be used with this documentclass and I used it just in case someone else with other documentclass find this question. You should not use it. She the first case of the edited answer. This is the way (Reduced space from your commands too as @RuixiZhang mentioned) – koleygr Sep 10 '18 at 14:40
  • I've just tried this out and the spacing is not consistent. In my original post, I have uploaded a picture of the outcome with these methods so you can see it more clearly. – wrb98 Sep 10 '18 at 14:52
  • @wrb98: I changed your commands values to more accurate and added tikzpicture above the page with rectangles of height 0.85cm to make visible the spaces. – koleygr Sep 10 '18 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.