# Different vertical spacing (parskip) between concurrent section titles and paragraphs

I'm trying to remove the spacing between section headings, but maintain the paragraph to section heading spacing.

Even though I set titlespacing before and after to *0, there is still spacing - quite a bit more in the case of \section.

I've tried a number of things, getting close with setting titlespacing{\<eachone>}{*0}{-5.8pt}, but it's not complete and has a number of problems. Removes vertical space to the first paragraph and section still has more space than expected.

Here's what I have:

And what I'm looking for:

\documentclass{scrbook}
\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}


Possibly related question

• I am confused. Are we allowed to wager more reputation than our balance, or is Mr. Clark attempting to set a coveted stackexchange record to be the first user with negative reputation? – Steven B. Segletes Mar 11 '13 at 12:26
• The bounty points have already been deducted from his reputation, cf. tex.stackexchange.com/privileges/set-bounties – bencarabelli Mar 11 '13 at 12:38

In response to Mr. Clark's stipulation that he didn't like the \vskips, those have been successfully removed. Further, all spacings are issued through the titlesec package and are now specified in terms of \baselineskip, which should provide more flexigility for fontsize changes. Significant changes in this edit were several. First, changing the titlesec format from "display" to "hang" accomplished a great deal. However, the issue of \lineskiplimit proved to be decisive. The \boxedsection command created by Jeff was of a size not close to an even multiple of \baselineskip and so LaTeX kept adding a line to correct a perceived problem. Setting the line-skip-limit to a more negative value resolved the issue. And it allowed me to use just the titlesec parameters to achieve the result, without any vspaces.

Nonetheless, there remained the issue of glue, to which I alluded in one of the comments. I was largely able to remedy it by setting \parskip to 0 and setting \textfloatsep so that it couldn't grow (I'm not wholly sure I'm using that command properly). However, a super-zoom on the flushed section headings can still occasionally perceive some glue-gap. Maybe there's another glue parameter that can help.

In response to the comment that there probably needs to be no separate commands used for the flushed sub- and subsub-section headings, I have remedied that problem, with one proviso. The method works by counting how many \pars have been issued after the higher-level section invocation. It starts by assuming you have a flush-heading sub*section. Then it starts counting \pars. Once it reaches a critical number, it concludes that you have added text to the section , and changes the \sub*section heading for the next lower heading level. Therefore, the user must be consistent in how they add text to a section. Currently, the triggers are set up for the following structure

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


Thus, there remains the requirement for the user to type his section headings consistently. And because of the feature of texexchange to squeeze blank lines out of listings, the needed blank lines in the body do not show up in the below listing.

\documentclass{scrbook}
\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%
\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}


• I expect @Jeffrey will not like it that much, as one has to manually tell TeX, where it should not insert the free space after the headline by using  \flush(sub)*section{title} instead of \(sub)*section{title}. But apart from that it is a nice solution =) – Dominikus K. Mar 11 '13 at 16:30
• @DominikusK. Resolved, with a proviso. – Steven B. Segletes Mar 11 '13 at 18:24
• Interesting solution. The hard-coded negative \vspace is a problem. As in your screenshot, 1.2.1 gray fill overlaps (confirmed locally) the preceding \section. Any change in font, font size, or spacing in the tikz title, requires manual tweaking. – h0tw1r3 Mar 12 '13 at 0:54
• I guess I really don't understand why when I set titlespacing ...*0, there is still space. – h0tw1r3 Mar 12 '13 at 0:56
• @JeffreyClark I don't understand titlesec either, but there is a rigidchapters package option (as opposed to the default rubberchapters option). Don't know if it applies to things other than "chapters", but it might be a factor. – Steven B. Segletes Mar 12 '13 at 1:44