summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/texit/texit-leaders.tex
blob: ed224da5ce064f496ed1618f3c3e0dd4a1049633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
\environment texit-style

\startcomponent texit-leaders

\startchapter[title={Leaders}]

The following example comes from a question on the \CONTEXT\ list. It
exhibits a few low level tricks. For the purpose of this example we
use \type {\ruledhbox} instead of \type {\hbox}. We start with a simple
command that puts something at the end of a line:

\startbuffer
\starttexdefinition MyFill #1
    \removeunwantedspaces
    \hfill
    \ruledhbox{#1}
\stoptexdefinition
\stopbuffer

\typebuffer[option=TEX] \getbuffer

We use this in:

\startbuffer[sample]
\startitemize[packed,joinedup][rightmargin=3em]
    \startitem
        \samplefile{ward}\MyFill{DW}
    \stopitem
\stopitemize
\stopbuffer

\typebuffer[sample][option=TEX]

and get:

\getbuffer[sample]

But, the requirement was that we move the number towards the right margin, so
instead we need something:

\startbuffer
\starttexdefinition MyFill #1
    \removeunwantedspaces
    \hfill
    \rlap{\ruledhbox to \rightskip{\hss#1}}
\stoptexdefinition
\stopbuffer

\typebuffer[option=TEX] \getbuffer

This already looks more like it:

\getbuffer[sample]

But also part of the requirements was that there should be dots between the end
of the last sentence and the number. In low level \TEX\ speak that means using
leaders: repeated boxed content where the repitition is driven by a glue
specification. Let's naively use leaders now:

\startbuffer
\starttexdefinition MyFill #1
    \leaders
        \ruledhbox to 1em{\hss.\hss}
        \hfill
    \ruledhbox{#1}
\stoptexdefinition
\stopbuffer

\typebuffer[option=TEX] \getbuffer

Let's see what we get:

\getbuffer[sample]

Again we need to move the number to the right. This time we need a different
solution because we need to fill the space in between. When \TEX\ ends a
paragraph it adds \type {\parfillskip} so we will now manipulate that parameter.

\startbuffer
\starttexdefinition MyFill #1
    \parfillskip-1\rightskip plus 1fil\relax
    \leaders
        \ruledhbox to 1em{\hss.\hss}
        \hfill
    \ruledhbox{#1}
\stoptexdefinition
\stopbuffer

\typebuffer[option=TEX] \getbuffer

Does it look better?

\getbuffer[sample]

Indeed it does, but watch this:

\startbuffer[sample]
\startitemize[packed,joinedup][rightmargin=8.5em]
    \startitem
        \samplefile{ward}\MyFill{DW}\par
        \samplefile{ward}\par
        \samplefile{ward}\MyFill{DW}
    \stopitem
\stopitemize
\stopbuffer

\typebuffer[sample][option=TEX]

The first \type {\MyFill} will set the \type {\parfillskip} to a value that will
also be used later on.

\getbuffer[sample]

The way out is the following

\startbuffer
\starttexdefinition MyFill #1
    \begingroup
    \parfillskip-1\rightskip plus 1fil\relax
    \leaders
        \ruledhbox to 1em{\hss.\hss}
        \hfill
    \ruledhbox{#1}
    \par
    \endgroup
\stoptexdefinition
\stopbuffer

\typebuffer[option=TEX] \getbuffer

This looks more or less okay. The \type {\par} keeps the adaption local but for
it to work well, the \type {\par} must be inside the group.

\getbuffer[sample]

Now it's time to go for perfection! First of all, we get rid of any leading
spacing. If we need some we should inject it after a cleanup. We also use a
different leader command. Instead of \type {to} we use a \type {spread} so that
we get half the emwidth and not something slightly less due to the width of the
period.

\startbuffer
\starttexdefinition MyFill #1
    \removeunwantedspaces
    \begingroup
    \parfillskip-1\rightskip plus 1fil\relax
    \cleaders
        \ruledhbox spread 1em{\hss.\hss}
        \hfill
    \ruledhbox{#1}
    \par
    \endgroup
\stoptexdefinition
\stopbuffer

\typebuffer[option=TEX] \getbuffer

So, we end up here:

\startbuffer[sample]
\startitemize[packed,joinedup][rightmargin=5em]
    \startitem
        \samplefile{sapolsky}\MyFill{RS}\par
    \stopitem
\stopitemize
\stopbuffer

\getbuffer[sample]

For which we used this:

\typebuffer[sample][option=TEX]

Finally we get rid of the tracing:

\startbuffer
\starttexdefinition unexpanded MyFill #1
    \begingroup
    \parfillskip-1\rightskip plus 1fil\relax
    \leaders
        \hbox to \emwidth{\hss.\hss}
        \hfill
    \hbox{#1}
    \par
    \endgroup
\stoptexdefinition
\stopbuffer

\typebuffer[option=TEX] \getbuffer

Watch a few more details. It brings us to:

\getbuffer[sample]

\page

\startbuffer
\definefiller
  [MyFiller]
  [offset=.25\emwidth,
   method=middle]

\starttexdefinition unexpanded MyFill #1
    \begingroup
    \parfillskip-1\rightskip plus 1fil\relax
    \filler[MyFiller]%
    \hbox{#1}
    \par
    \endgroup
\stoptexdefinition
\stopbuffer

\typebuffer[option=TEX] \getbuffer

\getbuffer[sample]

When writing these examples I realized that it's rather trivial to add this
option to the already existing filler mechanism. The definition of such a filler
looks like this:

\startbuffer
\definefiller
  [MyFiller]
  [offset=.25\emwidth,
   rightmargindistance=-\rightskip,
   method=middle]
\stopbuffer

\typebuffer[option=TEX] \getbuffer

\startbuffer[sample]
\startitemize[packed,joinedup][rightmargin=5em]
    \startitem
        \samplefile{sapolsky}\fillupto[MyFiller]{RS}
    \stopitem
\stopitemize
\stopbuffer

The sample code now becomes:

\typebuffer[sample][option=TEX]

Ans as expected renders as:

\getbuffer[sample]

\stopcomponent