Debian Codegraph @

texmf-dist/source/latex/cals/rowsep.dtx

raw
1 % \subsection{Row separation (rowsep)}
2
3 % \subsubsection{Data presentation}
4
5 % A horizontal line between table rows can't appear in the output
6 % immediately. Its formatting should be postponed until the next
7 % row and its context are known. Two basic cases:
8 % \begin{itemize}
9 % \item The default cell formatting is to have a border around cells.
10 % For some cell \textit{A} in the middle of a table, the user has
11 % overridden formatting to no borders. To imlement the user's wish,
12 % we should not create the bottom border for the cell \textit{B}
13 % which is above the \textit{A}. But we don't know about the wish
14 % for \textit{A} while processing \textit{B}. Therefore, the border
15 % between two rows can be established only after processing the
16 % both rows.
17 % \item The default cell border is 1pt, but a border between
18 % a body and a header or a footer row is 2pt. It means that the
19 % border between two rows should be created only after we sure
20 % that there is no table break.
21 % \end{itemize}
22 % Our approach is to define the desired formatting of a rowsep
23 % as a set of parameters in a token list. Later we can join two
24 % rowseps or a rowsep and context to a final rowsep.
25 %
26 % A rowsep token list consist of several items, each
27 % item is a list of tokens or token groups:
28 % \begin{enumerate}
29 % \item length
30 % \item left-border
31 % \item right-border
32 % \item user-specified thickness
33 % \end{enumerate}
34 % Values \textit{left-border} and \textit{right-border} are
35 % required to get a nice rectangle border around a cell. Without length
36 % correction, using the cell dimension, the border could look like:
37 % \begin{verbatim}
38 % xxxxxxxx
39 % xxx cell xxx
40 % xxxxxxxx
41 % \end{verbatim}
42 % With length correction, we get the correct result:
43 % \begin{verbatim}
44 % xxxxxxxxxxxx
45 % xxx cell xxx
46 % xxxxxxxxxxxx
47 % \end{verbatim}
48 %
49 % Here is an example of a rowsep specification. It consist of three items.
50 % The first item: length is 5cm, width 2pt, borders are 2mm.
51 % The second item: length is 9cm, borders are 2mm, no rule at all.
52 % The third item: length 2cm, default width, borders are 2pt.
53 % The token list for this specification:
54 % \begin{verbatim}
55 % { {5cm} {2mm} {2mm} {2pt} }
56 % { {9cm} {2mm} {2mm} {0pt} }
57 % { {2cm} {2pt} {2pt} \relax }
58 % \end{verbatim}
59 %
60 % Cell length and the left and right borders should be the correct lengths,
61 % the rule thickness can be |\relax|, in this case the actual thickness
62 % will be calculated during output.
63 %
64 % \begin{macro}{\cals@rs@pack}
65 % Construct a rowsep fragment from the arguments 2-5 and
66 % put it to the macro 1.
67 % \begin{macrocode}
68 \newcommand\cals@rs@pack[5]{%
69 \edef#1{\noexpand{#2\noexpand}\noexpand{#3\noexpand}\noexpand{#4\noexpand}%
70 \ifx \relax#5\relax \else \noexpand{#5\noexpand}\fi }}
71 % \end{macrocode}
72 % \end{macro}
73
74 % \begin{macro}{\cals@rs@unpack}
75 % The reverse for |\cals@rs@pack|. The first argument is a rowsep
76 % fragment (without enclosing curly braces), arguments 2-5 is macro
77 % names where to put the results.
78 % \begin{macrocode}
79 \newcommand\cals@rs@unpack[5]{%
80 \def\cals@tmp##1##2##3##4{\edef#2{##1}\edef#3{##2}\edef#4{##3}%
81 \ifx\relax##4\let#5=\relax \else \edef#5{##4}\fi}%
82 \expandafter\cals@tmp#1}
83 % \end{macrocode}
84 % \end{macro}
85
86 %
87 % ^^A -----------------------------------------------------
88 %
89 % \subsubsection{From individual decorations to rowsep specification}
90
91 % The rowsep specifications are created cell-by-cell and stored
92 % in the macros |\cals@current@rs@above| and |\cals@current@rs@below|.
93 % The construction happens with delay because we don't know
94 % the exact value of the right border until the next cell is processed.
95
96 % \begin{macro}{\cals@rs@spec@begin}
97 % Initializes |\cals@current@rs@above|, |\cals@current@rs@below|
98 % and set the flag of a new row.
99 % \begin{macrocode}
100 \newcommand\cals@rs@spec@begin{%
101 \def\cals@current@rs@above{}%
102 \def\cals@current@rs@below{}%
103 \let\cals@rs@spec@ll=\relax}
104 % \end{macrocode}
105 % \end{macro}
106
107 % \begin{macro}{\cals@rs@spec@next}
108 % \begin{macro}{\cals@rs@spec@nextII}
109 % Finalizes the decorations for the previous cell by using the left border
110 % of the current as the right border for the previous. Then
111 % remembers the decorations of the current cell ---
112 % the left border width,
113 % the widths of the top and bottom borders (|\relax| is ok) ---
114 % in the macros |\cals@rs@spec@ll|, |...@bl|, |...@bt|, |...@bb|.
115 % All the arguments much be macros.
116 % \begin{macrocode}
117 \newcommand\cals@rs@spec@next[4]{
118 \cals@rs@spec@nextII#2
119 \let\cals@rs@spec@ll=#1%
120 \let\cals@rs@spec@bl=#2%
121 \let\cals@rs@spec@bt=#3%
122 \let\cals@rs@spec@bb=#4%
123 }
124
125 \newcommand\cals@rs@spec@nextII[1]{%
126 \ifx \relax\cals@rs@spec@ll \else
127 \cals@rs@pack\cals@tmp\cals@rs@spec@ll\cals@rs@spec@bl#1\cals@rs@spec@bt
128 \llt@snoc\cals@current@rs@above\cals@tmp
129 \cals@rs@pack\cals@tmp\cals@rs@spec@ll\cals@rs@spec@bl#1\cals@rs@spec@bb
130 \llt@snoc\cals@current@rs@below\cals@tmp
131 \fi
132 }
133 % \end{macrocode}
134 % \end{macro}
135 % \end{macro}
136
137 % \begin{macro}{\cals@rs@spec@end}
138 % Finishes the rowsep specification by putting the last cell to it.
139 % The only implicit argument (|\cals@lastLeftWidth|) is the width
140 % of the right border of the last cell.
141 % \begin{macrocode}
142 \newcommand\cals@rs@spec@end[1]{}
143 \let\cals@rs@spec@end=\cals@rs@spec@nextII
144 % \end{macrocode}
145 % \end{macro}
146
147 %
148 % ^^A -----------------------------------------------------
149 %
150 % \subsubsection{``Waiting'' rowsep}
151 %
152 % \begin{macro}{\cals@rs@sofar@length}
153 % \begin{macro}{\cals@rs@sofar@borderl}
154 % \begin{macro}{\cals@rs@sofar@borderr}
155 % \begin{macro}{\cals@rs@sofar@width}
156 % Typesetting a row separator is not an easy task, especially
157 % because we support border-widths. Indeed, consider the worst
158 % case: four cells and all the borders are different.
159 % Our solution is an optimizer for a good case. We do not typeset
160 % a fragment of the rule immediately. Instead, we remember the
161 % parameters. If the next fragment is of the same width,
162 % we increase the length of the ``waiting'' fragment.
163 % Otherwise, we output the waiting fragment and the new fragment
164 % becomes the new waiting fragment.
165 % \begin{macrocode}
166 \newcommand\cals@rs@sofar@length{}
167 \newcommand\cals@rs@sofar@borderl{}
168 \newcommand\cals@rs@sofar@borderr{}
169 \newcommand\cals@rs@sofar@width{}
170 % \end{macrocode}
171 % \end{macro}
172 % \end{macro}
173 % \end{macro}
174 % \end{macro}
175
176 % \begin{macro}{\cals@rs@sofar@reset}
177 % Sets a flag that a new waiting rule should be started.
178 % \begin{macrocode}
179 \newcommand\cals@rs@sofar@reset{\let\cals@rs@sofar@width=\relax}
180 % \end{macrocode}
181 % \end{macro}
182
183 % \begin{macro}{\cals@rs@sofar@end}
184 % Prints the waiting rule, if exists.
185 % \begin{macrocode}
186 \newcommand\cals@rs@sofar@end{\ifx\relax\cals@rs@sofar@width
187 \else\cals@rs@sofar@out\fi}
188 % \end{macrocode}
189 % \end{macro}
190
191 % \begin{macro}{\cals@rs@sofar@next}
192 % Enlarges the current waiting rule, or typesets it and
193 % starts new if the widths do not match. All the parameters
194 % must be macro names. In order: length, left border, right
195 % border, width.
196 % \begin{macrocode}
197 \newcommand\cals@rs@sofar@next[4]{%
198 \ifx\relax\cals@rs@sofar@width
199 % \end{macrocode}
200 % Starts a new waiting rule.
201 % \begin{macrocode}
202 \let\cals@rs@sofar@length=#1%
203 \let\cals@rs@sofar@borderl=#2%
204 \let\cals@rs@sofar@borderr=#3%
205 \let\cals@rs@sofar@width=#4%
206 \else
207 \ifdim \cals@rs@sofar@width=#4\relax
208 % \end{macrocode}
209 % Enlarges the waiting rule.
210 % \begin{macrocode}
211 \dimen0=\cals@rs@sofar@length\relax
212 \advance\dimen0 by #1\relax
213 \edef\cals@rs@sofar@length{\the\dimen0}%
214 \let\cals@rs@sofar@borderr=#3%
215 \else
216 % \end{macrocode}
217 % Typesets the current and start a new waiting rule.
218 % \begin{macrocode}
219 \cals@rs@sofar@out
220 \let\cals@rs@sofar@length=#1%
221 \let\cals@rs@sofar@borderl=#2%
222 \let\cals@rs@sofar@borderr=#3%
223 \let\cals@rs@sofar@width=#4%
224 \fi
225 \fi}
226 % \end{macrocode}
227 % \end{macro}
228
229 % \begin{macro}{\cals@rs@sofar@over}
230 % Repeats the last rowsep fragment, probably with another settings.
231 % Arguments are like in |\cals@rs@sofar@next|.
232 % \begin{macrocode}
233 \newcommand\cals@rs@sofar@over[4]{%
234 \ifdim 0pt=#4
235 \relax
236 \else
237 \ifdim \cals@rs@sofar@width=#4\relax
238 % \end{macrocode}
239 % The width is not changed. We probably need to enlarge the right border
240 % and probably the left border too. The latter is a bit harder because
241 % we don't want to change it if the line continues from another cell
242 % (so, change only if |length|+|borderl|\textgreater |sofar@length|+|sofar@borderl|).
243 % \begin{macrocode}
244 \ifdim #3>\cals@rs@sofar@borderr\relax
245 \edef\cals@rs@sofar@borderr{#3}%
246 \fi
247 \dimen0=\cals@rs@sofar@length
248 \advance\dimen0 by \cals@rs@sofar@borderl\relax
249 \advance\dimen0 by -#2\relax
250 \ifdim #1>\dimen0 \relax
251 \edef\cals@rs@sofar@borderl{#2}%
252 \fi
253 \else
254 % \end{macrocode}
255 % Typesets the current and start a new waiting rule.
256 % \begin{macrocode}
257 \cals@rs@sofar@out
258 \hskip-#1\relax
259 \let\cals@rs@sofar@length=#1%
260 \let\cals@rs@sofar@borderl=#2%
261 \let\cals@rs@sofar@borderr=#3%
262 \let\cals@rs@sofar@width=#4%
263 \fi
264 \fi}
265 % \end{macrocode}
266 % \end{macro}
267
268 % \begin{macro}{\cals@rs@sofar@out}
269 % Typesets the waiting rule
270 % \begin{macrocode}
271 \newcommand\cals@rs@sofar@out{%
272 \ifdim 0pt=\cals@rs@sofar@width\relax
273 \hskip\cals@rs@sofar@length\relax
274 \else
275 \cals@halfWidthToDimen0\cals@rs@sofar@borderl
276 \hskip-\dimen0\relax
277 \cals@halfWidthToDimen2\cals@rs@sofar@borderr
278 \dimen4=\cals@rs@sofar@length\relax
279 \advance\dimen4 by \dimen0\relax \advance\dimen4 by \dimen2\relax
280 \cals@halfWidthToDimen6\cals@rs@sofar@width
281 \vrule height\dimen6 depth\dimen6 width\dimen4\relax
282 \hskip-\dimen2\relax
283 \fi}
284 % \end{macrocode}
285 % \end{macro}
286
287 % ^^A -----------------------------------------------------
288 %
289 % \subsubsection{From rowsep specification to typesetting}
290
291 % \begin{macro}{\cals@rs@joinTwo}
292 % Join and typeset two rowseps (arguments 2 and 3, must be macro names).
293 % The number and the lengths of the fragments in the rowseps
294 % should match.
295 % The argument 1 (also a macro name) is the default width.
296 % Corrupts the macros 2 and 3.
297 % Call this macro inside |sofar@reset|...|@end| group.
298 % \begin{macrocode}
299 \newcommand\cals@rs@joinTwo[3]{%
300 % \end{macrocode}
301 % The loop function.
302 % \begin{macrocode}
303 \def\next##1{%
304 \ifx \eol##1\let\next=\relax
305 \else
306 \toks0=\expandafter{##1}%
307 \edef\cals@tmpII{\the\toks0}%
308 \llt@decons#3%
309 % \end{macrocode}
310 % Now |\cals@tmpII| contains a current fragment of the first rowsep,
311 % and |\llt@car| of the second. Unpack the individual parameters.
312 % \begin{macrocode}
313 \cals@rs@unpack\cals@tmpII\cals@tmpLI \cals@tmpBlI \cals@tmpBrI \cals@tmpWI
314 \cals@rs@unpack\llt@car \cals@tmpLII\cals@tmpBlII\cals@tmpBrII\cals@tmpWII
315 % \end{macrocode}
316 % The special case is when we should not typeset a rowsep fragment.
317 % \begin{macrocode}
318 \let\cals@tmp=\cals@iftrue
319 \cals@maxWidth\cals@tmpWI\cals@tmpWII
320 \ifx \relax\cals@width\else \ifdim \cals@width=0pt %
321 \cals@rs@sofar@next\cals@tmpLI\cals@tmpBlI\cals@tmpBrI\cals@width
322 \let\cals@tmp=\cals@iffalse
323 \fi\fi
324 % \end{macrocode}
325 % Not the special case. Put the both definitions, and let the
326 % underlying functions take care of calculations.
327 % \begin{macrocode}
328 \cals@tmp\ifvoid
329 \cals@widthII#1\cals@tmpWI
330 \cals@rs@sofar@next\cals@tmpLI\cals@tmpBlI\cals@tmpBrI\cals@width
331 \cals@widthII#1\cals@tmpWII
332 \cals@rs@sofar@over\cals@tmpLII\cals@tmpBlII\cals@tmpBrII\cals@width
333 \fi
334 \fi
335 % \end{macrocode}
336 % End of |\next| definition: continue the loop.
337 % End of |\cals@rs@joinTwo| definition: start the loop.
338 % \begin{macrocode}
339 \next}%
340 \expandafter\next#2\eol}
341 % \end{macrocode}
342 % \end{macro}
343
344 % \begin{macro}{\cals@rs@joinOne}
345 % A simplified version of the previous macro. We have only one rowsep,
346 % and want to join and typeset it with regard to some width,
347 % given as the first macro parameter. Call this macro inside
348 % |sofar@reset|...|@end| group.
349 % \begin{macrocode}
350 \newcommand\cals@rs@joinOne[2]{%
351 \def\next##1{\ifx\eol##1\let\next=\relax\else
352 \toks0=\expandafter{##1}%
353 \edef\cals@tmpII{\the\toks0}%
354 \cals@rs@unpack\cals@tmpII\cals@tmpL\cals@tmpBl\cals@tmpBr\cals@tmpW
355 \cals@widthII#1\cals@tmpW
356 \cals@rs@sofar@next\cals@tmpL\cals@tmpBl\cals@tmpBr\cals@width
357 \fi\next}%
358 \expandafter\next#2\eol}
359 % \end{macrocode}
360 % \end{macro}