details-finetuningfloats.tex /size: 19 Kb    last modification: 2023-12-21 09:43
1% language=us
2
3% todo: \setlayeranchored[text-1]{HELLO WORLD}
4
5\environment details-environment
6
7\startcomponent details-finetuningfloats
8
9\startchapter[title={Finetuning graphics}]
10
11In this chapter we will discuss a few more tricks to control float placement.
12This control is needed if you want to typeset documents in a semi desk top
13publishing way.
14
15When you combine technical graphics, you may wish to align the content optically.
16This can be done with the \type {offset} command. We will demonstrate this with a
17couple of \METAPOST\ graphics:
18
19\startbuffer
20\startreusableMPgraphic{alpha}
21  fill fullsquare xyscaled(  2cm,  2cm) withcolor \MPcolor{red} ;
22  fill unitsquare xyscaled(+.5cm,+.5cm) withcolor \MPcolor{gray} ;
23\stopreusableMPgraphic
24
25\startreusableMPgraphic{beta}
26  fill fullsquare xyscaled(  2cm,  2cm) withcolor \MPcolor{red} ;
27  fill unitsquare xyscaled(+.5cm,-.5cm) withcolor \MPcolor{gray} ;
28\stopreusableMPgraphic
29
30\startreusableMPgraphic{gamma}
31  fill fullsquare xyscaled(  2cm,  2cm) withcolor \MPcolor{red} ;
32  fill unitsquare xyscaled(-.5cm,-.5cm) withcolor \MPcolor{gray} ;
33\stopreusableMPgraphic
34
35\startuseMPgraphic{delta}
36  fill fullsquare xyscaled(  2cm,  2cm) withcolor \MPcolor{red} ;
37  fill unitsquare xyscaled(-.5cm,+.5cm) withcolor \MPcolor{gray} ;
38\stopuseMPgraphic
39\stopbuffer
40
41\typebuffer \getbuffer
42
43\startbuffer
44\startcombination[2*2]
45  {\reuseMPgraphic{alpha}} {alpha}
46  {\reuseMPgraphic {beta}}  {beta}
47  {\reuseMPgraphic{gamma}} {gamma}
48  {\reuseMPgraphic{delta}} {delta}
49\stopcombination
50\stopbuffer
51
52\typebuffer
53
54In \in {figure} [fig:offset-1] we place these graphics in a \type {2*2} grid. As
55you can see, the centers don't align well.
56
57\placefigure[here][fig:offset-1]{}{\getbuffer}
58
59In \in {figure} [fig:offset-2] the centers of the graphic align well. This is
60accomplished by adding some space around the graphics.
61
62\startbuffer
63\startcombination[2*2]
64  {\ruledhbox{\offset[rightoffset=1cm]  {\reuseMPgraphic{alpha}}}} {alpha}
65  {\ruledhbox{\offset[bottomoffset=.5cm]{\reuseMPgraphic {beta}}}}  {beta}
66  {\ruledhbox{\offset[bottomoffset=.5cm]{\reuseMPgraphic{gamma}}}} {gamma}
67  {\ruledhbox{\offset[leftoffset=1cm]   {\reuseMPgraphic{delta}}}} {delta}
68\stopcombination
69\stopbuffer
70
71\placefigure[here][fig:offset-2]{}{\getbuffer}
72
73\starttyping
74\startcombination[2*2]
75  {\offset[rightoffset=1cm]  {\reuseMPgraphic{alpha}}} {alpha}
76  {\offset[bottomoffset=.5cm]{\reuseMPgraphic {beta}}}  {beta}
77  {\offset[bottomoffset=.5cm]{\reuseMPgraphic{gamma}}} {gamma}
78  {\offset[leftoffset=1cm]   {\reuseMPgraphic{delta}}} {delta}
79\stopcombination
80\stoptyping
81
82If we align the centers vertically, as demonstrated in \in {figure}
83[fig:offset-2] we can stick to a few bottom offsets.
84
85\starttyping
86\startcombination[4*1]
87                             {\reuseMPgraphic{alpha}}  {alpha}
88  {\offset[bottomoffset=.5cm]{\reuseMPgraphic {beta}}}  {beta}
89  {\offset[bottomoffset=.5cm]{\reuseMPgraphic{gamma}}} {gamma}
90                             {\reuseMPgraphic{delta}}  {delta}
91\stopcombination
92\stoptyping
93
94\startbuffer
95\startcombination[4*1]
96  {\ruledhbox                           {\reuseMPgraphic{alpha}}}  {alpha}
97  {\ruledhbox{\offset[bottomoffset=.5cm]{\reuseMPgraphic {beta}}}}  {beta}
98  {\ruledhbox{\offset[bottomoffset=.5cm]{\reuseMPgraphic{gamma}}}} {gamma}
99  {\ruledhbox                           {\reuseMPgraphic{delta}}}  {delta}
100\stopcombination
101\stopbuffer
102
103\placefigure[here][fig:offset-3]{}{\getbuffer}
104
105These examples demonstrate that the dimensions change with the offset. You can
106retain the dimensions but still align them by using the \type {x} and \type {y}
107parameter. This kind of manipulations will often result in a ugly spacing because
108the placement macros handle on the original dimensions. \in {Figure}
109[fig:offset-4] demonstrates this.
110
111\starttyping
112\startcombination[4*1]
113                   {\reuseMPgraphic{alpha}}  {alpha}
114  {\offset[y=-.5cm]{\reuseMPgraphic {beta}}}  {beta}
115  {\offset[y=-.5cm]{\reuseMPgraphic{gamma}}} {gamma}
116                   {\reuseMPgraphic{delta}}  {delta}
117\stopcombination
118\stoptyping
119
120\startbuffer
121\startcombination[4*1]
122  {\ruledhbox                 {\reuseMPgraphic{alpha}}}  {alpha}
123  {\ruledhbox{\offset[y=-.5cm]{\reuseMPgraphic {beta}}}}  {beta}
124  {\ruledhbox{\offset[y=-.5cm]{\reuseMPgraphic{gamma}}}} {gamma}
125  {\ruledhbox                 {\reuseMPgraphic{delta}}}  {delta}
126\stopcombination
127\stopbuffer
128
129\placefigure[here][fig:offset-4]{}{\getbuffer}
130
131In the previous chapter we demonstrated how a side float can be moved up or down
132by providing a placement directive or by preceding the placement with \type
133{\movesidefloat}. Such a move can be used to align a graphic with particular line
134of text. This command can also be used for alignment purposes similar to the
135\type {\offset} command. We will demonstrate this with the following graphics.
136
137\startbuffer
138\startreusableMPgraphic{gnu}
139  fill fullsquare xyscaled( 4cm, 1cm) withcolor \MPcolor{red} ;
140  fill unitsquare xyscaled(-1cm,.5cm)
141                   shifted (0,-.25cm) withcolor \MPcolor{gray} ;
142\stopreusableMPgraphic
143
144\startreusableMPgraphic{gnat}
145  fill fullsquare xyscaled( 4cm, 1cm) withcolor \MPcolor{red} ;
146  fill unitsquare xyscaled(+1cm,.5cm)
147                   shifted (0,-.25cm) withcolor \MPcolor{gray} ;
148\stopreusableMPgraphic
149\stopbuffer
150
151\typebuffer \getbuffer
152
153In the next two examples we shift the \type {gnu} and \type {gnat} graphics
154horizontally in order to get them aligned. The move does not change the
155dimensions of the float, but they do influence the paragraph shape.
156
157\startbuffer[a]
158\movesidefloat [x=.5cm]
159\placefigure   [left,none] {} {\reuseMPgraphic{gnu}}
160\stopbuffer
161
162\startbuffer[b]
163\movesidefloat [x=-.5cm]
164\placefigure   [left,none] {} {\reuseMPgraphic{gnat}}
165\stopbuffer
166
167\typebuffer[a,b]
168
169\getbuffer[a] \fakewords{50}{100}
170\getbuffer[b] \fakewords{50}{100}
171
172\blank
173
174It is possible to shift vertically by setting \type {y}, but this is often a bad
175idea and definitely may spoil alignment of graphics to the grid. If you have to
176revert to this trick, you are probably working in document screw||up mode. This
177is why in grid mode, we automatically round to an equal number of lines.
178
179If you know what text you're dealing with and also can be sure about the height
180of a graphic, you can trick \CONTEXT\ to ignore the dimensions of a graphic. Here
181we use the graphic:
182
183\startbuffer
184\startreusableMPgraphic{gnome}
185  fill fullsquare xyscaled(2cm, 1cm) withcolor \MPcolor{red} ;
186  fill fullsquare xyscaled(1cm,.5cm) withcolor \MPcolor{gray} ;
187\stopreusableMPgraphic
188\stopbuffer
189
190\typebuffer \getbuffer
191
192\startbuffer
193\placefigure[leftmargin,none,reset]{}{\reuseMPgraphic{gnome}}
194\stopbuffer
195
196\typebuffer \getbuffer
197
198The graphic is moved into the margin (\type {leftmargin}), has no caption (\type
199{none}), and all kind of tricky housekeeping is reset (\type {reset}).
200
201\startbuffer
202\placefigure[left,none,high,low]{}{\reuseMPgraphic{gnome}}
203\stopbuffer
204
205\getbuffer
206
207Now the next graphic is not influenced by the previous one, so we can place them
208close to each other. Use these tricks with care, especially if your document
209source is reused and the typeset products are not carefully checked.
210
211\typebuffer
212
213When \CONTEXT\ tries to determine if a float fits, it makes a couple of
214assumptions, for instance that the available room equals the text height minus
215the height of the text so far. You can slightly influence the way these values
216are interpreted by setting the calculation method. You can set the methods as
217follows:
218
219\starttyping
220\setupfloats[textmethod=0,sidemethod=1]
221\stoptyping
222
223Method~0 just looks at the raw dimensions, while method~1 lessens the maximum
224text height by one percent, thereby playing safe. Method~2 takes a window of
2251~point. This may lead to better decisions since we may run into rounding errors
226of several scaled points (which is small but troublesome). Method~2 is well
227suited when typesetting on a grid, because there everything has to fit in a
228rounded number of lines, which leaves no room for rounding errors.
229
230\starttabulate[||c|c|]
231\NC \bf grid mode     \NC \bf yes  \NC \bf no   \NC \NR
232\NC \type{sidemethod} \NC \type{2} \NC \type{1} \NC \NR
233\NC \type{textmethod} \NC \type{2} \NC \type{0} \NC \NR
234\stoptabulate
235
236\startusableMPgraphic{demo-1}{color}
237  path p ;
238  p := fullsquare xyscaled (3cm,2LineHeight) ;
239  fill p withcolor \MPvar{color} ;
240\stopusableMPgraphic
241
242\startusableMPgraphic{demo-2}{color,morecolor}
243  path p ; p := fullsquare xyscaled (6cm,4LineHeight) ;
244  path q ; q := fullsquare xyscaled (3cm,2LineHeight) ;
245  fill p withcolor \MPvar{color} ;
246  setbounds currentpicture to q ;
247  fill q withcolor \MPcolor{morecolor} ;
248\stopusableMPgraphic
249
250As you may know by now, we can use the directives \type {high}, \type {low},
251\type {height}, \type {depth} and \type {line} to influence the spacing around a
252side float. A real tight spacing can be achieved with \type {fit}.
253
254\starttyping
255\placefigure[left,fit,none]{}{some graphic}
256\stoptyping
257
258\placefigure[left,fit,none]{}{\useMPgraphic{demo-1}{color=red}}
259
260This kind of placements only make sense in special situations, because normally
261you don't want the graphic to touch the text.
262
263If you think that this is all a user may want, you're wrong. It is not imaginary
264that graphics have small pieces sticking out and|/|or lots of white space as part
265of their design. In that case, the bounding box can be set to a smaller size.
266
267\placefigure
268  [left,fit,none]
269  {}
270  {\setlayer
271      [graphics]
272      {\useMPgraphic{demo-2}{color=red,morecolor=tgreen}}}
273
274Now, when handling a side float, \CONTEXT\ first places the float, and then
275starts with typesetting the paragraph, cleverly avoiding the graphic. However,
276when the graphic is virtually larger than its known size, it may cover part of
277the preceding paragraph.
278
279How come that the graphic starting this paragraph does not do that? It is because
280we explicitly moved it to the background. This involves some preparation. At the
281document level, we define a layer called \type {graphic}.
282
283\starttyping
284\definelayer[graphics][position=yes]
285\stoptyping
286
287The position directive tells \CONTEXT\ that it should honour the position of the
288graphic. Next we must make sure that this layer is placed.
289
290\starttyping
291\setupbackgounds[page][background=graphics]
292\stoptyping
293
294Now we're ready to move graphics to this layer:
295
296\starttyping
297\placefigure
298  [left,fit,none]
299  {}{\setlayer[graphics]{graphic}}
300\stoptyping
301
302It's now a small step to more advanced movements. Say that you want to move the
303graphic a little bit to the left. In that case you can tell the layer placement
304to do so.
305
306\starttyping
307\placefigure
308  [left,fit,none]{}{\setlayer[graphics][hoffset=-12pt]{graphic}}
309\stoptyping
310
311From this you can deduce that there is also a movement in the vertical direction
312using \type {voffset}. In addition you can anchor the graphic using the \type
313{location} parameter and provide offsets.
314
315\placefigure
316  [left,fit,none]
317  {}
318  {\setlayer
319     [graphics][hoffset=-12pt]
320     {\useMPgraphic{demo-2}{color=red,morecolor=tgreen}}}
321
322As soon as you run into situations where float placement is to be consistently
323enforced, you will feel the need for dedicate placement macros. For example:
324
325\startbuffer
326\definefloat
327  [somefloat]
328  [figure]
329
330\setupfloat
331  [somefloat]
332  [sidespaceafter=,
333   sidespacebefore=,
334   default={left,none}]
335\stopbuffer
336
337\typebuffer \getbuffer
338
339Instead of resetting the side spacing, we could have default to \type {high,low},
340but this way we can overload the default placement and still get zero spacing.
341
342Throughout this manual we discuss features related to overlays and layers. These
343permit you to move content around in ways that either or not depend on the text
344flow. We have now come to another trick based on these mechanisms: bleeding.
345
346When printing a document, you need to take into account that when graphics go
347beyond the page boundary, you need to compensate for inaccuracies in cutting the
348pages. Such graphics are called bleeding graphics and the amount of bleed is
349often a few millimeters.
350
351The best way to handle such graphics is to use the correct dimensions and play
352with the edge widths and distances in combination with backspace and cut space. In
353a properly set up layout and by using a well designed set of predefined graphic
354placements, you can handle this quite well. A bleeding figure can be defined as
355follows:
356
357\startbuffer
358\definefloat
359  [edgefigure]
360  [figure]
361
362\setupfloat
363  [edgefigure]
364  [default={inner,height,high,low,none},
365   maxwidth=4cm]
366
367\defineexternalfigure
368  [edgefigure]
369  [width=\dimexpr\backspace+4cm-1mm\relax,
370   lines=4]
371\stopbuffer
372
373\typebuffer \getbuffer
374
375The default placement is pre|-|configured to have no additional vertical space and
376align on the height of a line (this is default behaviour so the \type {height}
377key is redundant here. The 1mm in the previous definition simulates what happens
378when a page is cut off slightly wrong: we get an annoying gap.
379
380\startbuffer[a]
381\placeedgefigure
382  {}
383  {\externalfigure[hacker][edgefigure]}
384\stopbuffer
385
386\typebuffer[a] \getbuffer[a] \fakewords{50}{100}
387
388One of the nice things about \TEX\ is that you can fine tune dimensions pretty
389well. So, instead of the previous placement, which turns out rather ugly, we can
390come up with a better one:
391
392\startbuffer
393\setupfloat
394  [edgefigure]
395  [default={inner,height,high,low,none},
396   maxwidth=4cm,
397   margin=\strutdepth]
398
399\defineexternalfigure
400  [edgefigure]
401  [width=\dimexpr\backspace+4cm+2mm\relax,
402   height=\dimexpr3\lineheight+\strutheight\relax]
403\stopbuffer
404
405\typebuffer \getbuffer
406
407This time we take no risk and add 2mm to the dimensions so that we can be sure
408that the edge of the graphic falls outside the page boundary.
409
410\getbuffer[a] \fakewords{50}{100}
411
412The \CONTEXT\ resourse library modules provide means to report back the
413dimensions of graphics used in a document, so that you can develop (tune) them
414with the proper dimensions. In practice a slightly wider than normal graphic
415(scaling it horizontally a few millimeters more) does not harm the visual
416appearance that much, so adapting a graphic to this kind of bleeding is not
417really needed.
418
419In addition to this (rather natural) way of adding bleed to a graphic, you can
420apply the \type {\bleed} macro. In the previously discussed method the figure
421placement mechanisms work with the real dimensions. The \type {bleed} macro is
422using scaling in a different way: from the perspective of \CONTEXT\ the graphic
423remains its original dimensions and the figure placement mechanisms will act
424accordingly. We will give a couple of examples of using this macro.
425
426Permitted bleeding locations are \type {l}, \type {r}, \type {t}, \type {b},
427\type {lr}, \type {bl}, \type {br}, \type {tl} and \type {tr}.
428
429\startbuffer
430\placesomefloat
431  [left,none,fit]
432  {}
433  {\setupbleeding[offset=5mm]%
434   \bleed[width=5cm,height=1cm,location=l]
435     {\externalfigure[mill][bleed]}}
436\stopbuffer
437
438\typebuffer \getbuffer \fakewords{50}{100}
439
440\startbuffer
441\placesomefloat
442  [left,none,fit]
443  {}
444  {\setupbleeding[offset=2mm]%
445   \bleed[width=5cm,height=1cm,location=l]
446     {\externalfigure[mill][bleed]}}
447\stopbuffer
448
449\typebuffer \getbuffer \fakewords{50}{100}
450
451The amount of bleeding depends on the postprocessing. In the previous paragraph
452we used a bleed offset of 5mm, and here we used 2mm. Because the graphic is
453scaled in order to match the bleed, it will be slightly distorted. With small
454values this will go unnoticed. You can set the offset with:
455
456\starttyping
457\setupbleeding[offset=5mm]
458\stoptyping
459
460Bleeding itself is accomplished by the \type {\bleed} macro as in:
461
462\starttyping
463\bleed
464  [width=5cm,height=1cm,location=l]
465  {\externalfigure[mill][width=\bleedwidth,height=\bleedheight]}
466\stoptyping
467
468It is kind of awkward to pass those two dimensions so here is a shorter way of
469doing the same:
470
471\starttyping
472\bleed
473  [width=5cm,height=1cm,location=l]
474  {\externalfigure[mill][bleed]}
475\stoptyping
476
477In fact, this uses the following definition:
478
479\starttyping
480\defineexternalfigure[bleed][width=\bleedwidth,height=\bleedheight]
481\stoptyping
482
483You can influence the scaling of a graphic by setting the \type {stretch}
484parameters. The location parameter determines the direction of the stretch: \type
485{l}~(left), \type {r}~(right), \type {t}~(top), \type {b}~(bottom) or a
486combination of these. We will now combine the previous example code with this
487knowledge.
488
489\startbuffer
490\placefigure
491  [left]
492  {}
493  {\bleed
494     [stretch=no,voffset=0pt,hoffset=1cm]
495     {\externalfigure[detcow][bleed]}}
496\stopbuffer
497
498\typebuffer \getbuffer \fakewords {100} {150}
499
500\startbuffer
501\placefigure
502  [left]
503  {}
504  {\bleed
505     [width=5cm,height=3cm,location=l]
506     {\externalfigure[detcow][bleed]}}
507\stopbuffer
508
509\typebuffer \getbuffer \fakewords {100} {150}
510
511\startbuffer
512\placefigure
513  [right]
514  {}
515  {\bleed
516     [width=5cm,height=3cm,location=r]
517     {\externalfigure[detcow][bleed]}}
518\stopbuffer
519
520\typebuffer \getbuffer \fakewords {100} {150}
521
522You can combine this feature with layers. We will now show a few applications
523which may look like magic at first glance, but will become natural to your
524repertoire once you have played with them.
525
526The next example moves the graphic to a layer associated with the (current) page.
527
528\startbuffer
529\placefigure
530  [right,none]
531  {}
532  {\setlayer
533     [graphics]
534     {\bleed
535        [width=5cm,height=3cm,location=rb]
536        {\externalfigure[detcow][bleed]}}}
537\stopbuffer
538
539\typebuffer \getbuffer \fakewords {100} {150}
540
541You can also predefine locations where graphics (or other content) needs to be
542anchored. A direct call to anchor looks as follows:
543
544\starttyping
545\placefigure
546  [left,none]
547  {}
548  {\anchor
549     [text-1]
550     [location=lt,hoffset=max,voffset=max]
551     [width=3cm,height=3cm,frame=on]%
552     {\externalfigure[detcow][width=5cm,frame=on]}}
553\stoptyping
554
555This will anchor a graphic in one of the text layers, but at the cost of
556specifying this in the document source. One way around this is to predefine
557anchors.
558
559\startbuffer
560\defineanchor[rightbottom][text-1][location=lt,hoffset=max,voffset=max]
561\defineanchor[righttop]   [text-1][location=lb,hoffset=max]
562\defineanchor[leftbottom] [text-1][location=rt,voffset=max]
563\defineanchor[lefttop]    [text-1][location=rb]
564\stopbuffer
565
566\startbuffer
567\defineanchor[rightbottom][text-1][preset=rightbottom]
568\defineanchor[righttop]   [text-1][preset=righttop]
569\defineanchor[leftbottom] [text-1][preset=leftbottom]
570\defineanchor[lefttop]    [text-1][preset=lefttop]
571\stopbuffer
572
573\typebuffer \getbuffer
574
575We will apply this to a predefined float type.
576
577\startbuffer
578\definefloat[myfigure][figure]
579\setupfloat[myfigure][sidespaceafter=,sidespacebefore=]
580\stopbuffer
581
582\typebuffer \getbuffer
583
584Our previous example can now be reduced to:
585
586\startbuffer
587\placemyfigure
588  [left,none]
589  {}
590  {\anchor[rightbottom]
591     {\externalfigure[detcow][width=5cm,frame=on]}}
592\stopbuffer
593
594\typebuffer \getbuffer \fakewords {100} {150}
595
596You can still specify dimensions and anchors can be combined with bleeding. Of
597course this kind of mixed usage means that you need to have some feeling for what
598these mechanisms do.
599
600\startbuffer
601\placemyfigure
602  [left,none]
603  {}
604  {\anchor
605     [rightbottom]
606     [width=5cm,height=3cm,frame=on]
607     {\bleed
608        [width=5cm,height=3cm,location=l]
609        {\externalfigure[detcow][bleed]}}}
610\stopbuffer
611
612\typebuffer \getbuffer \fakewords {100} {150}
613
614\startbuffer
615\placemyfigure
616  [right,none]
617  {}
618  {\anchor
619     [rightbottom]
620     [width=5cm,height=3cm,frame=on]
621     {\bleed
622        [width=5cm,height=3cm,location=r]
623        {\externalfigure[detcow][bleed]}}}
624\stopbuffer
625
626\typebuffer \getbuffer \fakewords {100} {150}
627
628\startbuffer
629\placemyfigure
630  [left,none]
631  {}
632  {\anchor
633     [lefttop]
634     [width=3cm,height=3cm,frame=on]
635     {\externalfigure[detcow][width=5cm,frame=on]}}
636\stopbuffer
637
638\typebuffer \getbuffer \fakewords {100} {150}
639
640\startbuffer
641\placemyfigure
642  [left,none]
643  {}
644  {\anchor
645     [lefttop]
646     [width=3cm,height=3cm,frame=on]
647     [offset=.5cm]
648     {\externalfigure[detcow][width=5cm,frame=on]}}
649\stopbuffer
650
651\typebuffer \getbuffer \fakewords {100} {150}
652
653\blank {\em Todo: parameter specifications of all those macros.}
654
655\stopchapter
656
657\stopcomponent
658