Take the 2-minute tour ×
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.

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.

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted
+150

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

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
add comment

Your Answer

 
discard

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.