2

I have some sections that have no direct text (other than subsections). For these blank sections, I am trying to make the subsection text (or heading) start at the same place as those sections that do have text. This seems to be specific to using leftmargin formatting of the title.

Here is an MWE:

\documentclass[a4paper]{article}
\usepackage{titlesec,lipsum}

\titleformat{\section}[leftmargin]
{}
{}
{0em}
{}

\titlespacing{\section}
{0cm}{1cm}{2cm}

\begin{document}

\section{section with text}
Section text
\subsection{subsection}
\lipsum[2]

\section{section with no text}
\subsection{subsection}
\lipsum[2]

\end{document}

The following image shows what I mean. I am trying to make "2.1 subsection" start at the same relative place as "section text". (In this particular document it does not make sense, but my particular document has a lot more formatting.)

example page

abeverley
  • 609
  • Related: http://tex.stackexchange.com/questions/101545/different-vertical-spacing-parskip-between-concurrent-section-titles-and-parag – Steven B. Segletes Apr 06 '14 at 23:38
  • Oh man, I was hoping it would be simpler than that ;-) Thanks, didn't find that question during my searches, will have a look and see if something in there works for me. – abeverley Apr 07 '14 at 06:48

1 Answers1

3

This works.

Just remember to leave a blank line before a \subsection when it doesn't start the section, e.g.

\section{name}
text

\subsection{name}

and the opposite when it is the first thing in the section, e.g.

\section{name}
\subsection{name}

MWE:

\documentclass[a4paper]{article}
\usepackage{titlesec}
\usepackage{lipsum}

\newcounter{mycounter}

\let\oldpar\par
\def\par{%
  \oldpar%
  \stepcounter{mycounter}%
}

\newcommand\subsecskip{%
  \ifnum\value{mycounter}<2
     \unskip%
     \vspace{-\baselineskip}%
  \fi
}

\titleformat{\section}[leftmargin]
{}
{}
{0em}
{\setcounter{mycounter}{0}}

\titlespacing*{\section}
{0cm}{1cm}{2cm}

\titleformat{\subsection}[hang]
{\subsecskip\normalfont\large\bfseries}
{\thesubsection}
{1em}
{}

\begin{document}
\section{section with text}
some text

\subsection{subsection}
\lipsum[2]

\section{section with no text}
\subsection{subsection}
\lipsum[2]

\end{document} 

Output:

enter image description here

karlkoeller
  • 124,410
  • Thanks, that works, except it goes wrong as soon as I put a description list in. All the paragraphs in a new section after the list are shifted up regardless of whether they are after an empty section or not. Redefining \par after the list does not help. Any ideas? – abeverley Apr 07 '14 at 15:23
  • Okay, redefining \par (again) after the start of the next section fixes the problem above, but is there a better way of fixing this? Looks like description is reverting par to its normal definition? – abeverley Apr 07 '14 at 15:31
  • @abeverley I'm sorry but that's the best I can do. Considering all the possibilities will become too difficult. – karlkoeller Apr 08 '14 at 04:25
  • No worries @karl - that's certainly a pretty good solution nonetheless. Thanks. – abeverley Apr 09 '14 at 04:45