details-snappingheads.tex /size: 9852 b    last modification: 2023-12-21 09:43
1% language=us
2
3\environment details-environment
4
5\startcomponent details-snappingheads
6
7\startchapter[title={Snapping heads}]
8
9The grid snapper in \MKIV\ is quite different from the one in \MKII. For not too
10complex layouts the old grid snapper was quite ok, but the new one should be a
11bit more robust. In the old situation the running text was assumed to fit on the
12grid and the normal baseline skip should do the job in combination with the grid
13aware spacing features and placement mechanisms like tables and figures.
14
15Snapping on a fixed grid is sort of counter intuitive in \TEX\ because it has an a
16advanced spacing model with glue. Publishers however love grids so we do need to
17support it. Of course when complex layouts are involved in a later stage of
18document preparation the grid is often abandoned. This manual uses the grid but I
19personally never use the grid. There are better ways to make your document look
20good and often a grid snapped document doesn't look that great anyway, because
21all elements should somehow fit in multiples of the line height.
22
23The \MKIV\ snapper does more analysis and therefore can compensate for the more
24nasty cases. Of course it can still fail but we hope to fix those cases when we
25run into them. Grids are controlled by keywords or a combination of them.
26
27\starttabulate[|tCT{blue}||]
28\NC none             \NC don't enlarge \NC \NR
29\NC halfline         \NC enlarge by halfline/halfline \NC \NR
30\NC line             \NC enlarge by line/line \NC \NR
31\NC strut            \NC enlarge by ht/dp (default) \NC \NR
32\NC first            \NC align to top line \NC \NR
33\NC last             \NC align to bottom line \NC \NR
34\NC mindepth         \NC round depth down \NC \NR
35\NC maxdepth         \NC round depth up \NC \NR
36\NC minheight        \NC round height down \NC \NR
37\NC maxheight        \NC round height up \NC \NR
38\NC local            \NC use local interline space \NC \NR
39\NC offset:-3tp      \NC vertical shift within box \NC \NR
40\NC bottom:lines     \NC \NC \NR
41\NC top:lines        \NC \NC \NR
42\NC box              \NC centers a box rounded upwards (box:.5 -> tolerance) \NC \NR
43\NC min              \NC centers a box rounded downwards \NC \NR
44\NC max              \NC centers a box rounded upwards \NC \NR
45\stoptabulate
46
47We combine these directives in so called grid options:
48
49\starttyping
50\definegridsnapping [normal]   [maxheight,maxdepth,strut]
51\definegridsnapping [standard] [maxheight,maxdepth,strut]
52\definegridsnapping [yes]      [maxheight,maxdepth,strut]
53
54\definegridsnapping [strict]   [maxdepth:0.8,maxheight:0.8,strut]
55\definegridsnapping [tolerant] [maxdepth:1.2,maxheight:1.2,strut]
56\definegridsnapping [math]     [maxdepth:1.05,maxheight:1.05,strut]
57
58\definegridsnapping [top]      [minheight,maxdepth,strut]
59\definegridsnapping [bottom]   [maxheight,mindepth,strut]
60\definegridsnapping [both]     [minheight,mindepth,strut]
61
62\definegridsnapping [broad]    [maxheight,maxdepth,strut,0.8]
63\definegridsnapping [fit]      [maxheight,maxdepth,strut,1.2]
64
65\definegridsnapping [first]    [first]
66\definegridsnapping [last]     [last]
67\definegridsnapping [high]     [minheight,maxdepth,none]
68\definegridsnapping [one]      [minheight,mindepth]
69\definegridsnapping [low]      [maxheight,mindepth,none]
70\definegridsnapping [none]     [none]
71\definegridsnapping [line]     [line]
72\definegridsnapping [strut]    [strut]
73\definegridsnapping [box]      [box]
74\definegridsnapping [min]      [min]
75\definegridsnapping [max]      [max]
76
77\definegridsnapping [middle]   [maxheight,maxdepth]
78\stoptyping
79
80As you can see, keys like \type {maxdepth} can take a criterium as extra detail,
81separated by a colon. These options look obscure and often you need to trial and
82error a bit to get what you want. This is no real problem because most cases are
83handles automatically. Only headings and structuring elements that exceed a
84line height might need some treatment. For instance you might want to be more
85tolerant for (fractions of) a point overflow or when you know that always a blank
86follows you can decide to limit the height of some element to a line. Some of
87the options, like \type {math} and \type {middle} are used internally.
88
89On the next pages we show how the \type {maxheight} and \type {maxdepth}
90fractions work on a sample line.
91
92\page
93
94\bgroup
95    \page
96    \enabledirectives[visualizers.fraction=.5]
97    \dostepwiserecurse {0} {10} {1} {
98        \edef\one{\ifnum#1=10 10\else0.#1\fi}
99        \dostepwiserecurse {0} {10} {1} {
100            \edef\two{\ifnum##1=10 10\else0.##1\fi}
101            \definegridsnapping [test:\one:\two][maxdepth:\one,maxheight:\two,strut]
102            \par
103                \blackrule[height=1pt,depth=1pt,width=\textwidth,color=green]
104            \par
105            \par
106                \snaptogrid[test:\one:\two]
107                    \ruledhbox{\backgroundline[red]{\white\bf maxdepth:\one,maxheight:\two}}
108            \par
109        }
110    }
111    \par
112    \blackrule[height=1pt,depth=1pt,width=\textwidth,color=green]
113    \par
114    \page
115    \enabledirectives[visualizers.fraction]
116\egroup
117
118Next we show some of the options in action. For practical reasons we start a new
119page each time. The sample is input as:
120
121\startbuffer
122\bf                             none \par
123\bfb \hskip2cm                  none \par
124\bfd \hskip6cm                  none \par
125\bf                             test \par
126\bfb \hskip2cm                  test \par
127\bfd \hskip6cm                  test \par
128\bf                             grid \par
129\bfb \hskip2cm                  grid \par
130\bfd \hskip6cm                  grid \par
131\bf                      \strut strut \par
132\bfb \hskip2cm           \strut strut \par
133\bfd \hskip6cm           \strut strut \par
134\bfb \hskip2cm \setstrut \strut setstrut \par
135\bfd \hskip6cm \setstrut \strut setstrut \par
136\stopbuffer
137
138\typebuffer
139
140\unexpanded\def\SampleGrid#1%
141  {\page
142   \section{Grid snapping method \quotation{#1}}
143    This is just a line to start with but next we %
144    show what method \type {#1} does. \par
145    \start
146    \setuplayout[grid=#1] % the demo value
147    \showstruts
148    \getbuffer
149    \stop
150    And here we end the demo.
151    \setuplayout[grid=yes] % the document default
152    \page}
153
154\SampleGrid{normal}
155\SampleGrid{strict}
156\SampleGrid{tolerant}
157\SampleGrid{top}
158\SampleGrid{bottom}
159\SampleGrid{both}
160\SampleGrid{broad}
161\SampleGrid{fit}
162\SampleGrid{first}
163\SampleGrid{last}
164\SampleGrid{high}
165\SampleGrid{one}
166\SampleGrid{low}
167\SampleGrid{none}
168\SampleGrid{line}
169\SampleGrid{strut}
170\SampleGrid{box}
171\SampleGrid{min}
172\SampleGrid{max}
173\SampleGrid{middle}
174
175We now come to the topic of this chapter: snapping heads. The problem with
176section heads is that they often exceed the line height. Even worse, they can
177be more than one line high.
178
179The next pages show some ways to control snapping around heads. The result can be
180confusing, even when we use a font that we assume behaves like a regular style.
181For instance in Latin Modern the bold style has larger heights and depths than
182the regular style and even 0.1pt can force the snapper to add a line. The
183examples use that font.
184
185The grid option of \type {setuphead} normally takes one keyword that refers to
186the local snapper. However, the result gets then snapped again. This is because
187the local snapper can use a different line height. Historically the local snapper is
188the default but you can force global snapping by prefixing with the \type
189{global} keyword. The next table summarizes the ways you can control snapping:
190
191\starttabulate
192\NC \type {(nothing)}  \NC local snapping plus global snapping \NC \NR
193\NC \type {local}      \NC local snapping plus global snapping \NC \NR
194\NC \type {foo}        \NC local \type {foo} snapping cf.\ font style plus global snapping \NC \NR
195\NC \type {local:foo}  \NC local \type {foo} snapping cf.\ font style plus global snapping  \NC \NR
196\NC \type {global}     \NC global snapping \NC \NR
197\NC \type {global:foo} \NC global \type {foo} snapping \NC \NR
198\stoptabulate
199
200\startbuffer[demo]
201\setuppapersize
202  [A5][A5]
203
204\setuplayout
205  [grid=yes,
206   width=middle,
207   height=middle,
208   backspace=1cm,
209   topspace=2mm,
210   lines=38,
211   bottomspace=2mm,
212   footer=0cm,
213   header=4mm,
214   headerdistance=5mm]
215
216\definehead
217  [MyHead]
218  [subsubsubject]
219  [style=\tf,
220   textstyle=,
221   numberstyle=,
222   before=,
223   after=]
224
225\showgrid
226
227\starttext
228
229\starttexdefinition TestHead #1
230    \page
231    \setupheadertexts[\type{#1}]
232    \setuphead[MyHead][grid={#1},style=\tf,interlinespace=]
233    \MyHead{some head 1.1}
234    \hskip1cm line following 1.1
235    \MyHead{some head 1.2}
236    \hskip1cm line following 1.2
237    \MyHead{some head 1.3a\par some head 1.3b}
238    \hskip1cm line following 1.3
239    \setuphead[MyHead][grid={#1},style=\bf,interlinespace=]
240    \MyHead{some head 2.1}
241    \hskip1cm line following 2.1
242    \MyHead{some head 2.2}
243    \hskip1cm line following 2.2
244    \MyHead{some head 2.3a\par some head 2.3b}
245    \hskip1cm line following 2.3
246    \setuphead[MyHead][grid=,style=\bf,interlinespace=4ex]
247    \MyHead{some head 3.1}
248    \hskip1cm line following 3.1
249    \MyHead{some head 3.2}
250    \hskip1cm line following 3.2
251    \MyHead{some head 3.3a\par some head 3.3b}
252    \hskip1cm line following 3.3
253    \setuphead[MyHead][grid={#1},style=\bfb,interlinespace=]
254    \MyHead{some head 4.1}
255    \hskip1cm line following 4.1
256    \MyHead{some head 4.2}
257    \hskip1cm line following 4.2
258    \page
259\stoptexdefinition
260
261\TestHead{yes}
262\TestHead{tolerant}
263\TestHead{global:tolerant}
264
265\stoptext
266\stopbuffer
267
268\typebuffer
269
270\placefigure[page]{}{\typesetbuffer[demo][page=1,background=color,backgroundcolor=white,width=\textwidth]}
271\placefigure[page]{}{\typesetbuffer[demo][page=2,background=color,backgroundcolor=white,width=\textwidth]}
272\placefigure[page]{}{\typesetbuffer[demo][page=3,background=color,backgroundcolor=white,width=\textwidth]}
273
274\stopchapter
275
276\stopcomponent
277