# How can I make \titlespacing not omit the top spacing?

In a multicols*{2} environment for 12pt linehight text, I use

\titlespacing*{\subsection}{0pt}{12pt}{12pt}


To create exactly 12pt of space before and after subsections. Together with a line height of 12pt for the subsection font this creates 36pt-height subsections, which covers 3 lines of ordinary text.

This works fine anywhere on the page but not if a subsections happens to be on the top of a column, where the top-spacing is automatically omitted. I want to enforce 36pt total height wherever the section happens to be.

How can I make \titlespacing not omit the top spacing at the top of a column?

Update 2:

I edited the question to use 12pt, as the suggested solutions seem to not cover all cases so far or introduce slightly wrong gaps (+/- ~2pt) that are not easily visible if too artificial pointsizes are used. The following MWE now uses 12pt lines and covers all three relevant cases: headline within column, on top of column 1 and on to on column 2.

\documentclass[12pt]{article}

\usepackage{lipsum}
\usepackage{multicol}

\usepackage[compact]{titlesec}

\titleformat{\subsection}
{\fontsize{10pt plus 0pt minus 0pt}{12pt plus 0pt minus 0pt}\selectfont\bf} % format
{} % label
{0pt} %sep
{} %after

\titlespacing*{\subsection}{0pt}{12pt}{12pt}

\begin{document}

\fontsize{10pt}{12pt}\selectfont

\begin{multicols}{2}\raggedcolumns

\lipsum[1-2]
\columnbreak
\lipsum[1]
\subsection*{Good Section}
\lipsum[2]

\end{multicols}

\newpage

\begin{multicols}{2}\raggedcolumns

\lipsum[1]
\subsection*{Good Section}
\lipsum[2]
\columnbreak
\lipsum[1-2]

\end{multicols}

\end{document}

-
A MWE would be hepful (you have 500 rep. so I suppose you know what does it mean). – yo' Jan 12 '13 at 12:58
Added a MWE ... – TeXter Jan 12 '13 at 13:43
So you'd like a section title leave some space no matter what? – egreg Jan 12 '13 at 13:54
Yes. While in the MWE it does not look that bad, in my real document/layout with more styled section headers and page headers they look odd if tucked right at the top. – TeXter Jan 12 '13 at 14:07

You may add \vspace{-1cm}\vspace*{1cm} in the proper argument of \titleformat -- the two spaces will offset each other except at the top of a page. This is simpler than tohecz's "box" solution and, unlike egreg's solution, will not result in additional vertical space between a \section and a directly adjoining \subsection. (Note that in this case the \section's "after" space will override the \subsection's "before" space.)

EDIT: For whatever reason, it seems one must also subtract \baselineskip in \vspace*(and re-add it in \vspace).

EDIT2: This is the best I can come up with (a hack for which I lack an explanation):

1. Within \titleformat, the difference between fonsize and \baselineskip (in your revised example: 2pt) must be subtracted with \vspace and re-added with \vspace*.

2. For any \subsection immediately after \begin{multicols}, the value of \baselineskip (12pt) must be subtracted with \vspace* in the document body.

\documentclass[12pt]{article}

\usepackage{lipsum}
\usepackage{multicol}

\usepackage[compact]{titlesec}

\titleformat{\subsection}
{\vspace{\dimexpr -12pt+10pt}\vspace*{\dimexpr 12pt-10pt}%
\fontsize{10pt plus 0pt minus 0pt}{12pt plus 0pt minus 0pt}\selectfont\bf} % format
{} % label
{0pt} %sep
{} %after

\titlespacing*{\subsection}{0pt}{12pt}{12pt}

\begin{document}

\fontsize{10pt}{12pt}\selectfont

\begin{multicols}{2}\raggedcolumns

\vspace*{-12pt}

\lipsum[1-2]
\columnbreak
\lipsum[1]
\subsection*{Good Section}
\lipsum[2]

\end{multicols}

\newpage

\begin{multicols}{2}\raggedcolumns

\lipsum[1]
\subsection*{Good Section}
\lipsum[2]
\columnbreak
\lipsum[1-2]

\end{multicols}

\end{document}

-
I would prefer this solution, but in addition to the intended extra space also one full extra (empty) line seems to be inserted on the second page right in front of the subsection. I don't know where this comes from... it is easy to see if you use 1pt instead of 1cm... – TeXter Jan 12 '13 at 16:42
@TeXter For whatever reason, adding (and subtracting) also a \baselineskip seems to do the trick. – lockstep Jan 12 '13 at 16:59
I found an unfortunate bug in this solution. It works on the top of the first column, but not if the (sub)section happens to fall at the top of the second column (e.g. after a \columnbreak for example). In this case some but not all additional space seems to get added. – TeXter Jan 14 '13 at 10:07
@TeXter Right now, I've no idea why this is so. :-( – lockstep Jan 14 '13 at 10:18
I un-accepted the solution and updated the first post for a better MWE. – TeXter Jan 14 '13 at 11:05

You should insert the "vertical space" in the section title, but not as a skip. Rather you have to use an invisible rule; here's for \subsection, define \section as you prefer.

However I don't see how this can be an improvement of the typographic quality of your document, particularly if the skip is not as big as the one used here for the example (1cm).

\documentclass{article}

\usepackage{lipsum}
\usepackage{multicol}

\usepackage{titlesec}

\titleformat{\subsection}
[block]
{\normalsize\bfseries}
{\thesubsection}
{\vrule height\dimexpr\ht\strutbox+1cm\relax width 0pt depth 0pt }
\titlespacing*{\subsection}{0pt}{0pt}{1cm}

\begin{document}\raggedcolumns

\begin{multicols}{2}

\lipsum[1]
\subsection*{Good section}
\lipsum[2-3]

\end{multicols}

\newpage

\begin{multicols}{2}\raggedcolumns

\lipsum[1-3]

\end{multicols}

\end{document}


-
This seems to result in (unwanted) additional vertical space between a \section and a directly adjoining \subsection. – lockstep Jan 12 '13 at 14:34
It depends on the "real" implementation of the commands, which the OP doesn't show. – egreg Jan 12 '13 at 14:36
Hmm, this solution seems to loose ~1.5pt somewhere. If I use the example with a lineheight of 12pt (both surrounding text and subsection font, and use 6pt instead of 1cm for the rule/spacing, I would assume that the subsection-title takes up exactly 2 lines of text, but there is a slight offset of lines of ~1.5 pt after the section when compared to the text in the right column. Happens both on the good and the bad section... – TeXter Jan 12 '13 at 16:52

The solution would be to enclose each \subsection into \vbox like:

\vbox{\subsection*{Bad section}}


If you want to do it automatically, this is possible, but you have to repeat the trick for each sectioning command that you want to use this way (\section, \subsection, ...) and each variant (starred, with optional argument, without optional argument:

\documentclass[a4paper]{article}

\usepackage{lipsum}
\usepackage{multicol}

\usepackage{sectsty}
\allsectionsfont{\normalsize\bfseries}

\usepackage[compact]{titlesec}
\titlespacing*{\subsection}{0pt}{1cm}{1cm}

\makeatletter
%section
\let\x@section\section
\def\xs@section*#1   {\vbox{\x@section*{#1}}}
\def\xb@section[#1]#2{\vbox{\x@section[#1]{#2}}}
\def\xx@section#1    {\vbox{\x@section{#1}}}
\def\section{\@ifnextchar*\xs@section{\@ifnextchar[\xb@section\xx@section}}
%subsection
\let\x@subsection\subsection
\def\xs@subsection*#1   {\vbox{\x@subsection*{#1}}}
\def\xb@subsection[#1]#2{\vbox{\x@subsection[#1]{#2}}}
\def\xx@subsection#1    {\vbox{\x@subsection{#1}}}
\def\subsection{\@ifnextchar*\xs@subsection{\@ifnextchar[\xb@subsection\xx@subsection}}
\makeatother

\begin{document}\raggedcolumns

\begin{multicols}{2}

\lipsum[1]
\subsection*{Good section}
\lipsum[2-3]

\end{multicols}

\newpage

\begin{multicols}{2}\raggedcolumns