在社区的帮助下,我找到了一个自定义sections
字幕的解决方案。TeX 似乎对section
-title 进行了一些关于如何处理分页符的广泛处理。
\documentclass[a4paper,11pt]{article}
\usepackage[margin=2.5cm]{geometry} % for margins on a A4paper
\usepackage[tracking=true,factor=1100,stretch=10,shrink=10,final]{microtype} % have some better layout of individual chars
\usepackage{fontspec}
\usepackage[shorthands=off,bidi=basic]{babel}
\usepackage{xcolor} % for highlighting AND background images-color!!!
%\usepackage[defaultlines=3,all]{nowidow} % prevents orphans and widows
\usepackage{widows-and-orphans}
\usepackage{setspace}
\onehalfspacing
\usepackage{parskip}
\usepackage[raggedright]{titlesec} % to have the section NOT justify, i.e. the lines can be shorter (for linebreak)
\usepackage{keyval}
% Set formats for each heading level
\titleformat*{\section}{\normalsize\bfseries\color{gray}}
\titleformat*{\subsection}{\normalsize\bfseries\color{gray}}
\titleformat*{\subsubsection}{\normalsize\bfseries\color{gray}}
\titlespacing*{\section}{0pt}{*3}{0pt}
\titlespacing*{\subsection}{0pt}{*3}{0pt}
\titlespacing*{\subsubsection}{0pt}{*3}{0pt}
\makeatletter
\define@key{chk}{subtitle}{\def\chk@subtitle{#1}}
\newcommand\sectionLvl[3][]{%
{ \setkeys{chk}{subtitle={},#1}%
\normalsize\bfseries\color{gray}%required for deeper level
\ifcase#2\relax\expandafter\chapter\or%section
\relax\expandafter\section\or%subsection
\relax\expandafter\subsection\or%subsubsection
\expandafter\subsubsection
\fi
[#3]{#3%check if subtitle exists
\ifx\chk@subtitle\@empty\relax\else%add subtitle
\nopagebreak\endgraf\vspace{-0.5\baselineskip}
\parbox[t]{\linewidth}{%
\raggedright\normalfont\sffamily%font of subtitel
\textcolor{gray}{\chk@subtitle}%subtitle itself
}\endgraf\vspace{-\baselineskip}%
\fi%end of subtitle
}
}}
\makeatother
\usepackage{kantlipsum}
\begin{document}
\sectionLvl[subtitle={Test Subtitle}]{1}{first Kant}
\kant[1-3]
\vspace{4.5cm}
\sectionLvl[subtitle={Test Subtitle}]{1}{second Kant}
\kant[1-4]
sectionLvl[subtitle={Test Subtitle}]{1}{third Kant}
\kant[1-4]
\end{document}
通过上述解决方案,我现在面临的问题是section
(带有其副标题)将成为寡妇。
如果我想防止这种情况发生,我想我需要使用一个包。我搜索了有关如何防止寡妇的提示,找到了两个主要包:nowidow
和widows-and-orphans
。使用widows-and-orphans
我没有成功。虽然nowidow
看起来很旧,但它正在工作(通过使用defaultlines=3
和额外的\nopagebreak
)。副作用是我现在至少有 3 行。
有没有办法既能坚持至少 2 行,又能避免一些不必要的分页符?