mag-1105-mkiv.tex /size: 6046 b    last modification: 2023-12-21 09:43
1% author    : Hans Hagen
2% copyright : ConTeXt Development Team
3% license   : Creative Commons Attribution ShareAlike 4.0 International
4% reference : pragma-ade.nl | contextgarden.net | texlive (related) distributions
5% origin    : the ConTeXt distribution
6%
7% comment   : Because this manual is distributed with TeX distributions it comes with a rather
8%             liberal license. We try to adapt these documents to upgrades in the (sub)systems
9%             that they describe. Using parts of the content otherwise can therefore conflict
10%             with existing functionality and we cannot be held responsible for that. Many of
11%             the manuals contain characteristic graphics and personal notes or examples that
12%             make no sense when used out-of-context.
13
14\bitwiseflip \normalizelinemode \plusthree
15
16\usemodule[magazine-basic,abr-02]
17
18\usemodule[scite]
19
20\startbuffer[abstract]
21    Here is yet another short example of a feature introduced in \CONTEXT\ \LMTX.
22    It is mostly meant as teaser: maybe users have additional demands for this
23    low level mechanisn.
24\stopbuffer
25
26\setuplayout
27  [backspace=20mm,
28   topspace=10mm]
29
30\startdocument
31  [title={Playing with boxes, a teaser},
32   author={Hans Hagen},
33  %affiliation=PRAGMA ADE,
34   date=August 2020,
35   number=1105 LMTX]
36
37When a paragraph is typeset in a vertical box, we get a bunch of lines pasted
38together. If you want to change something in the result, you can disassemble that
39box in detailed ways (in \LUA) but maybe it's something simple that you like to
40do. For that we have a an interface to the lines on such a box. We start with a
41few settings:
42
43\starttyping[option=TEX]
44\bitwiseflip \normalizelinemode \plusthree % indent and normalize
45\stoptyping
46
47These two settings will become default in \CONTEXT\ \LMTX, but here we need them in
48order to show some properties of boxes.
49
50\startbuffer[sample]
51\setbox0\ruledvbox \bgroup
52    \leftskip         10pt
53    \rightskip        20pt
54    \hangindent       40pt
55    \hangafter         2
56    \parindent         5pt
57    \parfillleftskip  80pt plus 1 fill
58    \parfillrightskip 40pt plus 1 fill
59    \input tufte
60\egroup
61\stopbuffer
62
63\typebuffer[sample][option=TEX]
64
65This box has a bit weird setup but we want to demonstrate something to it's okay:
66
67\startlinecorrection
68    \getbuffer[sample] \box0
69\stoplinecorrection
70
71
72In case you wonder, the left and right parskips are a \MKIV\ thing and supported
73in \LMTX\ in a more advanced way. The left one can be used to push the last line
74to the right.
75
76\startbuffer[overview]
77\dorecurse {\boxlines 0} {
78    [#1] (%
79        \the\boxlinewd 0 #1, \the\boxlineht 0 #1, \the\boxlinedp 0 #1,
80        \the\boxlinels 0 #1, \the\boxliners 0 #1,
81        \the\boxlinelh 0 #1, \the\boxlinerh 0 #1,
82        \the\boxlinein 0 #1, \the\boxlinelp 0 #1, \the\boxlinerp 0 #1
83    ) \par
84}
85\stopbuffer
86
87\typebuffer[overview][option=TEX]
88
89\getbuffer[sample]
90
91\startpacked \tt \txx
92    \getbuffer[overview]
93\stoppacked
94
95The properties of a line are: width, height and depth, left and right skip,
96applied left and right hang, indentation (of the first line), left and right
97filler of the last line. You can use the commands shown as other dimensions, so
98\type {\the} is only needed when you serialize a property.
99
100Some properties can be changed (others might follow later). Here we change the
101width of line four (the equal sign is optional):
102
103\startbuffer
104\the\boxlinewd 0 4 \space& \boxlinewd 0 4 = 40pt \the\boxlinewd 0 4
105\stopbuffer
106
107\typebuffer[option=TEX]
108
109We get: \inlinebuffer . We can detach a line from the box. Here we put two lines
110in another box that gets assigned to a box register, and a third line is assigned
111to a box register as|-|is:
112
113\startbuffer
114\setbox2\ruledhbox{\copyboxline 0 2}
115\setbox4\ruledhbox{\boxline     0 4}
116\setbox6           \boxline     0 6
117\stopbuffer
118
119\typebuffer[option=TEX] \getbuffer
120
121The result is:
122
123\startbuffer
124[2] [\box2] \par
125[4] [\box4] \par
126[6] [\box6] \par
127\stopbuffer
128
129\typebuffer[option=TEX]
130
131\startlinecorrection
132    \startpacked
133        \getbuffer
134    \stoppacked
135\stoplinecorrection
136
137You can query the natural width, height and depth too:
138
139\startbuffer
140used width  : \the\boxlinewd 0 5, natural width  : \the\boxlinenw 0 5
141used heigth : \the\boxlineht 0 5, natural height : \the\boxlinenh 0 5
142used depth  : \the\boxlinedp 0 5, natural depth  : \the\boxlinend 0 5
143\stopbuffer
144
145\typebuffer[option=TEX] \startlines \getbuffer \stoplines
146
147Next we replace an existing line by a new line:
148
149\startbuffer
150\setboxline 0 3 \hbox {\darkred \bold This is a replacement line!}
151\stopbuffer
152
153\typebuffer[option=TEX] \getbuffer
154
155The original box is now:
156
157\startlinecorrection
158\box0
159\stoplinecorrection
160
161We can repack the box with the usual \TEX\ commands: \type {\vpack {\unvbox0}}
162but here we just did a \type {\box0}.
163
164An example of a more useful application is the following. First we fill a box:
165
166\startbuffer
167\setbox\scratchbox\ruledvbox \bgroup
168    \input ward
169\egroup
170\stopbuffer
171
172\typebuffer[option=TEX] \getbuffer
173
174With little effort we can add line numbers:
175
176\startbuffer
177\ruledvbox \bgroup
178    \dorecurse {\boxlines \scratchbox} {%
179        \dontleavehmode
180        \strut
181        \llap{\hbox to 1.5em{\txx#1\hss}}\boxline \scratchbox #1\par
182    }
183}
184\stopbuffer
185
186\typebuffer[option=TEX]
187
188The result is:
189
190\startlinecorrection
191    {\forgetall \getbuffer}
192\stoplinecorrection
193
194\stopdocument
195
196% \protected\def\guessbestwidth#1#2#3%
197%   {\beginlocalcontrol
198%    \begingroup
199%    \hsize#1\relax
200%    \setbox\scratchboxone\hbox\bgroup#3\egroup
201%    \doloop {%
202%       \setbox\scratchboxtwo\vbox\bgroup\unhcopy\scratchboxone\egroup
203%       \scratchcounter\boxlines\scratchboxtwo
204%       \ifdim\boxlinenw\scratchboxtwo\scratchcounter<\boxlinewd\scratchboxtwo\scratchcounter
205%         \advance\hsize-#2\relax
206%       \else
207%         \exitloop
208%       \fi
209%    }%
210%    \normalexpanded{\endgroup\endlocalcontrol\dimexpr\the\hsize\relax}}
211%
212% \hsize \guessbestwidth{10cm}{1mm}{\input{ward}}
213%
214% \input{ward}
215%
216% \the\guessbestwidth{10cm}{1mm}{\input{ward}}
217