# Align section titles in margins properly

This is a follow-up to First paragraph indented after section title in margin, but I'm unable to vote, comment, or accept answers there because it's made under a guest account and not linked to this account.

Putting section titles in the outer margin when using the memoir class causes a tiny indent for the first paragraph, even with paragraph indent turned off and skipping lines between paragraphs turned on. Gonzalo Medina's answer (change \setaftersecskip to 1sp) fixes that problem, even without the negative indent trick, but it introduces another problem: the section titles no longer line up consistently with the first paragraph of the section. Sometimes the top of the title aligns with the top of the paragraph, sometimes the bottom lines up with the baseline of the first line of the paragraph, and sometimes it is between two paragraphs. Also, it looks like sometimes the line separating paragraphs is too narrow (not a full line height.)

Gonzalo also changed \marginpar in my example to \marginnotes and added that package to the preamble. That seems to fix the first part of the problem, but the line spacing between section/subsection changes still seems off. Is there another way to fix this?

(I don't have a way to edit questions/answers yet, but if someone could merge this question with the other question, that would be fine.)

\documentclass[12pt,a5]{memoir}
\usepackage{lipsum}

\semiisopage%  a default margin layouts

% fix margin notes

\setmarginnotes{3em}{0.2\textwidth}{2\onelineskip}

%% No numbered sections

\setcounter{secnumdepth}{0}

%% Section Names in margin

\marginpar{\bfseries\centering #1}}

%% set section, subsection and subsubsection headers
%% all the same, small bold text

\setsecindent{0em}
\setaftersecskip{0em}

\setsubsecindent{0em}
\setaftersubsecskip{0em}

% etc.

%% Paragraph styles
%%
%% no indent, skip line instead

\abnormalparskip{\baselineskip}
\setlength{\parindent}{0pt}

\checkandfixthelayout

\begin{document}

\section{First Section}
\lipsum[1]
\subsection{First Subsection}
\lipsum[1]
\section{Second Section}
\lipsum[1]

\end{document}

In my actual project, which is much longer than the MWE, the alignment issue was even worse, plus some of the section titles were getting put in the wrong margin and cut off by the page edge. But I seem to have found a solution, based on this other question, How to keep heading together with text.

What I did was I used both Gonsalo Medina's solution, including the \marginnote substitution, and \needspace, like this:

\needspace{4em}\marginnote{\bfseries\centering #1}}

\setsecindent{0em}
\setbeforesecskip{-2em}
\setaftersecskip{1sp}