TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

\subsection{This is not nice}
\subsection{This is fine}

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.

share|improve this question
up vote 5 down vote accepted

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



     \removelastskip%         EDIT 2
     \vspace{-\baselineskip}% EDIT 2 ??????????????




enter image description here

share|improve this answer
Thanks! This is looking very good! The small problem I have is that apparently other factors (such as the selected font and font size) also alter the size of the spacing and one has to guesstimate the value to put on the \vspace. I've got to go now but, if needed, I can post later a small example showing this problem. – Juan A. Navarro May 30 '11 at 17:25
@Juan A. Navarro: I have changed it a little bit but the \vspace value still needs to be measured. Can not figure out where it is added. – Danie Els May 31 '11 at 2:36
@Juan A. Navarro: It looks if the additional spacing is \baselineskip – Danie Els May 31 '11 at 4:28
Excellent! Works perfectly. I also had to add a \vspace{-\parskip} because I'm also using the parskip package; but other than that it works great with my settings. It would still be nice to have a version that works with stretchable spaces between pars and sections; but for the moment this is just what I need. Thanks! – Juan A. Navarro May 31 '11 at 11:46
Apparently, I can't award you the bounty just yet. But I'll do it as soon as the system allows me to do it. – Juan A. Navarro May 31 '11 at 11:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.