我正在尝试删除章节标题之间的间距,但保留段落与章节标题之间的间距。
即使我将titlespacing
前后设置为*0
,仍然会有间距——在 的情况下间距会大得多\section
。
我尝试了很多方法,设置得比较接近titlespacing{\<eachone>}{*0}{-5.8pt}
,但还不完整,而且有很多问题。删除第一个段落和部分的垂直空间仍然比预期的多。
以下是我所拥有的:
我正在寻找:
\documentclass{scrbook}
\usepackage[letterpaper,asymmetric,left=108pt,right=54pt,top=41pt,bottom=41pt,headsep=13pt,headheight=14pt,footskip=23pt,marginparwidth=72pt]{geometry}
\usepackage[compact,explicit,calcwidth]{titlesec}
\usepackage{tikz,lipsum}
\usetikzlibrary{calc}
%
\usepackage{setspace}\onehalfspacing
%
\setkomafont{sectioning}{\normalfont}
%
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
%
\newlength\titlemarginwidth
\setlength\titlemarginwidth{54pt}
%
\newcommand\boxedsection[6]{{%
\usekomafont{sectioning}\usekomafont{#2}%
\begin{tikzpicture}[inner sep=0, inner ysep=0.7ex, node distance=0]%
\node[anchor=base west,rectangle,text width=\titlemarginwidth-\columnsep-1pt,color=#3] at (0,0) (numnode) {\vphantom{yZ}\hspace{1ex}#6};%
\node[anchor=base west,rectangle,text width=\textwidth-.5pt] at ($(numnode.base east)+(\columnsep,0)$) (textnode) {\vphantom{yZ}#1};%
\begin{pgfonlayer}{background}%
\fill[color=#4] (0,0|-numnode.north) rectangle (numnode.south east|-textnode.south east);%
\draw[#5,semithick] (0,0|-numnode.north) -- (textnode.north east);%
\draw[#5,semithick] (0,0|-textnode.south west) -- (textnode.south east);%
\end{pgfonlayer}%
\end{tikzpicture}%
}}%
%
\titlespacing*{\chapter}{-0.7\titlemarginwidth}{50pt}{40pt}
\titleformat{\chapter}[display]%
{\usekomafont{sectioning}\usekomafont{chapter}}%
{\emph{\chaptertitlename\ \thechapter}}%
{0em}%
{\emph{#1}}%
%
\titlespacing*{\section}{-\titlemarginwidth}{*0}{*0}
\titleformat{\section}[display]
{\setstretch{1}}%
{}{0pt}{\boxedsection{#1}{section}{white}{black}{black}{\thesection}}%
%
\titlespacing*{\subsection}{-\titlemarginwidth}{*0}{*0}
\titleformat{\subsection}[display]
{\setstretch{1}}%
{}{0pt}{\boxedsection{#1}{subsection}{black}{gray!30}{black}{\thesubsection}}%
%
\titlespacing*{\subsubsection}{-\titlemarginwidth}{*0}{*0}
\titleformat{\subsubsection}[display]
{\setstretch{1}}%
{}{0pt}{\boxedsection{#1}{subsubsection}{black}{white}{black}{\thesubsubsection}}%
%
\begin{document}
\chapter{Test}
\section{Section}
\lipsum[2]
\section{Section}
\subsection{Subection}
\subsubsection{Subsubection}
\lipsum[2]
\end{document}
答案1
对最新编辑的评论:
为了回应克拉克先生不喜欢\vskip
s 的规定,这些 s 已被成功删除。此外,所有间距都是通过titlesec
包发出的,现在以 为单位指定\baselineskip
,这应该为字体大小的更改提供更大的灵活性。这次编辑中有几个重大变化。首先,将 titlesec 格式从“display”更改为“hang”,这取得了很大的成就。然而, 的问题\lineskiplimit
被证明是决定性的。Jeff\boxedsection
创建的命令的大小不接近 的偶数倍\baselineskip
,因此 LaTeX 不断添加一行来纠正一个可察觉的问题。将 line-skip-limit 设置为更负的值解决了这个问题。它允许我仅使用参数titlesec
来实现结果,而无需任何 vspaces。
尽管如此,仍然存在粘合问题,我在其中一条评论中提到了这一点。我基本上可以通过将其设置\parskip
为 0 并\textfloatsep
使其无法增长来解决这个问题(我不确定我是否正确使用了该命令)。但是,在刷新的节标题上进行超级缩放仍然偶尔会感觉到一些粘合间隙。也许有另一个粘合参数可以提供帮助。
对早期编辑的评论:
针对可能不需要为刷新的子节和子子节标题使用单独命令的评论,我已经解决了该问题,但有一个条件。该方法通过计算\par
在更高级别的节调用之后发出了多少个 s 来工作。它首先假设您有一个刷新标题的子*节。然后它开始计数\pars
。一旦达到临界数字,它就会得出结论,您已将文本添加到节中,并将标题更改\sub*section
为下一个较低标题级别。因此,用户在向节中添加文本的方式必须保持一致。目前,触发器设置为以下结构
\section{name}
<blank line>
text
<blank line>
\subsection{name}
<blank line>
text
<blank line>
\subsubsection{name}
或用于平齐标题
\section{name}
<blank line>
\subsection{name}
<blank line>
\subsubsection{name}
因此,仍然需要用户一致地输入其章节标题。并且由于 texexchange 会从列表中挤出空行的功能,正文中所需的空行不会显示在下面的列表中。
\documentclass{scrbook}
\usepackage[letterpaper,asymmetric,left=108pt,right=54pt,top=41pt,bottom=41pt,headsep=13pt,headheight=14pt,footskip=23pt,marginparwidth=72pt]{geometry}
\usepackage[compact,explicit,calcwidth]{titlesec}
\usepackage{tikz,lipsum}
\usetikzlibrary{calc}
\usepackage{ifthen}
%
\usepackage{setspace}\onehalfspacing
%
\setkomafont{sectioning}{\normalfont}
%
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
%
\newlength\titlemarginwidth
\setlength\titlemarginwidth{54pt}
%
\newcommand\boxedsection[6]{{%
\usekomafont{sectioning}\usekomafont{#2}%
\begin{tikzpicture}[inner sep=0, inner ysep=0.7ex, node distance=0]%
\node[anchor=base west,rectangle,text
width=\titlemarginwidth-\columnsep-1pt,color=#3] at (0,0) (numnode)
{\vphantom{yZ}\hspace{1ex}#6};%
\node[anchor=base west,rectangle,text width=\textwidth-.5pt] at
($(numnode.base east)+(\columnsep,0)$) (textnode) {\vphantom{yZ}#1};%
\begin{pgfonlayer}{background}%
\fill[color=#4] (0,0|-numnode.north) rectangle (numnode.south
east|-textnode.south east);%
\draw[#5,semithick] (0,0|-numnode.north) -- (textnode.north
east);%
\draw[#5,semithick] (0,0|-textnode.south west) -- (textnode.south
east);%
\end{pgfonlayer}%
\end{tikzpicture}%
}}%
\newcommand\flushboxedsection[6]{{%
\usekomafont{sectioning}\usekomafont{#2}%
\begin{tikzpicture}[inner sep=0, inner ysep=0.7ex, node distance=0]%
\node[anchor=base west,rectangle,text
width=\titlemarginwidth-\columnsep-1pt,color=#3] at (0,0) (numnode)
{\vphantom{yZ}\hspace{1ex}#6};%
\node[anchor=base west,rectangle,text width=\textwidth-.5pt] at
($(numnode.base east)+(\columnsep,0)$) (textnode) {\vphantom{yZ}#1};%
\begin{pgfonlayer}{background}%
\fill[color=#4] (0,0|-numnode.north) rectangle (numnode.south
east|-textnode.south east);%
\draw[#5,semithick] (0,0|-textnode.south west) -- (textnode.south
east);%
\end{pgfonlayer}%
\end{tikzpicture}%
}}%
%
\titlespacing*{\chapter}{-0.7\titlemarginwidth}{50pt}{40pt}
\titleformat{\chapter}[hang]%
{\usekomafont{sectioning}\usekomafont{chapter}}%
{\emph{\chaptertitlename\ \thechapter}}%
{0em}%
{\emph{#1}}%
\titlespacing*{\section}{-\titlemarginwidth}{.6\baselineskip}{*0}%
\titleformat{\section}[hang]%
{\setstretch{1.6}}%
{}{0pt}{\boxedsection{#1}{section}{white}{black}{black}{\thesection}%
}
\def\tsubsectionbox{%
\titlespacing*{\subsection}{-\titlemarginwidth}{.8\baselineskip}{*0}%
\titleformat{\subsection}[hang]%
{}%
{}{0pt}{%
\boxedsection{##1}{subsection}{black}{gray!30}{black}{\thesubsection}}%
}
%
\def\fsubsectionbox{%
\titlespacing*{\subsection}{-\titlemarginwidth}{*0}{.2\baselineskip}%
\titleformat{\subsection}[hang]%
{\setstretch{1.38}}%
{}{0pt}{%
\flushboxedsection{##1}{subsection}{black}{gray!30}{black}{\thesubsection}}%
}
%
\newcounter{subparcounter}
\let\savesection\section
\def\section{\fsubsectionbox\setcounter{subparcounter}{0}\savesection}
%
\def\tsubsubsectionbox{%
\titlespacing*{\subsubsection}{-\titlemarginwidth}{.8\baselineskip}{*0}%
\titleformat{\subsubsection}[hang]%
{}%
{}{0pt}{%
\boxedsection{##1}{subsubsection}{black}{white}{black}{\thesubsubsection}}%
}
%
\def\fsubsubsectionbox{%
\titlespacing*{\subsubsection}{-\titlemarginwidth}{*0}{0\baselineskip}%
\titleformat{\subsubsection}[hang]%
{\setstretch{1.1}}%
{}{0pt}{%
\flushboxedsection{##1}{subsubsection}{black}{white}{black}{\thesubsubsection}}%
}
%
\newcounter{subsubparcounter}
\let\savesubsection\subsection
\def\subsection{\fsubsubsectionbox\setcounter{subsubparcounter}{0}%
\savesubsection}
%
\let\savepar\par
\def\par{%
\savepar%
\addtocounter{subsubparcounter}{1}%
\addtocounter{subparcounter}{1}%
\ifthenelse{5 = \value{subsubparcounter}}%
{\tsubsubsectionbox}%
{}%
\ifthenelse{5 = \value{subparcounter}}%
{\tsubsectionbox}%
{}%
}%
%
% SEE: http://www.tug.org/TUGboat/tb28-1/tb88bazargan.pdf
\lineskiplimit = -12pt % <--MAY NEED TO BE MADE MORE NEGATIVE!
\parskip 0ex
\setlength{\textfloatsep}{20pt plus 0pt minus 4pt}
%
\begin{document}
\chapter{Test}
\section{Section}
\lipsum[2]
text
\subsection{Subsection}
Text
\subsubsection{Subsubsection}
Text
\section{Section}
\subsection{Subsection}
\subsubsection{Subsubsection}
Text
\section{Section}
Text
\subsection{Subsection}
Text
\subsubsection{Subsubsection}
Text
\lipsum[2]
\section{Section}
\lipsum[2]
text
\subsection{Subsection}
Text
\subsubsection{Subsubsection}
Text
\section{Section}
\subsection{Subsection}
\subsubsection{Subsubsection}
Text
\section{Section}
Text
\subsection{Subsection}
Text
\subsubsection{Subsubsection}
Text
\lipsum[2]
\section{Section}
\lipsum[2]
text
\subsection{Subsection}
Text
\subsubsection{Subsubsection}
Text
\section{Section}
\subsection{Subsection}
\subsubsection{Subsubsection}
Text
\section{Section}
\subsection{Subsection}
Text
\subsubsection{Subsubsection}
Text
\lipsum[2]
\end{document}