Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Visual Debugging in TeX - 25

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Bijlage X Visual Debugging in TEX 157

Visual Debugging in TEX


how things are done

Hans Hagen

September 25 1996

Although an integral part of CONTEXT, this module is one of the support modules. Its stand alone character permits use
in PLAIN TEX or TEX based macropackages. If in some examples the verbatim listings don’t show up nice, this is due to
processing by a system that does not support buffering. In CONTEXT we show the commands in the margin, use bit more
advanced way of numbering, and typeset the source in TEXnicolored verbatim. Sorry for this inconvenience.
This module is still in development. Depending on my personal need and those of whoever uses it, the macros will be
improved in terms of visualization, efficiency and compatibility.
1 1 \ifx \undefined \writestatus \input supp-mis.tex \fi
One of the strong points of TEX is abstraction of textual input. When macros are defined well and do what we want them
to do, we will seldom need the tools present in What You See Is What You Get systems. For instance, when entering
text we don’t need rulers, because no manual shifting and/or alignment of text is needed. On the other hand, when we
are designing macros or specifying layout elements, some insight in TEX’s advanced spacing, kerning, filling, boxing and
punishment abilities will be handy. That’s why we’ve implemented a mechanism that shows some of the inner secrets of
TEX.
2 2 \writestatus{loading}{Context Support Macros / Visualization}
In this module we are going to redefine some TEX primitives and PLAIN macro’s. Their original meaning is saved in
macros with corresponding names, preceded by normal. These original macros are (1) used to temporary restore the old
values when needed and (2) used to prevent recursive calls in the macros that replace them.
3 3 \unprotect
There are three types of boxes, one horizontal and two vertical in nature. As we will see later on, all three types are to be
handled according to their orientation and baseline behavior. Especially \vtop’s need our special attention.
4 4 \let\normalhbox = \hbox
5 \let\normalvbox = \vbox
6 \let\normalvtop = \vtop
Next come the flexible skips, which come in two flavors too. Like boxes these are handled with TEX primitives.
5 7 \let\normalhskip = \hskip
8 \let\normalvskip = \vskip
Both penalties and kerns are taken care of by mode sensitive primitives. This means that when making them visible, we
have to take the current mode into account.
6 9 \let\normalpenalty = \penalty
10 \let\normalkern = \kern
Glues on the other hand are macro’s defined in PLAIN TEX. As we will see, their definitions make the implementation of
their visible counterparts a bit more TEXnical.
7 11 \let\normalhglue = \hglue
12 \let\normalvglue = \vglue
Math mode has its own spacing primitives, preceded by m. Due to the relation with the current font and the way math is
typeset, their unit mu is not compatible with other dimensions. As a result, the visual appearance of these primitives is
kept primitive too.
8 13 \let\normalmkern = \mkern
14 \let\normalmskip = \mskip
Fills can be made visible quite easy. We only need some additional negation macros. Because PLAIN TEX only offers
\hfilneg and \vfilneg, we define our own alternative double ll’ed ones.
158 Visual Debugging in TEX Bijlage X

9 15 \def\hfillneg%
16 {\normalhskip\!!zeropoint \!!plus-1fill\relax}
10 17 \def\vfillneg%
18 {\normalvskip\!!zeropoint \!!plus-1fill\relax}

The positive stretch primitives are used independant and in combination with \leaders.
11 19 \let\normalhss = \hss
20 \let\normalhfil = \hfil
21 \let\normalhfill = \hfill
22 \let\normalvss = \vss
23 \let\normalvfil = \vfil
24 \let\normalvfill = \vfill

Keep in mind that both \hfillneg and \vfillneg are not part of PLAIN TEX and therefore not documented in
standard TEX documentation. They can nevertheless be used at will.
12 25 \let\normalhfilneg = \hfilneg
26 \let\normalhfillneg = \hfillneg
27 \let\normalvfilneg = \vfilneg
28 \let\normalvfillneg = \vfillneg

Visualization is not always wanted. Instead of turning this option off in those (unpredictable) situations, we just redefine
a few PLAIN macros.
13 29 \def\rlap#1{\normalhbox to \!!zeropoint{#1\normalhss}}
30 \def\llap#1{\normalhbox to \!!zeropoint{\normalhss#1}}
14 31 \def˜{\normalpenalty\!!tenthousand\ }

Ruled boxes can be typeset is many ways. Here we present just one alternative. This implementation may be a little
complicated, but it supports all three kind of boxes. The next command expects a hboxi specification, like:
\makeruledbox0

We can make the baseline of a box visible, both dashed and as a rule. Normally the line is drawn on top of the baseline,
but a smashed alternative is offered too. If we want them all, we just say:
\baselineruletrue
\baselinefilltrue
\baselinesmashtrue

At the cost of some overhead these alternatives are implemented using \if’s:
15 32 \newif\ifbaselinerule \baselineruletrue
33 \newif\ifbaselinefill \baselinefillfalse
34 \newif\ifbaselinesmash \baselinesmashfalse

Rules can be turned on and off, but by default we have:


\topruletrue
\bottomruletrue
\leftruletrue
\rightruletrue

As we see below:
16 35 \newif\iftoprule \topruletrue
36 \newif\ifbottomrule \bottomruletrue
37 \newif\ifleftrule \leftruletrue
38 \newif\ifrightrule \rightruletrue

The width in the surrounding rules can be specified by assigning an apropriate value to the dimension used. This module
defaults the width to:
\boxrulewidth=.2pt

Although we are already low on hdimensionsi it’s best to spend one here, mainly because it enables easy manipulation,
like multiplication by a given factor.
17 39 \newdimen\boxrulewidth \boxrulewidth=.2pt

The core macro \makeruledbox looks a bit hefty. The manipulation at the end is needed because we want to preserve
both the mode and the baseline. This means that \vtop’s and \vbox’es behave the way we expect them to do.
Bijlage X Visual Debugging in TEX 159

test
test test test
test

The \cleaders part of the macro is responsible for the visual baseline. The \normalhfill belongs to this primitive
too. By storing and restoring the height and depth of box #1, we preserve the mode.
18 40 \def\makeruledbox#1%
41 {\edef\ruledheight {\the\ht#1}%
42 \edef\ruleddepth {\the\dp#1}%
43 \edef\ruledwidth {\the\wd#1}%
44 \setbox\scratchbox=\normalvbox
45 {\dontcomplain
46 \offinterlineskip
47 \hrule
48 \!!height\boxrulewidth
49 \iftoprule\else\!!width\!!zeropoint\fi
50 \normalvskip-\boxrulewidth
51 \normalhbox to \ruledwidth
52 {\vrule
53 \!!height\ruledheight
54 \!!depth\ruleddepth
55 \!!width\ifleftrule\else0\fi\boxrulewidth
56 \ifdim\ruledheight>\!!zeropoint \else \baselinerulefalse \fi
57 \ifdim\ruleddepth>\!!zeropoint \else \baselinerulefalse \fi
58 \ifbaselinerule
59 \ifdim\ruledwidth<20\boxrulewidth
60 \baselinefilltrue
61 \fi
62 \cleaders
63 \ifbaselinefill
64 \hrule
65 \ifbaselinesmash
66 \!!height\boxrulewidth
67 \else
68 \!!height.5\boxrulewidth
69 \!!depth.5\boxrulewidth
70 \fi
71 \else
72 \normalhbox
73 {\normalhskip2.5\boxrulewidth
74 \vrule
75 \ifbaselinesmash
76 \!!height\boxrulewidth
77 \else
78 \!!height.5\boxrulewidth
79 \!!depth.5\boxrulewidth
80 \fi
81 \!!width5\boxrulewidth
82 \normalhskip2.5\boxrulewidth}%
83 \fi
84 \fi
85 \normalhfill
86 \vrule
87 \!!width\ifrightrule\else0\fi\boxrulewidth}%
88 \normalvskip-\boxrulewidth
89 \hrule
90 \!!height\boxrulewidth
91 \ifbottomrule\else\!!width\!!zeropoint\fi}%
92 \wd#1=\!!zeropoint
93 \setbox#1=\ifhbox#1\normalhbox\else\normalvbox\fi
94 {\normalhbox{\box#1\lower\ruleddepth\box\scratchbox}}%
95 \ht#1=\ruledheight
96 \wd#1=\ruledwidth
97 \dp#1=\ruleddepth}

Just in case one didn’t notice: the rules are in fact layed over the box. This way the contents of a box cannot visually
interfere with the rules around (upon) it. A more advanced version of ruled boxes can be found in one of the core modules
of CONTEXT. There we take offsets, color, rounded corners, backgrounds and alignment into account too.
160 Visual Debugging in TEX Bijlage X

These macro’s can be used instead of \hbox, \vbox and \vtop. They just do what their names state. Using an auxiliary
macro would save us a few words of memory, but it would make their appearance even more obscure.
one two three four five \hbox {\strut one two \hbox {three} four five}
19 98 \def\ruledhbox%
99 {\normalhbox\bgroup
100 \dowithnextbox{\makeruledbox\nextbox\box\nextbox\egroup}%
101 \normalhbox}

first line
second line \vbox {\strut first line \par second line \par
third line third line \par fourth line \par fifth line \strut
fourth line }
fifth line
20 102 \def\ruledvbox%
103 {\normalvbox\bgroup
104 \dowithnextbox{\makeruledbox\nextbox\box\nextbox\egroup}%
105 \normalvbox}

first line
second line \vtop {\strut first line \par second line \par
third line third line \par fourth line \par fifth line \strut
fourth line }
fifth line
21 106 \def\ruledvtop%
107 {\normalvtop\bgroup
108 \dowithnextbox{\makeruledbox\nextbox\box\nextbox\egroup}%
109 \normalvtop}

Of the next two macros the first can be used to precede a box of ones own choice. One can for instance prefix boxes with
\ruledbox and afterwards — when the macro satisfy the needs — let it to \relax.
\ruledbox\hbox{What rules do you mean?}

The macro \setruledbox can be used to directly rule a box.


\setruledbox12=\hbox{Who’s talking about rules here?}

At the cost of some extra macros we can implement a variant that does not need the =, but we stick to:
22 110 \def\ruledbox%
111 {\dowithnextbox{\makeruledbox\nextbox\box\nextbox}}
23 112 \def\setruledbox#1=%
113 {\dowithnextbox{\makeruledbox\nextbox\setbox#1=\nextbox}}

Before we meet the visualizing macro’s, we first implement ourselves some handy utility ones. Just for the sake of
efficiency and readability, we introduce some status variables, that tell us a bit more about the registers we use:
\ifflexible
\ifzero
\ifnegative
\ifpositive

These status variables are set when we call for one of the investigation macros, e.g.
\investigateskip\scratchskip

We use some dirty trick to check stretchability of hskipsi. Users of these macros are invited to study their exact behavior
first. The positive and negative states both include zero and are in fact non-negative (≥ 0) and non-positive (≤ 0) .
24 114 \newif\ifflexible
115 \newif\ifzero
116 \newif\ifnegative
117 \newif\ifpositive
25 118 \def\investigateskip#1%
119 {\relax
120 \scratchdimen=#1\relax
121 \edef\!!stringa{\the\scratchdimen}%
122 \edef\!!stringb{\the#1}%
Bijlage X Visual Debugging in TEX 161

123 \ifx\!!stringa\!!stringb \flexiblefalse \else \flexibletrue \fi


124 \ifdim#1=\!!zeropoint\relax
125 \zerotrue \else
126 \zerofalse \fi
127 \ifdim#1<\!!zeropoint\relax
128 \positivefalse \else
129 \positivetrue \fi
130 \ifdim#1>\!!zeropoint\relax
131 \negativefalse \else
132 \negativetrue \fi}
26 133 \def\investigatecount#1%
134 {\relax
135 \flexiblefalse
136 \ifnum#1=0
137 \zerotrue \else
138 \zerofalse \fi
139 \ifnum#1<0
140 \positivefalse \else
141 \positivetrue \fi
142 \ifnum#1>0
143 \negativefalse \else
144 \negativetrue \fi}
27 145 \def\investigatemuskip#1%
146 {\relax
147 \edef\!!stringa{\the\scratchmuskip}%
148 \edef\!!stringb{0mu}%
149 \def\!!stringc##1##2\\{##1}%
150 \expandafter\edef\expandafter\!!stringc\expandafter
151 {\expandafter\!!stringc\!!stringa\\}%
152 \edef\!!stringd{-}%
153 \flexiblefalse
154 \ifx\!!stringa\!!stringb
155 \zerotrue
156 \negativefalse
157 \positivefalse
158 \else
159 \zerofalse
160 \ifx\!!stringc\!!stringd
161 \positivefalse
162 \negativetrue
163 \else
164 \positivetrue
165 \negativefalse
166 \fi
167 \fi}
Indentation, left and/or right skips, redefinition of \par and assignments to \everypar can lead to unwanted results.
We can therefore turn all those things off with \dontinterfere.
28 168 \def\dontinterfere%
169 {\everypar = {}%
170 \let\par = \endgraf
171 \parindent = \!!zeropoint
172 \parskip = \!!zeropoint
173 \leftskip = \!!zeropoint
174 \rightskip = \!!zeropoint
175 \relax}
In this module we do a lot of box manipulations. Because we don’t want to be confronted with to many over- and underfull
messages we introduce \dontcomplain.
29 176 \def\dontcomplain%
177 {\hbadness = \!!tenthousand
178 \hfuzz = \maxdimen
179 \vbadness = \!!tenthousand
180 \vfuzz = \maxdimen}
Now the neccessary utility macros are defined, we can make a start with the visualizing ones. The implementation of
these macros is a compromise between readability, efficiency of coding and processing speed. Sometimes we do in steps
what could have been done in combination, sometimes we use a few boxes more or less then actually needed, and more
than once one can find the same piece of rule drawing code twice.
162 Visual Debugging in TEX Bijlage X

Depending on the context, one can force visual vertical cues being centered along \hsize or being put at the current
position. Although centering often looks better, we’ve chosen the second alternative as default. The main reason for doing
so is that often when we don’t set the \hsize ourselves, TEX takes the value of the surrounding box. As a result the
visual cues can migrate outside the current context.
This behavior is accomplished by a small but effective auxiliary macro, which behavior can be influenced by the boolean
\centeredvcue. By saying
\centeredvcuetrue
one turns centering on. As said, we turn it off.
30 181 \newif\ifcenteredvcue \centeredvcuefalse
31 182 \def\normalvcue#1%
183 {\normalhbox \ifcenteredvcue to \hsize \fi {\normalhss#1\normalhss}}
We could have used the more robust version
\def\normalvcue%
{\normalhbox \ifcenteredvcue to \hsize \fi
\bgroup\bgroup\normalhss
\aftergroup\normalhss\aftergroup\egroup
\let\next=}
or the probably best one:
\def\normalvcue%
{\hbox \ifcenteredvcue to \hsize
\bgroup\bgroup\normalhss
\aftergroup\normalhss\aftergroup\egroup
\else
\bgroup
\fi
\let\next=}
Because we don’t have to preserve hcatcodesi and only use small arguments, we stick to the first alternative.
We build our visual cues out of rules. At the cost of a much bigger DVI file, this is to be prefered over using characters
(1) because we cannot be sure of their availability and (2) because their dimensions are fixed.
As with ruled boxes, we use a hdimensioni to specify the width of the ruled elements. This dimension defaults to:
\testrulewidth=\boxrulewidth
Because we prefer whole numbers for specifying the dimensions, we often use even multiples of \testrulewidth.
A second variable is introduced because of the stretch components of hskipsi. At the cost of some accuracy we can make
this stretch visible.
\visiblestretchtrue
32 184 \newdimen\testrulewidth \testrulewidth=\boxrulewidth
185 \newif\ifvisiblestretch \visiblestretchfalse
We start with the easiest part, the fills. The scheme we follow is visual filling – going back – normal filling. Visualizing is
implemented using \cleaders. Because the hboxi that follows this command is constructed only once, the \copy is
not really a prerequisite. We prefer using a \normalhbox here instead of a \hbox.
33 186 \def\setvisiblehfilbox#1\to#2#3#4%
187 {\setbox#1=\normalhbox
188 {\vrule
189 \!!width#2\testrulewidth
190 \!!height#3\testrulewidth
191 \!!depth#4\testrulewidth}%
192 \smashbox#1}
34 193 \def\doruledhfiller#1#2#3#4%
194 {#1#2%
195 \bgroup
196 \dontinterfere
197 \dontcomplain
198 \setvisiblehfilbox0\to{4}{#3}{#4}%
199 \setvisiblehfilbox2\to422%
200 \copy0\copy2
201 \bgroup
Bijlage X Visual Debugging in TEX 163

202 \setvisiblehfilbox0\to422%
203 \cleaders
204 \normalhbox to 12\testrulewidth
205 {\normalhss\copy0\normalhss}%
206 #1%
207 \egroup
208 \setbox0=\normalhbox
209 {\normalhskip-4\testrulewidth\copy0\copy2}%
210 \smashbox0
211 \box0
212 \egroup}
The horizontal fillers differ in their boundary visualization. Watch the small dots. Fillers can be combined within reason-
able margins.
\hss test
\hfil test
\hfill test
\hfil\hfil test \hfil
The negative counterparts are visualizes, but seldom become visible, apart from their boundaries.
\hfilneg test
\hfillneg test
Although leaders are used for visualizing, they are visualized themselves correctly as the next example shows.
◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦
All five substitutions use the same auxiliary macro. Watch the positive first – negative next approach.
35 213 \def\ruledhss%
214 {\doruledhfiller\normalhss\normalhfilneg{0}{0}}
36 215 \def\ruledhfil%
216 {\doruledhfiller\normalhfil\normalhfilneg{10}{-6}}
37 217 \def\ruledhfill%
218 {\doruledhfiller\normalhfill\normalhfillneg{18}{-14}}
38 219 \def\ruledhfilneg%
220 {\doruledhfiller\normalhfilneg\normalhfil{-6}{10}}
39 221 \def\ruledhfillneg%
222 {\doruledhfiller\normalhfillneg\normalhfill{-14}{18}}

The vertical mode commands adopt the same visualization scheme, but are implemented in a slightly different way.
40 223 \def\setvisiblevfilbox#1\to#2#3#4%
224 {\setbox#1=\normalvcue
225 {\vrule
226 \!!width#2\testrulewidth
227 \!!height#3\testrulewidth
228 \!!depth#4\testrulewidth}%
229 \smashbox#1}%
41 230 \def\doruledvfiller#1#2#3%
231 {#1#2%
232 \bgroup
233 \dontinterfere
234 \dontcomplain
235 \offinterlineskip
236 \setvisiblevfilbox0\to422%
237 \setbox2=\normalhbox
238 {\normalhskip -#3\testrulewidth\copy0}%
239 \smashbox2
240 \copy2
241 \bgroup
242 \setbox2=\normalhbox
243 {\normalhskip -2\testrulewidth\copy0}%
244 \smashbox2
245 \copy2
164 Visual Debugging in TEX Bijlage X

246 \cleaders
247 \normalvbox to 12\testrulewidth
248 {\normalvss\copy2\normalvss}%
249 #1%
250 \setbox2=\normalvbox
251 {\vskip-2\testrulewidth\copy2}%
252 \smashbox2
253 \box2
254 \egroup
255 \setbox2=\normalvbox
256 {\vskip-2\testrulewidth\copy2}%
257 \smashbox2
258 \box2
259 \egroup}
Because they act the same as their horizontal counterparts we only show a few examples.
\vss \vfil \vfill \vfilneg \vfillneg

last line last line last line last line last line
Keep in mind that \vfillneg is not part of PLAIN TEX, but are mimmicked by a macro.
42 260 \def\ruledvss%
261 {\doruledvfiller\normalvss\normalvfilneg{2}}
43 262 \def\ruledvfil%
263 {\doruledvfiller\normalvfil\normalvfilneg{-4}}
44 264 \def\ruledvfill%
265 {\doruledvfiller\normalvfill\normalvfillneg{-12}}
45 266 \def\ruledvfilneg%
267 {\doruledvfiller\normalvfilneg\normalvfil{8}}
46 268 \def\ruledvfillneg%
269 {\doruledvfiller\normalvfillneg\normalvfill{16}}
Skips differ from kerns in two important aspects:
• line and pagebreaks are allowed at a skip
• skips can have a positive and/or negative stretchcomponent

Stated a bit different: kerns are fixed skips at which no line or pagebreak can occur. Because skips have a more open
character, they are visualized in a open way.
one \hskip +30pt plus 5pt two \hskip +30pt \hskip
one two three four five -10pt plus 5pt three \hskip 0pt four \hskip +30pt
five
When skips have a stretch component, this is visualized by means of a dashed line. Positive skips are on top of the
baseline, negative ones are below it. This way we can show the combined results. An alternative visualization of stretch
could be drawing the mid line over a length of the stretch, in positive or negative direction.
47 270 \def\doruledhskip%
271 {\relax
272 \dontinterfere
273 \dontcomplain
274 \investigateskip\scratchskip
275 \ifzero
276 \setbox0=\normalhbox
277 {\normalhskip-\testrulewidth
278 \vrule
279 \!!width4\testrulewidth
280 \!!height16\testrulewidth
281 \!!depth16\testrulewidth}%
282 \else
283 \setbox0=\normalhbox to \ifnegative-\fi\scratchskip
284 {\vrule
285 \!!width2\testrulewidth
286 \ifnegative\!!depth\else\!!height\fi16\testrulewidth
287 \cleaders
288 \hrule
Bijlage X Visual Debugging in TEX 165

289 \ifnegative
290 \!!depth2\testrulewidth
291 \!!height\!!zeropoint
292 \else
293 \!!height2\testrulewidth
294 \!!depth\!!zeropoint
295 \fi
296 \normalhfill
297 \ifflexible
298 \normalhskip\ifnegative\else-\fi\scratchskip
299 \normalhskip2\testrulewidth
300 \cleaders
301 \normalhbox
302 {\normalhskip 2\testrulewidth
303 \vrule
304 \!!width2\testrulewidth
305 \!!height\ifnegative-7\else9\fi\testrulewidth
306 \!!depth\ifnegative9\else-7\fi\testrulewidth
307 \normalhskip 2\testrulewidth}%
308 \normalhfill
309 \fi
310 \vrule
311 \!!width2\testrulewidth
312 \ifnegative\!!depth\else\!!height\fi16\testrulewidth}%
313 \setbox0=\normalhbox
314 {\ifnegative\else\normalhskip-\scratchskip\fi
315 \box0}%
316 \fi
317 \smashbox0%
318 \ifvisiblestretch \else
319 \flexiblefalse
320 \fi
321 \ifflexible
322 % breaks ok but small displacements can occur
323 \skip2=\scratchskip
324 \advance\skip2 by -1\scratchskip
325 \divide\skip2 by 2
326 \advance\scratchskip by -\skip2
327 \normalhskip\scratchskip
328 \normalpenalty\!!tenthousand
329 \box0
330 \normalhskip\skip2
331 \else
332 \normalhskip\scratchskip
333 \box0
334 \fi
335 \egroup}
48 336 \def\ruledhskip%
337 {\bgroup
338 \afterassignment\doruledhskip
339 \scratchskip=}
The visual skip is located at a feasible point. Normally this does not interfere with the normaltypesetting process. The
next examples show (1) the default behavior, (2) the (not entirely correct) distributed stretch and (3) the way the text is
typeset without cues.
test test testtest test testtest test testtest test testtest test testtest test testtest test
testtest test testtest test testtest test testtest test testtest test testtest test testtest test
testtest test test
test test testtest test testtest test testtest test testtest test testtest test testtest test
testtest test testtest test testtest test testtest test testtest test testtest test testtest test
testtest test test
test test testtest test testtest test testtest test testtest test testtest test testtest test
testtest test testtest test testtest test testtest test testtest test testtest test testtest test
testtest test test
We are less fortunate when implementing the vertical skips. This is a direct result of interference between the boxes that
visualize the skip and skip removal at a pagebreak. Normally skips disappear at the top of a page, but not of course
when visualized in a \vbox. A quite perfect simulation could have been built if we would have had available two more
166 Visual Debugging in TEX Bijlage X

primitives: \hnop and \vnop. These new primitives could stand for boxes that are visible but are not taken into account
in any way. They are there for us, but not for TEX.
first line

second line
first line \vskip +30pt plus 5pt second line
\vskip +30pt \vskip -10pt plus 5pt third line
third line \par fourth line \vskip +30pt fifth line \vskip
fourth line 0pt sixth line

fifth line
sixth line
We have to postpone \prevdepth. Although this precaution probably is not completely waterproof, it works quite well.
49 340 \def\dodoruledvskip%
341 {\nextdepth=\prevdepth
342 \dontinterfere
343 \dontcomplain
344 \offinterlineskip
345 \investigateskip\scratchskip
346 \ifzero
347 \setbox0=\normalvcue
348 {\vrule
349 \!!width32\testrulewidth
350 \!!height2\testrulewidth
351 \!!depth2\testrulewidth}%
352 \else
353 \setbox0=\normalvbox to \ifnegative-\fi\scratchskip
354 {\hrule
355 \!!width16\testrulewidth
356 \!!height2\testrulewidth
357 \ifflexible
358 \cleaders
359 \normalhbox to 16\testrulewidth
360 {\normalhss
361 \normalvbox
362 {\normalvskip 2\testrulewidth
363 \hrule
364 \!!width2\testrulewidth
365 \!!height2\testrulewidth
366 \normalvskip 2\testrulewidth}%
367 \normalhss}%
368 \normalvfill
369 \else
370 \normalvfill
371 \fi
372 \hrule
373 \!!width16\testrulewidth
374 \!!height2\testrulewidth}%
375 \setbox2=\normalvbox to \ht0
376 {\hrule
377 \!!width2\testrulewidth
378 \!!height\ht0}%
379 \ifnegative
380 \ht0=\!!zeropoint
381 \setbox0=\normalhbox
382 {\normalhskip2\testrulewidth % will be improved
383 \normalhskip-\wd0\box0}%
384 \fi
385 \smashbox0%
386 \smashbox2%
387 \setbox0=\normalvcue
388 {\box2\box0}%
389 \setbox0=\normalvbox
390 {\ifnegative\normalvskip\scratchskip\fi\box0}%
Bijlage X Visual Debugging in TEX 167

391 \smashbox0%
392 \fi
393 \ifvisiblestretch
394 \ifflexible
395 \skip2=\scratchskip
396 \advance\skip2 by -1\scratchskip
397 \divide\skip2 by 2
398 \advance\scratchskip by -\skip2
399 \normalvskip\skip2
400 \fi
401 \fi
402 \normalpenalty\!!tenthousand
403 \box0
404 \prevdepth=\nextdepth % not \dp0=\nextdepth
405 \normalvskip\scratchskip}
We try to avoid interfering at the top of a page. Of course we only do so when we are in the main vertical list.
50 406 \def\doruledvskip%
407 {\par
408 \ifdim\pagegoal=\maxdimen
409 \ifinner
410 \dodoruledvskip
411 \fi
412 \else
413 \dodoruledvskip
414 \fi
415 \egroup}
51 416 \def\ruledvskip%
417 {\bgroup
418 \afterassignment\doruledvskip
419 \scratchskip=}

The macros that implement the kerns are a bit more complicated than needed, because they also serve the visualization
of glue, our PLAIN defined kerns with stretch or shrink. We’ve implemented both horizontal and vertical kerns as ruled
boxes.
one \kern +30pt two \kern +30pt \kern -10pt three
one two three four five \kern 0pt four \kern +30pt five
Positive and negative kerns are placed on top or below the baseline, so we are able to track their added result. We didn’t
mention spacings of 0 pt yet. Zero values are visualized a bit different, because we want to see them anyhow.
52 420 \def\doruledhkern%
421 {\dontinterfere
422 \dontcomplain
423 \baselinerulefalse
424 \investigateskip\scratchskip
425 \boxrulewidth=2\testrulewidth
426 \ifzero
427 \setbox0=\ruledhbox to 8\testrulewidth
428 {\vrule
429 \!!width\!!zeropoint
430 \!!height16\testrulewidth
431 \!!depth16\testrulewidth}%
432 \setbox0=\normalhbox
433 {\normalhskip-4\testrulewidth\box0}%
434 \else
435 \setbox0=\ruledhbox to \ifnegative-\fi\scratchskip
436 {\vrule
437 \!!width\!!zeropoint
438 \ifnegative\!!depth\else\!!height\fi16\testrulewidth
439 \ifflexible
440 \normalhskip2\testrulewidth
441 \cleaders
442 \normalhbox
443 {\normalhskip 2\testrulewidth
444 \vrule
445 \!!width2\testrulewidth
446 \!!height\ifnegative-7\else9\fi\testrulewidth
447 \!!depth\ifnegative9\else-7\fi\testrulewidth
448 \normalhskip 2\testrulewidth}%
168 Visual Debugging in TEX Bijlage X

449 \normalhfill
450 \else
451 \normalhfill
452 \fi}%
453 \testrulewidth=2\testrulewidth
454 \setbox0=\ruledhbox{\box0}% \make...
455 \fi
456 \smashbox0%
457 \normalpenalty\!!tenthousand
458 \normalhbox to \!!zeropoint
459 {\ifnegative\normalhskip1\scratchskip\fi
460 \box0}%
461 \afterwards\scratchskip
462 \egroup}
53 463 \def\ruledhkern#1%
464 {\bgroup
465 \let\afterwards=#1\relax
466 \afterassignment\doruledhkern
467 \scratchskip=}

After having seen the horizontal ones, the vertical kerns will not surprise us. In this example we use \par to switch to
vertical mode.
first line

second line
first line \par \kern +30pt second line \par
\kern +30pt \par \kern -10pt third line \par
third line fourth line \par \kern +30pt fifth line \par
fourth line \kern 0pt sixth line

fifth line
sixth line
Like before, we have to postpone \prevdepth. If we leave out this trick, we got ourselves some wrong spacing.
54 468 \def\dodoruledvkern%
469 {\nextdepth=\prevdepth
470 \dontinterfere
471 \dontcomplain
472 \baselinerulefalse
473 \offinterlineskip
474 \investigateskip\scratchskip
475 \boxrulewidth=2\testrulewidth
476 \ifzero
477 \setbox0=\ruledhbox to 32\testrulewidth
478 {\vrule
479 \!!width\!!zeropoint
480 \!!height4\testrulewidth
481 \!!depth4\testrulewidth}%
482 \else
483 \setbox0=\ruledvbox to \ifnegative-\fi\scratchskip
484 {\hsize16\testrulewidth
485 \ifflexible
486 \cleaders
487 \normalhbox to 16\testrulewidth
488 {\normalhss
489 \normalvbox
490 {\normalvskip 2\testrulewidth
491 \hrule
492 \!!width2\testrulewidth
493 \!!height2\testrulewidth
494 \normalvskip 2\testrulewidth}%
495 \normalhss}%
496 \normalvfill
497 \else
498 \vrule
Bijlage X Visual Debugging in TEX 169

499 \!!width\!!zeropoint
500 \!!height\ifnegative-\fi\scratchskip
501 \normalhfill
502 \fi}
503 \fi
504 \testrulewidth=2\testrulewidth
505 \setbox0=\ruledvbox{\box0}% \make...
506 \smashbox0%
507 \setbox0=\normalvbox
508 {\ifnegative\normalvskip\scratchskip\fi
509 \normalvcue
510 {\ifnegative\normalhskip-16\testrulewidth\fi\box0}}%
511 \smashbox0%
512 \normalpenalty\!!tenthousand
513 \box0
514 \prevdepth=\nextdepth} % not \dp0=\nextdepth

55 515 \def\doruledvkern%
516 {\ifdim\pagegoal=\maxdimen
517 \ifinner
518 \dodoruledvkern
519 \fi
520 \else
521 \dodoruledvkern
522 \fi
523 \afterwards\scratchskip
524 \egroup}

56 525 \def\ruledvkern#1%
526 {\bgroup
527 \let\afterwards=#1\relax
528 \afterassignment\doruledvkern
529 \scratchskip=}

57 530 \def\ruledkern%
531 {\ifvmode
532 \let\next=\ruledvkern
533 \else
534 \let\next=\ruledhkern
535 \fi
536 \next\normalkern}

The non-primitive glue commands are treated as kerns with stretch. This stretch is presented as a dashed line. I have to
admit that until now, I’ve never used these glue commands.

one \hglue +30pt plus 5pt two \hglue +30pt \hglue


one two three four five -10pt plus 5pt three \hglue 0pt four \hglue +30pt
five

58 537 \def\doruledhglue%
538 {\leavevmode
539 \scratchcounter=\spacefactor
540 \vrule\!!width\!!zeropoint
541 \normalpenalty\!!tenthousand
542 \ruledhkern\normalhskip\scratchskip
543 \spacefactor=\scratchcounter
544 \egroup}

59 545 \def\ruledhglue%
546 {\bgroup
547 \afterassignment\doruledhglue\scratchskip=}
170 Visual Debugging in TEX Bijlage X

first line

second line
first line \vglue +30pt plus 5pt second line
\vglue +30pt \vglue -10pt plus 5pt third line
third line \par fourth line \vglue +30pt fifth line \vglue
fourth line 0pt sixth line

fifth line
sixth line
60 548 \def\doruledvglue%
549 {\par
550 \nextdepth=\prevdepth
551 \hrule\!!height\!!zeropoint
552 \normalpenalty\!!tenthousand
553 \ruledvkern\normalvskip\scratchskip
554 \prevdepth=\nextdepth
555 \egroup}
61 556 \def\ruledvglue%
557 {\bgroup
558 \afterassignment\doruledvglue\scratchskip=}
Mathematical kerns and skips are specified in mu. This font related unit is incompatible with those of hdimensionsi and
hskipsi. Because in math mode spacing is often a very subtle matter, we’ve used a very simple, not overloaded way to
show them.
62 559 \def\dodoruledmkern#1%
560 {\dontinterfere
561 \dontcomplain
562 \setbox0=\normalhbox
563 {$\normalmkern\ifnegative-\fi\scratchmuskip$}%
564 \setbox0=\normalhbox to \wd0
565 {\vrule
566 \!!height16\testrulewidth
567 \!!depth16\testrulewidth
568 \!!width\testrulewidth
569 \leaders
570 \hrule
571 \!!height\ifpositive16\else-14\fi\testrulewidth
572 \!!depth\ifpositive-14\else16\fi\testrulewidth
573 \normalhfill
574 \ifflexible
575 \normalhskip-\wd0
576 \leaders
577 \hrule
578 \!!height\testrulewidth
579 \!!depth\testrulewidth
580 \normalhfill
581 \fi
582 \vrule
583 \!!height16\testrulewidth
584 \!!depth16\testrulewidth
585 \!!width\testrulewidth}%
586 \smashbox0%
587 \ifnegative
588 #1\scratchmuskip
589 \box0
590 \else
591 \box0
592 #1\scratchmuskip
593 \fi
594 \egroup}
$a \mkern 3mu = \mkern 3mu b \quad \mkern -2mu
a = b + c
+ \mkern -2mu \quad c$
63 595 \def\doruledmkern%
Bijlage X Visual Debugging in TEX 171

596 {\investigatemuskip\scratchmuskip
597 \flexiblefalse
598 \dodoruledmkern\normalmkern}
64 599 \def\ruledmkern%
600 {\bgroup
601 \afterassignment\doruledmkern\scratchmuskip=}
$a \mskip 3mu = \mskip 3mu b \quad \mskip -2mu
a = b + c
+ \mskip -2mu \quad c$
65 602 \def\doruledmskip%
603 {\investigatemuskip\scratchmuskip
604 \flexibletrue
605 \dodoruledmkern\normalmskip}
66 606 \def\ruledmskip%
607 {\bgroup
608 \afterassignment\doruledmskip\scratchmuskip=}
After presenting fills, skip, kerns and glue we’ve come to see penalties. In the first implementation — most of the time
needed to develop this set of macros went into testing different types of visualization — penalties were mere small blocks
with one black half, depending on the sign. This most recent version also gives an indication of the amount of penalty.
Penalties can go from less than −10000 to over +10000, and their behavior is somewhat non-lineair, with some values
having special meanings. We therefore decided not to use its value for a lineair indicator.
one \penalty +100 two \penalty +100 \penalty
one two three four five -100 three \penalty 0 four \penalty +100 five
The small sticks at the side of the penalty indicate it size. The next example shows the positive and negative penalties of
0, 1, 10, 100, 1000 and 10000.
test test test test test test test
test test test test test test test
This way stacked penalties of different severance can be shown in combination.
test test test test
67 609 \def\setruledpenaltybox#1#2#3#4#5#6%
610 {\setbox#1=\normalhbox
611 {\ifnum#2=0 \else
612 \ifnum#2>0
613 \def\sign{+}%
614 \else
615 \def\sign{-}%
616 \fi
617 \dimen0=\ifnum\sign#2>9999
618 28\else
619 \ifnum\sign#2>999
620 22\else
621 \ifnum\sign#2>99
622 16\else
623 \ifnum\sign#2>9
624 10\else
625 4
626 \fi\fi\fi\fi \testrulewidth
627 \ifnum#2<0
628 \normalhskip-\dimen0
629 \normalhskip-2\testrulewidth
630 \vrule
631 \!!width2\testrulewidth
632 \!!height#3\testrulewidth
633 \!!depth#4\testrulewidth
634 \fi
635 \vrule
636 \!!width\dimen0
637 \!!height#5\testrulewidth
638 \!!depth#6\testrulewidth
639 \ifnum#2>0
640 \vrule
641 \!!width2\testrulewidth
642 \!!height#3\testrulewidth
172 Visual Debugging in TEX Bijlage X

643 \!!depth#4\testrulewidth
644 \fi
645 \fi}%
646 \smashbox#1}
68 647 \def\doruledhpenalty%
648 {\dontinterfere
649 \dontcomplain
650 \investigatecount\scratchcounter
651 \testrulewidth=2\testrulewidth
652 \boxrulewidth=\testrulewidth
653 \setbox0=\ruledhbox to 8\testrulewidth
654 {\ifnegative\else\normalhss\fi
655 \vrule
656 \!!depth8\testrulewidth
657 \!!width\ifzero0\else4\fi\testrulewidth
658 \ifpositive\else\normalhss\fi}%
659 \setruledpenaltybox{2}{\scratchcounter}{0}{8}{-3.5}{4.5}%
660 \normalpenalty\!!tenthousand
661 \setbox0=\normalhbox
662 {\normalhskip-4\testrulewidth
663 \ifnegative
664 \box2\box0
665 \else
666 \box0\box2
667 \fi}%
668 \smashbox0%
669 \box0
670 \normalpenalty\scratchcounter
671 \egroup}
69 672 \def\ruledhpenalty%
673 {\bgroup
674 \afterassignment\doruledhpenalty
675 \scratchcounter=}
The size of a vertical penalty is also shown on the horizontal axis. This way there is less interference with the often
preceding or following skips and kerns.
first line
first line \par \penalty +100 second line \par
second line
\penalty +100 \par \penalty -100 third line \par
third line
\penalty 0 fourth line \par \penalty +100 fifth
fourth line
line
fifth line
70 676 \def\doruledvpenalty%
677 {\ifdim\pagegoal=\maxdimen
678 \else
679 \nextdepth=\prevdepth
680 \dontinterfere
681 \dontcomplain
682 \investigatecount\scratchcounter
683 \testrulewidth=2\testrulewidth
684 \boxrulewidth=\testrulewidth
685 \setbox0=\ruledhbox
686 {\vrule
687 \!!height4\testrulewidth
688 \!!depth4\testrulewidth
689 \!!width\!!zeropoint
690 \vrule
691 \!!height\ifnegative.5\else4\fi\testrulewidth
692 \!!depth\ifpositive.5\else4\fi\testrulewidth
693 \!!width8\testrulewidth}%
694 \setruledpenaltybox{2}{\scratchcounter}{4}{4}{.5}{.5}%
695 \setbox0=\normalhbox
696 {\normalhskip-4\testrulewidth
697 \ifnegative
698 \box2\box0
699 \else
700 \box0\box2
701 \fi
702 \normalhss}%
Bijlage X Visual Debugging in TEX 173

703 \smashbox0%
704 \normalpenalty\!!tenthousand
705 \nointerlineskip
706 \dp0=\nextdepth % not \prevdepth=\nextdepth
707 \normalvbox
708 {\normalvcue{\box0}}%
709 \fi
710 \normalpenalty\scratchcounter
711 \egroup}
71 712 \def\ruledvpenalty%
713 {\bgroup
714 \afterassignment\doruledvpenalty
715 \scratchcounter=}
72 716 \def\ruledpenalty%
717 {\ifvmode
718 \let\next=\ruledvpenalty
719 \else
720 \let\next=\ruledhpenalty
721 \fi
722 \next}

For those who want to manipulate the visual cues in detail, we have grouped them.
73 723 \def\showfils%
724 {\let\hss = \ruledhss
725 \let\hfil = \ruledhfil
726 \let\hfill = \ruledhfill
727 \let\hfilneg = \ruledhfilneg
728 \let\hfillneg = \ruledhfillneg
729 \let\vss = \ruledvss
730 \let\vfil = \ruledvfil
731 \let\vfill = \ruledvfill
732 \let\vfilneg = \ruledvfilneg
733 \let\vfillneg = \ruledvfillneg}
74 734 \def\dontshowfils%
735 {\let\hss = \normalhss
736 \let\hfil = \normalhfil
737 \let\hfill = \normalhfill
738 \let\hfilneg = \normalhfilneg
739 \let\hfillneg = \normalhfillneg
740 \let\vss = \normalvss
741 \let\vfil = \normalvfil
742 \let\vfill = \normalvfill
743 \let\vfilneg = \normalvfilneg
744 \let\vfillneg = \normalvfillneg}
75 745 \def\showboxes%
746 {\baselineruletrue
747 \let\hbox = \ruledhbox
748 \let\vbox = \ruledvbox
749 \let\vtop = \ruledvtop}
76 750 \def\dontshowboxes%
751 {\let\hbox = \normalhbox
752 \let\vbox = \normalvbox
753 \let\vtop = \normalvtop}
77 754 \def\showskips%
755 {\let\hskip = \ruledhskip
756 \let\vskip = \ruledvskip
757 \let\kern = \ruledkern
758 \let\mskip = \ruledmskip
759 \let\mkern = \ruledmkern
760 \let\hglue = \ruledhglue
761 \let\vglue = \ruledvglue}
78 762 \def\dontshowskips%
763 {\let\hskip = \normalhskip
764 \let\vskip = \normalvskip
765 \let\kern = \normalkern
766 \let\mskip = \normalmskip
174 Visual Debugging in TEX Bijlage X

767 \let\mkern = \normalmkern


768 \let\hglue = \normalhglue
769 \let\vglue = \normalvglue}
79 770 \def\showpenalties%
771 {\let\penalty = \ruledpenalty}
80 772 \def\dontshowpenalties%
773 {\let\penalty = \normalpenalty}
All these nice options come together in two macros. The first one turns the options on, the second turnes them off. Both
macros only do their job when we are actually showing the composition.
\showingcompositiontrue
\showcomposition
Because the output routine can do tricky things, like multiple column typesetting and manipulation of the pagebody,
shifting things around and so on, the macro \dontshowcomposition best can be called when we enter this routine.
Too much visual cues just don’t make sense. In CONTEXT this has been taken care of.
81 774 \newif\ifshowingcomposition
82 775 \def\showcomposition%
776 {\ifshowingcomposition
777 \showfils
778 \showboxes
779 \showskips
780 \showpenalties
781 \fi}
83 782 \def\dontshowcomposition%
783 {\ifshowingcomposition
784 \dontshowfils
785 \dontshowboxes
786 \dontshowskips
787 \dontshowpenalties
788 \fi}

Just to make things even more easy, we have defined:


\showmakeup
For the sake of those who don’t (yet) use CONTEXT we preset \defaulttestrulewidth to the already set value.
Otherwise we default to a corps related value.
\def\defaulttestrulewidth{.2pt}
Beware, it’s a macro not a hdimensioni.
84 789 \ifx\korpsgrootte\undefined
790 \edef\defaulttestrulewidth{\the\testrulewidth}
791 \else
792 \def\defaulttestrulewidth{.02\korpsgrootte} % still dutch
793 \fi
85 794 \def\showmakeup%
795 {\testrulewidth=\defaulttestrulewidth
796 \showingcompositiontrue
797 \showcomposition}
86 798 \protect
The documented source you have been reading was processed using some surrogate makeup. When this file is processed
in CONTEXT, a few more examples show up here, like a local table of contents and a local register.

You might also like