# Align multi-line section head on the grid

What I want to achieve

Align the first line of a multi-line section head on the grid and consecutive lines according to the baseline distance given in the font definition. The baseline distance of the font used for the section heads does not match the grid distance.

What I have so far

If a section head consists of only one line, then grid=high does what I expect; the first (in fact, the only line) is aligned on the grid. However, when the head consists of more than one line, the last line is always aligned on the grid, instead of the first.

Available documentation

The Details manual lists several values for grid and quite some documentation. But all examples are single-line heads, unfortunately.

The code

\setuplayout [grid=yes]
\definefont  [BigFont]  [Bold at 20pt] [24pt]

\showgrid
\starttext

\startsection [title=Lorem ipsum dolor]
\input ward
\stopsection

\startsection [title=Lorem ipsum dolor sit amet consectetur adipisicing elit]
\input ward
\stopsection

% Demonstration what I'd like to achieve
\blank [4*line]
\startlinecorrection
\BigFont\raisebox5pt\vbox\bgroup
3 Lorem ipsum dolor sit amet consectetur\crlf
\stoplinecorrection
\input ward

\stoptext


The first single-line head is fine. The second is the wrong multi-line head that wants to be corrected; and the third one is a hack to show how it should look like.

-

My approach makes two changes to your code:

1. Do not place the headings on grid explicitly: remove the option grid=high. (Recommendation based on prior experience.)
2. Stuff headings into a frame with the parameter location=top.

The code below remains remarkably robust. Even when dealing with headings that take three lines and more, things will remain on grid. There is, however, one property of your example that it does not account for: if the heading is the first element on a new page, the title will skip the first line. Personally, I prefer that to the heading extending upwards into the header.

% macros=mkvi
\setuplayout [grid=yes]
\definefont  [BigFont]  [Bold at 20pt] [24pt]

\unprotect

\newdimen\section_frame_width
\def\section_command#number#title{%
\section_frame_width\hsize%% we need to calculate the remaining
\setbox\scratchbox\hbox{#number\space}%% horizontal space
#number\space
\framed[
width=\section_frame_width,%% “local” or “fit” doesn’t work
before=,
location=top,
frame=on,%% comment this for production
align=right,]{#title}%
}

style=\BigFont,
after=,
% grid=high,
before={\blank[2*line]},
command=\section_command,
]
\protect

\showgrid

\starttext

\startsection [title=Lorem ipsum dolor]
\input ward
\stopsection

\dorecurse{4}{%
\startsection [title=Lorem ipsum dolor sit amet consectetur adipisicing elit]
\input ward \stopsection %% two lines
\startsection [title=Lorem ipsum dolor sit amet consectetur adipisicing elit
Lorem ipsum dolor sit amet consectetur adipisicing elit]
\input ward \stopsection %% three
\startsection [title=Lorem ipsum dolor sit amet consectetur adipisicing elit
Lorem ipsum dolor sit amet consectetur adipisicing elit
Lorem ipsum dolor sit amet consectetur adipisicing elit]
\input ward \stopsection %% five
}

\stoptext


-