1

I'm trying to place section headers in a column on the left with the content in another column on the right. I found a a question with exactly that, however there's one problem with the solution. If a section begins with a subsection, there's a space before the subsection, preventing the section header and the section content (the subsection) from aligning vertically. I can't seem to fix this in either solution presented in the other question. Can anyone help figure this out please?

\documentclass[a4paper]{article}
\usepackage[showframe]{geometry}
\usepackage{titlesec}

\usepackage{lipsum}

\geometry{left=6cm,right=3cm} % 3cm for the sections
\titleformat{\section}[leftmargin]
  {\normalfont\bfseries\filright}
  {\thesection}
  {1em}
  {}
\titlespacing*{\section}
  {3cm}
  {2ex plus .2ex minus .2ex}
  {1sp}% should be 0, but it must be positive

\begin{document}

\lipsum[2]

\section{A title}

\subsection{A subsection}
\lipsum[3]

\section{A very very very very very long title}

\lipsum[3]

\end{document}
ajzafar
  • 13

1 Answers1

0

Here is a solution that requires you to manually mark a subsection that immediately starts after a section heading with a new command \instantsubsection:

\documentclass[a4paper]{article}
\usepackage[showframe]{geometry}
\usepackage{titlesec}

\usepackage{lipsum}

\geometry{left=6cm,right=3cm} % 3cm for the sections

\titleformat{\section}[leftmargin]
  {\normalfont\bfseries\filright}
  {\thesection}
  {1em}
  {}
\titlespacing*{\section}
  {3cm}
  {2ex plus .2ex minus .2ex}
  {1sp}% should be 0, but it must be positive

\titleformat{\subsection}
  {\normalfont\large\bfseries}
  {\thesubsection}
  {1em}
  {}
\titlespacing*{\subsection}
  {0pt}
  {3.25ex plus 1ex minus .2ex}
  {1.5ex plus .2ex}

\def\instantsubsection{%
  \leavevmode
  {\normalfont\large\bfseries \vskip-\baselineskip}%
  \vspace*{-3.25ex plus 1ex minus .2ex}%
  \subsection
}

\begin{document}

\lipsum[2]

\section{A title}

\instantsubsection{A subsection}
\lipsum[3]

\subsection{A subsection}
\lipsum[3]

\section{A very very very very very long title}

\lipsum[3]

\end{document}

The title format and spacing for \subsection are explicitly given (default values, taken from this answer) so that is becomes obvious where the magical values in \instantsubsection come from.

\instantsubsection first enters horizontal mode which basically starts a new paragraph line. It then sets the same font format as used in the subsection heading. What happens next is that the current dummy line is removed with a negative \vskip by \baselineskip, which is the distance between adjacent baselines (the "line height"). Another negative \vspace removes the vertical spacing inserted before every subsection. Now we can go on with the normal \subsection command.

If we hadn't changed the font format before, the subsection heading wouldn't start on the same baseline as the section heading in the margin because paragraph lines and headings usually have different baseline distances. As you can see in the output, the baselines match quite well:

output

siracusa
  • 13,411