TransWikia.com

Align multi-line section head on the grid

TeX - LaTeX Asked on June 25, 2021

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]
setuphead   [section]  [style=BigFont, grid=high, before={blank[4*line]}]

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
    BigFontraisebox5ptvboxbgroup
      3 Lorem ipsum dolor sit amet consecteturcrlf
      nullkern20pt adipisicing elitegroup
  stoplinecorrection
  input ward

stoptext

screenshot

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.

2 Answers

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

newdimensection_frame_width
unexpandeddefsection_command#number#title{%
  section_frame_widthhsize%% we need to calculate the remaining
  setboxscratchboxhbox{#numberspace}%% horizontal space
  advancesection_frame_width by -wdscratchbox
  #numberspace
  framed[
    width=section_frame_width,%% “local” or “fit” doesn’t work
    before=,
    location=top,
    frame=on,%% comment this for production
    align=right,]{#title}%
}

setuphead   [section]  [
  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

first line of structure title on grid

Correct answer by Philipp Gesang on June 25, 2021

The accepted solution is not (or no longer) working for me. I had to swap def for define.

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

unprotect
newdimensection_frame_width
define[2]section_command{%
  section_frame_widthhsize             % we need to calculate the remaining
  setboxscratchboxhbox{#1space} % horizontal space
  advancesection_frame_width by -wdscratchbox
  #1space
  framed [
       width=section_frame_width,
      before=,
    location=top,
       align=right,]{#2}%
}

setuphead [section] [
    style=BigFont,
    after=,
   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}
stoptext

Answered by Theo Schmitz on June 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP