有没有办法自动添加\baselineskip
后\section
仅当该部分从页面的最顶部开始时才给定(请参阅下面的 MWE)?
注意:MWE 不包括\titlesec
。对于包含\titlesec
,请参阅对于使用 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}
答案1
如果的位置\section*{
高于某个确定的阈值(从底部计算,如zref-savepos
包中所指定),则这会增加额外的垂直空间。阈值可能需要随意更改以满足需要。
\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}