[Tex/LaTex] Titlesec: remove space after empty margin section

sectioningspacingtitlesec

I'm using titlesec to assign some custom formatting to my sections. In particular I want to use margin \section's, so that the title section is typeset on the left margin, and the text of that section starts at the same height as the tile.

But I'm having a little problem with the spacing: when a \section is immediately followed by a \subsection, an unwanted vertical space is added and the title of the subsection does not align anymore with the title of the section (see code and picture bellow). How could I fix this?

\documentclass{article}
\usepackage{lipsum}
\usepackage{titlesec}
\titleformat{\section}[leftmargin]{\raggedright\scshape}{}{0pt}{}
\titlespacing*{\section}{2.5cm}{*2.5}{0.5cm}
\titleformat{\subsection}{\bfseries}{}{0pt}{}
\titlespacing*{\subsection}{0pt}{*2}{*1}
\begin{document}
\section{First}
\lipsum[1]
\section{Second}
\subsection{This is not nice}
\lipsum[1]
\subsection{This is fine}
\lipsum[1]
\end{document}

titlesec spacing bug

Update: As I really need an answer to this (I'm using it in some document that I should deliver soon), I'll offer a bounty to anybody who can bring me an answer.

For whoever decides to tackle this issue, I've navigated a bit into the source from titlesec and found some hackish way to get the desired result. It involves suppressing some \leavevmode, delaying an assignment to some spacing parameter after the subsection is typeset, and making titlesec to typeset the problematic \subsection header directly into the document and not in a “box” for positioning. This regrettably,
also causes problems with the indentation of the following paragraph.

I would love to see a solution simpler than this. Conceptually what I want to do is very simple: after the \section somehow tell titlesec to suppress any vertical spacing that would be inserted before the next \subsection. So I still hope that some simple solution exists but I'm missing it because I have not much experience on spacing, vertical/horizontal modes and how paragraphs are typeset.

Even if the solution has to be more complicated, the ideal would work well together with titlesec and do not break any of its other options. However, in the end I would also settle for a lesser solution which patches the definition of \subsection and makes it work at least in my example and my intended configuration given above.

Best Answer

This is a very interesting layout. Try the following code. A boolean is hooked to the \everypar. This means that if there is plain paragraph first it is set to false and the next subsection will have normal spacing. Note also the strange place where the \secskip command end up, but it seems to work.

EDIT 1: Restore original \everypar and remove vertical space of \lastskip before moving subsection up. Remove all glue and add \raggedbottom to prevent stretching.

EDIT 2: It looks if the additional vertical spacing is \baselineskip

\usepackage{titlesec}
\makeatletter
\newif\ifaftersec\aftersecfalse

\newcommand\setsubskip{%
    \global\aftersectrue
    \everypar{%
        \global\aftersecfalse
        \if@noskipsec
            \global\@noskipsecfalse
            \clubpenalty\@M
            \hskip-\parindent
            \begingroup
                \@svsechd\unskip{\hspace{\@tempskipb}}%
            \endgroup
        \else
            \clubpenalty\@clubpenalty\everypar{}%
        \fi}}

\newcommand\subskip{%
  \ifaftersec
     \removelastskip%         EDIT 2
     \vspace{-\baselineskip}% EDIT 2 ??????????????
  \fi
  \global\aftersecfalse}

\titleformat{\section}[leftmargin]{\raggedright\scshape}{}{0pt}{}[\setsubskip]
\titlespacing*{\section}{2.5cm}{2.5ex}{0.5cm}

\titleformat{\subsection}{\subskip\bfseries}{}{0pt}{}[]
\titlespacing*{\subsection}{0pt}{2ex}{1ex}

\raggedbottom
\makeatother

enter image description here

Related Question