5

Is there a way to automatically add a \baselineskip after a given \section, only if that section starts at the very top of a page (see MWE below)?

Note: The MWE does not include \titlesec. For documents with \titlesec requirement, see Add vertical space after section only if at top of the page, for documents using titlesec

\documentclass{book}

\usepackage{kantlipsum}

\begin{document}

\chapter*{Chapter 1}
\section*{Section 1}
\kant[1]

\section*{Section 2}
\kant[2]

\clearpage

%% Is there a way to have the following behaviour, automatically?

\section*{Section 3}
\vspace{\baselineskip}
\kant[3]

\end{document}
5

This adds extra vertical space if the position of \section*{ is above some definite threshold (counted from bottom, as specified in the zref-savepos package. The threshold value might have to be changed at will to fit the needs.

\documentclass{book}


\usepackage{zref-savepos}
\usepackage{kantlipsum}
\usepackage{xpatch}%
\usepackage{zref}

\def\toptreshold{40000000}

\newlength{\ssectionaddskip}
\setlength{\ssectionaddskip}{\baselineskip}

\newcounter{dummysection}
\makeatletter
\xpretocmd{\@ssect}{\stepcounter{dummysection}\zsaveposy{starredsectionstart::\number\value{dummysection}}}{}{}

\xapptocmd{\@ssect}{%
  \ifnumgreater{%
    \zposy{starredsectionstart::\number\value{dummysection}}}{\toptreshold}{%
    \vspace{\ssectionaddskip}}{%
    %  Do nothing, since vertical position is lower than the treshold
  }}{}{}

\makeatother

\begin{document}

\chapter*{Chapter 1}
\section*{Section 1}
\kant[1]

\section*{Section 2}
\kant[2]

\clearpage

%% Is there a way to have the following behaviour, automatically?

\section*{Section 3}
\vspace{\baselineskip}
\kant[3]

\end{document}

enter image description here

  • This works perfectly. I am very grateful for it, and also coming to learn about packages zref-savepos and zref. I am assuming the part {\toptreshold}{\vspace{\ssectionaddskip}}{}}{}{} is where one would tweak the vertical space, just in case design specs would change? Thank you so much. – gsl Feb 12 '15 at 15:14
  • 1
    @gsl: Yes, the code is very compact, I will expand it a little bit... just a moment please ;-) The threshold value should be changed by \setlength{\ssectionaddtopskip}{...} to an appropiate value. If you use a \begingroup...\endgroup you could change the value even without backing up the old value. – user31729 Feb 12 '15 at 15:16
  • 1
    @gsl: Well, titlesec redefines a lot of section stuff actually – user31729 Feb 12 '15 at 15:55
  • @gsl: Yes, that's better because it would change your initial question here and I assume that the titlesec experts will provide an answer quickly – user31729 Feb 12 '15 at 15:58

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.