Debian Codegraph @

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

raw
1 % \subsection{Row dispatcher}
2
3 % \begin{macro}{\cals@row@dispatch}
4 % Depending if the current row has a rowspan cell or not,
5 % the execution is different.
6 % \begin{macrocode}
7 \newcommand\cals@row@dispatch{%
8 \ifx b\cals@current@context
9 \cals@ifInRspan\iftrue
10 \cals@row@dispatch@span
11 \else
12 \cals@row@dispatch@nospan
13 \fi
14 \else
15 \cals@row@dispatch@nospan
16 \fi}
17 % \end{macrocode}
18 % \end{macro}
19
20 % \begin{macro}{\cals@row@dispatch@nospan}
21 % After a row is typeset in a box, this macro decides what to do next.
22 % Usually, it should just add decorations and output the row.
23 % But if a table break is required, it should put the current row
24 % to backup, typeset the footer, the break, the header and only then
25 % the row from the backup. Summary of main parameters:
26 % \begin{itemize}
27 % \item rowsep from the last row (|\cals@last@rs|) and the
28 % last context (|\cals@last@context|)
29 % \item current row (|\cals@current@row|), its decorations
30 % (|\cals@current@cs|, |\cals@current@rs@above|,
31 % |\cals@current@rs@below|) and context (|\cals@current@context|)
32 % \end{itemize}
33 % \begin{macrocode}
34 \newcommand\cals@row@dispatch@nospan{%
35 % \end{macrocode}
36 % The header and footer rows are always typeset without further
37 % considerations.
38 % \begin{macrocode}
39 \let\cals@last@context@bak=\cals@last@context
40 \ifx h\cals@current@context \else
41 \ifx f\cals@current@context \else
42 % \end{macrocode}
43 % In the body, if a break is required: do it.
44 % \begin{macrocode}
45 \cals@ifbreak\iftrue
46 \setbox\cals@backup@row=\box\cals@current@row
47 \setbox\cals@backup@cs=\box\cals@current@cs
48 \let\cals@backup@rs@above=\cals@current@rs@above
49 \let\cals@backup@rs@below=\cals@current@rs@below
50 \let\cals@backup@context=\cals@current@context
51 \cals@tfoot@tokens
52 \lastrule
53 \cals@issue@break
54 \cals@thead@tokens
55 \setbox\cals@current@row=\box\cals@backup@row
56 \setbox\cals@current@cs=\box\cals@backup@cs
57 \let\cals@current@rs@above=\cals@backup@rs@above
58 \let\cals@current@rs@below=\cals@backup@rs@below
59 \let\cals@current@context=\cals@backup@context
60 \fi\fi\fi
61 % \end{macrocode}
62 % Typeset the row. If the width of the row is more than hsize, then
63 % the issue-code should not fit the row to hsize.
64 % \begin{macrocode}
65 \ifdim\wd\cals@current@row>\hsize\relax
66 \def\cals@tohsize{}%
67 \fi
68 \cals@issue@row
69 % \end{macrocode}
70 % Consider a table such that thead+row1 do not fit to a page
71 % (see the unit test |regression/test_010_wrongbreak|).
72 % Without the next code, the following happens:
73 % thead and row1 are typeset, but the output procedure is not
74 % executed yet. Therefore, when row2 is ready, we detect that
75 % a table break is required and create it. Then the output procedure
76 % moves thead+row1 on the next page. The result:
77 % thead and row1 on one page, row2 and the rest on the next page
78 % instead of the whole table on one page.
79 % Solution: force a run of the output procedure after
80 % the first row of a table chunk.
81 % \begin{macrocode}
82 \ifx b\cals@last@context
83 {\dimen0=\pagetotal\relax
84 \advance\dimen0 by \cals@tfoot@height\relax
85 \advance\dimen0 by -\pagegoal
86 \ifdim\dimen0>0pt\relax
87 \vskip\dimen0
88 \penalty9999 % with 10000, the output page builder is not called
89 \vskip-\dimen0\relax
90 \fi
91 }%
92 \fi
93 }
94 % \end{macrocode}
95 % \end{macro}
96
97 % \begin{macro}{\cals@row@dispatch@span}
98 % The only specific thing to rowspanned rows is that we should not
99 % allow breaks between the rows in one group. We put these rows
100 % to one box, and process this big box as a big row.
101 % \begin{macrocode}
102 \newcommand\cals@row@dispatch@span{%
103 % \end{macrocode}
104 % Output the row to the backup box. If the row is the first
105 % row in the span, let its decorations will be the decorations
106 % for the future big row. Also, reset the values of leftskip
107 % and rightskip to avoid adding them twice, once in a individual
108 % row, and once to the common span box.
109 % \begin{macrocode}
110 \ifvoid\cals@backup@row
111 \setbox\cals@backup@row=\vbox{\box\cals@current@row}%
112 \setbox\cals@backup@cs=\box\cals@current@cs
113 \let\cals@backup@rs@above=\cals@current@rs@above
114 \let\cals@backup@last@rs@below=\cals@last@rs@below
115 \let\cals@backup@context=\cals@last@context
116 \cals@backup@leftskip=\leftskip\relax
117 \cals@backup@rightskip=\rightskip\relax
118 \let\cals@backup@tohsize=\cals@tohsize
119 \leftskip=0pt\relax \rightskip=0pt\relax \def\cals@tohsize{}%
120 \else
121 \setbox\cals@backup@row=\vbox{\unvbox\cals@backup@row
122 \cals@issue@row}%
123 \fi
124 \let\cals@last@rs@below=\cals@current@rs@below
125 \let\cals@last@context=\cals@current@context
126 % \end{macrocode}
127 % If this is the last row of the span, create the fake big row
128 % and use the normal dispatcher.
129 %
130 % \begin{macrocode}
131 \cals@ifLastRspanRow\iftrue
132 \setbox\cals@current@row=\box\cals@backup@row
133 \setbox\cals@current@cs=\box\cals@backup@cs
134 \let\cals@current@rs@above=\cals@backup@rs@above
135 \let\cals@last@rs@below=\cals@backup@last@rs@below
136 \let\cals@last@context=\cals@backup@context
137 \leftskip=\cals@backup@leftskip
138 \rightskip=\cals@backup@rightskip
139 \let\cals@tohsize=\cals@backup@tohsize
140 \cals@row@dispatch@nospan
141 \fi
142 }
143 % \end{macrocode}
144 % \end{macro}
145
146 % \begin{macro}{\cals@backup@row}
147 % \begin{macro}{\cals@backup@cs}
148 % Boxes and skips for backup.
149 % \begin{macrocode}
150 \newbox\cals@backup@row
151 \newbox\cals@backup@cs
152 \newskip\cals@backup@leftskip
153 \newskip\cals@backup@rightskip
154 % \end{macrocode}
155 % \end{macro}
156 % \end{macro}
157
158 % To decide on table breaks and row separation decorations,
159 % we need to trace context.
160
161 % \begin{macro}{\cals@current@context}
162 % The context of the current row. Possible values,
163 % set as a "|\let|" to a character:
164 % \begin{itemize}
165 % \item n: no context, should not happen when the value is required
166 % \item h: table header
167 % \item f: table footer
168 % \item b: table body
169 % \end{itemize}
170 % \end{macro}
171
172 % \begin{macro}{\cals@last@context}
173 % The context of the previous row. Possible values, set as a
174 % "|\let|" to a character:
175 % \begin{itemize}
176 % \item n: there is no previous row (not only the start of a table,
177 % but also the start of a table chunk)
178 % \item h, f, b: table header, footer, body
179 % \item r: a last rule of the table (or its chunk) is just output.
180 % This status is used to allow multiple calls to |\lastrule|.
181 % Probably the use of |current| instead of |last| is more logical,
182 % but using |last| is more safe. Who knows if an user decides to use
183 % |\lastrule| somewhere in a middle of a table.
184 % \end{itemize}
185 % \end{macro}
186
187 % \begin{macro}{\cals@ifbreak}
188 % Table breaks can be manual or automatic. The first is easy,
189 % the second is near to impossible if we take into account
190 % table headers and footer. The following heuristic seems good.
191 %
192 % Check if the current row plus the footer fits to the
193 % rest of the page. If not, a break is required. This approach
194 % is based on two assumptions:
195 % \begin{itemize}
196 % \item the height of the footer is always the same, and
197 % \item any body row is larger than the footer.
198 % \end{itemize}
199 %
200 % More precise and technical description: |\cals@ifbreak| decides
201 % if an automatic table break is required and leaves the
202 % macro |\cals@iftrue| (yes) or |\cals@iffalse| (no) in the input stream.
203 % If the user sets |\cals@tbreak@tokens| (using |\tbreak|),
204 % break is forced. Otherwise, no break is allowed:
205 % \begin{itemize}
206 % \item inside a box (= inside a float)
207 % \item In the header
208 % \item In the footer
209 % \item Immediately after the header
210 % \item At the beginning of a chunk of a table.
211 % \end{itemize}
212 % Otherwise break is recommended when
213 % the sum of the height of the current row and of the footer part
214 % is greater as the rest height of the page.
215 % The implicit first parameter is used for if-fi balancing,
216 % see |\cals@iftrue|.
217 % \begin{macrocode}
218 \newcommand\cals@ifbreak[1]{}
219 \def\cals@ifbreak{%
220 \let\cals@tmp=\cals@iffalse
221 \let\cals@tmpII=\cals@iftrue
222 \ifx\relax\cals@tbreak@tokens
223 \ifinner\else
224 \ifx h\cals@current@context \else
225 \ifx f\cals@current@context \else
226 \ifx h\cals@last@context \else
227 \ifx n\cals@last@context \else
228 \dimen0=\pagetotal\relax
229 \advance\dimen0 by \ht\cals@current@row\relax
230 %\showthe\ht\cals@current@row\relax
231 \ifx \cals@tfoot@tokens\relax \else
232 %\show\cals@tfoot@height\relax
233 \advance\dimen0 by \cals@tfoot@height\relax
234 \fi
235 %\showthe\dimen0\relax
236 \ifdim \dimen0>\pagegoal\relax
237 \let\cals@tmp=\cals@tmpII
238 \fi
239 \fi\fi\fi\fi\fi
240 \else \let\cals@tmp=\cals@tmpII % tbreak@tokens
241 \fi
242 \cals@tmp}
243 % \end{macrocode}
244 % \end{macro}
245
246 % \begin{macro}{\cals@issue@break}
247 % By default, force a page break, otherwise use user's tokens
248 % set by |\tbreak|.
249 % \begin{macrocode}
250 \newcommand\cals@issue@break{\ifx \relax\cals@tbreak@tokens \penalty-10000 %
251 \else \cals@tbreak@tokens \fi
252 \let\cals@tbreak@tokens=\relax
253 \let\cals@last@context=n}
254 % \end{macrocode}
255 % \end{macro}
256
257 % \begin{macro}{\cals@set@tohsize}
258 % \begin{macro}{\cals@tohsize}
259 % Table row contains not only the row itself, but also |\leftskip|
260 % and |\rightskip|. Now the dilemma. If the row is just |\hbox|,
261 % than the glue component is ignored, and the table always aligned
262 % left. On the other side, if the row is |\hbox to \hsize|, then
263 % the user gets underfulled boxes. A simple solution is to
264 % switch on and off the |hsize|-part depending on the skips.
265 % \begin{macrocode}
266 \newcommand\cals@tohsize{}
267 \newcommand\cals@set@tohsize{\def\cals@tohsize{}%
268 \ifnum\gluestretchorder\leftskip>0\relax \def\cals@tohsize{to \hsize}\fi
269 \ifnum\gluestretchorder\rightskip>0\relax \def\cals@tohsize{to \hsize}\fi
270 }
271 % \end{macrocode}
272 % \end{macro}
273 % \end{macro}
274
275 % \begin{macro}{\cals@activate@rtl}
276 % \begin{macro}{\cals@deactivate@rtl}
277 % \begin{macro}{\cals@hbox}
278 % For bidi support, use |\hboxR| instead of |\hbox|.
279 % Actually, more is required for bidi support, and
280 % these macros are retained only as "legacy".
281 % Otherwise I'd move the code into the beginning of `calstable`.
282 % \begin{macrocode}
283 \newcommand\cals@hbox{}
284 \newcommand\cals@activate@rtl{\let\cals@hbox=\hboxR}
285 \newcommand\cals@deactivate@rtl{\let\cals@hbox=\hbox}
286 \cals@deactivate@rtl
287 % \end{macrocode}
288 % \end{macro}
289 % \end{macro}
290 % \end{macro}
291
292 % \begin{macro}{\cals@issue@rowsep@alone}
293 % Typesets the top (or bottom) frame of a table:
294 % combines |\cals@current@rs@above| and |\cals@framers@width|
295 % and outputs the row separator.
296 % \begin{macrocode}
297 \newcommand\cals@issue@rowsep@alone{%
298 \setbox0=\cals@hbox\cals@tohsize{%
299 \cals@hskip@lr\leftskip\rightskip
300 \cals@rs@sofar@reset
301 \cals@rs@joinOne\cals@framers@width\cals@current@rs@above
302 \cals@rs@sofar@end
303 \cals@hskip@lr\rightskip\leftskip}%
304 \ht0=0pt \dp0=0pt \box0 }
305 % \end{macrocode}
306 % \end{macro}
307
308 % \begin{macro}{\cals@issue@rowsep}
309 % Combine row separations |\cals@last@rs@below| and |\cals@current@rs@above|,
310 % taking into considiration the width of the rule:
311 % \begin{itemize}
312 % \item n to h, f, b (the top frame): use |\cals@framers@width|
313 % and ignore |last@rs@below| because we don't have it
314 % \item h to h, b to b, f to f (the usual separator): use |\cals@rs@width|
315 % \item for all other combinations (header to body, body to footer),
316 % including impossible: use |\cals@bodyrs@width|
317 % \end{itemize}
318 % \begin{macrocode}
319 \newcommand\cals@issue@rowsep{%
320 \ifx n\cals@last@context \cals@issue@rowsep@alone \else
321 \ifx \cals@last@context\cals@current@context
322 \let\cals@tmpIII=\cals@rs@width \else
323 \let\cals@tmpIII=\cals@bodyrs@width \fi
324 \setbox0=\cals@hbox\cals@tohsize{%
325 \cals@hskip@lr\leftskip\rightskip
326 \cals@rs@sofar@reset
327 \cals@rs@joinTwo\cals@tmpIII\cals@last@rs@below\cals@current@rs@above
328 \cals@rs@sofar@end
329 \cals@hskip@lr\rightskip\leftskip}%
330 \ht0=0pt \dp0=0pt \box0 %
331 \fi}
332 % \end{macrocode}
333 % \end{macro}
334
335
336 % \begin{macro}{\cals@last@row@height}
337 % For spanning support, we need to remember the height of the last row
338 % \begin{macrocode}
339 \newdimen\cals@last@row@height
340 % \end{macrocode}
341 % \end{macro}
342
343 % \begin{macro}{\cals@issue@row}
344 % Typesets the current row and its decorations, then updates
345 % the last context. Regards |\leftskip| and |\rightskip|
346 % by putting them inside the row.
347 % \begin{macrocode}
348 \newcommand\cals@issue@row{%
349 % \end{macrocode}
350 % Decorations: first the column separation, then the row separation.
351 % \begin{macrocode}
352 \nointerlineskip
353 \setbox0=\vtop{\cals@hbox\cals@tohsize{\cals@hskip@lr\leftskip\rightskip
354 \box\cals@current@cs \cals@hskip@lr\rightskip\leftskip}}%
355 \ht0=0pt\relax\box0
356 \nointerlineskip
357 \cals@issue@rowsep
358 \nointerlineskip
359 % \end{macrocode}
360 % Output the row, update the last context.
361 % \begin{macrocode}
362 \cals@hbox\cals@tohsize{\cals@hskip@lr\leftskip\rightskip
363 \box\cals@current@row \cals@hskip@lr\rightskip\leftskip}%
364 \let\cals@last@rs@below=\cals@current@rs@below
365 \let\cals@last@context=\cals@current@context
366 \nobreak}
367 % \end{macrocode}
368 % \end{macro}
369
370 % \subsection{Table elements}
371
372 % \begin{environment}{\calstable}
373 % Setup the parameters and let the row dispatcher to do all the work.
374 % \begin{macrocode}
375 \newenvironment{calstable}[1][\cals@table@alignment]{%
376 \if@RTL\@RTLtabtrue\cals@activate@rtl\fi
377 \let\cals@thead@tokens=\relax
378 \let\cals@tfoot@tokens=\relax
379 \let\cals@tbreak@tokens=\relax
380 \cals@tfoot@height=0pt \relax
381 \let\cals@last@context=n%
382 \let\cals@current@context=b%
383 \parindent=0pt \relax%
384 \cals@setup@alignment{#1}%
385 \cals@setpadding{Ag}\cals@setcellprevdepth{Al}\cals@set@tohsize%
386 %% Alignment inside is independent on center/flushright outside
387 \parfillskip=0pt plus1fil\relax
388 \let\cals@borderL=\relax
389 \let\cals@borderR=\relax
390 \let\cals@borderT=\relax
391 \let\cals@borderB=\relax
392 % \end{macrocode}
393 % Bug fix for \url{http://tex.stackexchange.com/questions/167400/fancyhdr-and-cals-vertical-merge-problem}.
394 % Table inside a table is ok, but when there are 1) page break inside
395 % a table in the text flow, and 2) a table with a vertically straddled
396 % cell is created in the output procedure, then this table inside table
397 % needs additional cleaning.
398 % \begin{macrocode}
399 \setbox\cals@backup@row=\box\voidb@x\relax
400 \cals@AtBeginTable
401 }{% End of the table
402 \cals@tfoot@tokens\lastrule\cals@AtEndTable}
403 % \end{macrocode}
404 % \end{environment}
405
406 % \begin{macro}{\cals@AtBeginTable}
407 % \begin{macro}{\cals@AtEndTable}
408 % Callbacks for more initialization possibilities.
409 % \begin{macrocode}
410 \newcommand\cals@AtBeginTable{}%
411 \newcommand\cals@AtEndTable{}%
412 % \end{macrocode}
413 % \end{macro}
414 % \end{macro}
415
416 % \begin{macro}{\lastrule}
417 % Typesets the last rule (bottom frame) of a table chunk.
418 % Repeatable calls are ignored.
419 % Useful in the macro |\tfoot|.
420 % \begin{macrocode}
421 \newcommand\lastrule{%
422 \ifx r\cals@last@context \relax \else
423 \let\cals@last@context=r%
424 \nointerlineskip
425 \let\cals@current@rs@above=\cals@last@rs@below\cals@issue@rowsep@alone%
426 \fi}
427 % \end{macrocode}
428 % \end{macro}
429
430 % \begin{macro}{\thead}
431 % Table: the header. Remember for later use, typeset right now.
432 % \begin{macrocode}
433 \newcommand\thead[1]{%
434 \def\cals@thead@tokens{\let\cals@current@context=h%
435 #1\let\cals@current@context=b}%
436 \cals@thead@tokens}
437 % \end{macrocode}
438 % \end{macro}
439
440 % \begin{macro}{\tfoot}
441 % Table: the footer. Remember for later use. Right now, typeset
442 % to a box to calculate an expected height for the table breaker
443 % |\cals@ifbreak|.
444 % \begin{macrocode}
445 \newcommand\tfoot[1]{%
446 \def\cals@tfoot@tokens{\let\cals@current@context=f#1}%
447 \setbox0=\vbox{\cals@tfoot@tokens}%
448 \cals@tfoot@height=\ht0 \relax}
449 % \end{macrocode}
450 % \end{macro}
451
452 % \begin{macro}{\cals@tfoot@height}
453 % The height of the footer.
454 % \begin{macrocode}
455 \newdimen\cals@tfoot@height
456 % \end{macrocode}
457 % \end{macro}
458
459 % \begin{macro}{\tbreak}
460 % Table: force a table break. Argument should contain something
461 % like |\penalty-10000 |.
462 % \begin{macrocode}
463 \newcommand\tbreak[1]{\def\cals@tbreak@tokens{#1}}
464 % \end{macrocode}
465 % \end{macro}
466
467 % \begin{macro}{\cals@table@alignment}
468 % The default alignment of tables in the text flow.
469 % Doesn't affect the text alignment inside cells.
470 % \begin{itemize}
471 % \item |n|: no settings, the default |\leftskip| and |\rightskip| are used
472 % \item |l|: align left
473 % \item |c|: align center
474 % \item |r|: align right
475 % \end{itemize}
476 % This setting is appeared in the version 2.3. Earlier versions
477 % worked as it were |n|.
478 % \begin{macrocode}
479 \newcommand\cals@table@alignment{l}
480 % \end{macrocode}
481 % \end{macro}