将多行节头与网格对齐

将多行节头与网格对齐

我想要实现的目标

根据字体定义中给出的基线距离,将多行节头的第一行与网格和后续行对齐。节头所用字体的基线距离与网格距离不匹配。

我目前拥有的

如果节头只有一行,那么grid=high就会如我所料;第一行(实际上,唯一一行)在网格上对齐。但是,当节头由多行组成时,最后一行总是在网格上对齐,而不是第一行。

可用文档

详细手册 列出了几个值grid和相当多的文档。但不幸的是,所有示例都是单行标题。

代码

\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
    \BigFont\raisebox5pt\vbox\bgroup
      3 Lorem ipsum dolor sit amet consectetur\crlf
      \null\kern20pt adipisicing elit\egroup
  \stoplinecorrection
  \input ward

\stoptext

截屏

第一个单行头部还行。第二个是错误的多行头部,需要纠正;第三个是一个 hack,用来展示它应该是什么样子。

答案1

我的方法对您的代码进行了两处更改:

  1. 不要明确地将标题放在网格上:删除该选项grid=high。(根据先前经验提出的建议。)
  2. 使用参数将标题塞入框架location=top

下面的代码仍然非常健壮。即使处理三行或更多行的标题,内容仍将保持在网格中。但是,您的示例有一个属性没有考虑到:如果标题是新页面上的第一个元素,则标题将跳过第一行。就我个人而言,我更喜欢这样,而不是标题向上延伸到页眉中。

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

\unprotect

\newdimen\section_frame_width
\unexpanded\def\section_command#number#title{%
  \section_frame_width\hsize%% we need to calculate the remaining
  \setbox\scratchbox\hbox{#number\space}%% horizontal space
  \advance\section_frame_width by -\wd\scratchbox
  #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}%
}

\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

网格上结构标题的第一行

答案2

接受的解决方案对我来说不起作用(或不再起作用)。我不得不def换成define

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

\unprotect
\newdimen\section_frame_width
\define[2]\section_command{%
  \section_frame_width\hsize             % we need to calculate the remaining
  \setbox\scratchbox\hbox{#1\space} % horizontal space
  \advance\section_frame_width by -\wd\scratchbox
  #1\space
  \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

相关内容