Quiq/LaTeX/LongTable.pm

filter: Statementstmt 83.6% 92 / 110 Branchbran 50.0% 39 / 78 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 67.5% 148 / 219 CC 45 SCAR 47.7
CC
45
Cov
68%
latex
142.0
__ANON__
2.0
BEGIN
1.0
CRAP 118.1
« Quiq/Schedule.pm ↑ Summary Quiq/Eog.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::LaTeX::LongTable - Erzeuge LaTeX longtable
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 Der Code
16  
17 use Quiq::LaTeX::LongTable;
18 use Quiq::LaTeX::Code;
19
20 my $tab = Quiq::LaTeX::LongTable->new(
21 alignments => ['l','r','c'],
22 caption => 'Ein Test',
23 titles => ['Links','Rechts','Zentriert'],
24 rows => [
25 ['A',1,'AB'],
26 ['AB',2,'CD'],
27 ['ABC',3,'EF'],
28 ['ABCD',4,'GH'],
29 ],
30 );
31
32 my $l = Quiq::LaTeX::Code->new;
33 my $code = $tab->latex($l);
34  
35 produziert
36  
37 \begin{longtable}{|lrc|}
38 \hline
39 Links & Rechts & Zentriert \\ \hline
40 \endfirsthead
41 \multicolumn{3}{r}{\emph{Fortsetzung}} \
42 \hline
43 Links & Rechts & Zentriert \\ \hline
44 \endhead
45 \hline
46 \multicolumn{3}{r}{\emph{weiter nächste Seite}} \
47 \endfoot
48 \caption{Ein Test}
49 \endlastfoot
50 A & 1 & AB \\ \hline
51 AB & 2 & CD \\ \hline
52 ABC & 3 & EF \\ \hline
53 ABCD & 4 & GH \\ \hline
54 \end{longtable}
55  
56 was im LaTeX-Dokument in etwa so aussieht
57  
58 +--------------------------+
59 | Links Rechts Zentriert |
60 +--------------------------+
61 | A 1 AB |
62 +--------------------------+
63 | AB 12 CD |
64 +--------------------------+
65 | ABC 123 EF |
66 +--------------------------+
67 | ABCD 1234 GH |
68 +--------------------------+
69
70 Tabelle 1: Ein Test
71  
72 =cut
73  
74 # -----------------------------------------------------------------------------
75  
76 package Quiq::LaTeX::LongTable;
77 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 1 our $VERSION = '1.240';
85  
86 # -----------------------------------------------------------------------------
87  
88 =head1 METHODS
89  
90 =head2 Konstruktor
91  
92 =head3 new() - Instantiiere LaTeX LongTable-Objekt
93  
94 =head4 Synopsis
95  
96 $tab = $class->new(@keyVal);
97  
98 =head4 Arguments
99  
100 =over 4
101  
102 =item align => $align (Default: 'c')
103  
104 Horizontale Ausrichtung der Tabelle auf der Seite. Mögliche Werte:
105 'l', 'c'.
106  
107 =item alignments => \@alignments (Default: [])
108  
109 Liste der Kolumnen-Ausrichtungen. Mögliche Werte je Kolumne: 'l',
110 'r', 'c'.
111  
112 =item border => $border (Default: 'hHV' oder 'hvHV' bei multiLine)
113  
114 Linien in und um die Tabelle. Der Wert ist eine Zeichenkette, die
115 sich aus den Zeichen 't', 'h', 'v', 'H', 'V' zusammensetzt.
116  
117 =item callbackArguments => \@arr (Default: [])
118  
119 Liste von zusätzlichen Argumenten, die an die Funktionen
120 C<rowCallback> und C<titleCallback> übergeben werden.
121  
122 =item caption => $str
123  
124 Unterschrift zur Tabelle.
125  
126 =item indent => $length
127  
128 Einrückung der Tabelle vom linken Rand. Die Option C<align> darf
129 dann nicht gesetzt sein, auch nicht auf 'l'.
130  
131 =item label => $str
132  
133 Label der Tabelle, über welches sie referenziert werden kann.
134  
135 =item language => 'german'|'english'
136  
137 Die Sprache des LaTeX-Dokuments.
138  
139 =item multiLine => $bool (Default: undef)
140  
141 Wende C<\makecell> auf alle Kolumnen an. Diese Option
142 muss aktiviert werden, wenn mehrzeilige Zellen mehrzeilig
143 dargestellt werden sollen, denn dies kann LaTeX nicht. Wird die
144 Option aktiviert, muss das Package C<makecell> geladen werden.
145  
146 =item rows => \@rows (Default: [])
147  
148 Liste der Tabellenzeilen.
149  
150 =item rowCallback => sub {} (Default: I<siehe unten>)
151  
152 Subroutine, die für jede Zeile in @rows die Zeileninformation
153 liefert, die in den LaTeX-Code eingesetzt wird. Default:
154  
155 sub {
156 my ($self,$l,$row,$n) = @_;
157
158 my @row;
159 for my $val (@$row) {
160 push @row,$l->protect($val);
161 }
162
163 return @row;
164 }
165  
166 =item titleColor => $color
167  
168 Farbe der Titelzeile.
169  
170 =item titleWrapper => $code
171  
172 Zeichenkette, die um jeden Kolumnentitel gelegt wird. Für C<%s>
173 wird der Titel eingesetzt. Auf diesem Weg kann ein Makro
174 auf jeden Titel angewendet werden. Z.B. serifenlosen, fetten Font
175 einstellen:
176  
177 titleWrapper => '\textsf{\textbf{%s}}'
178  
179 =item titles => \@titles (Default: [])
180  
181 Liste der Kolumnentitel.
182  
183 =item titleCallback => sub {} (Default: I<siehe unten>)
184  
185 Subroutine, die die Titelinformation liefert, die in den
186 LaTeX-Code eingesetzt wird. Default:
187  
188 sub {
189 my ($self,$l,$title,$n) = @_;
190 return $l->protect($title);
191 }
192  
193 =back
194  
195 =head4 Returns
196  
197 LongTable-Objekt
198  
199 =head4 Description
200  
201 Instantiiere ein LaTeX LongTable-Objekt und liefere eine Referenz
202 auf dieses Objekt zurück.
203  
204 =cut
205  
206 # -----------------------------------------------------------------------------
207  
208 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
209 2 my $class = shift;
210 # @_: @keyval
211  
212 my $self = $class->SUPER::new(
213 align => 'c',
214 alignments => [],
215 border => undef,
216 callbackArguments => [],
217 caption => undef,
218 indent => undef,
219 label => undef,
220 language => 'german',
221 multiLine => undef,
222 postVSpace => undef,
223 rows => [],
224 4 ▶ rowCallback => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
225 4 my ($self,$l,$row,$n) = @_;
226  
227 4 my @row;
228 4 for my $val (@$row) {
229 12 push @row,$l->protect($val);
230 }
231  
232 4 return @row;
233 },
234 titleColor => undef,
235 titleWrapper => undef,
236 titles => [],
237 3 ▶ titleCallback => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
238 3 my ($self,$l,$title,$n) = @_;
239 3 return $l->protect($title);
240 },
241 2 );
242 2 $self->set(@_);
243  
244 2 return $self;
245 }
246  
247 # -----------------------------------------------------------------------------
248  
249 =head2 Objektmethoden
250  
251 =head3 latex() - Generiere LaTeX-Code
252  
253 =head4 Synopsis
254  
255 $code = $tab->latex($l);
256 $code = $class->latex($l,@keyVal);
257  
258 =head4 Description
259  
260 Generiere den LaTeX-Code des Objekts und liefere diesen
261 zurück. Als Klassenmethode gerufen, wird das Objekt intern erzeugt
262 und mit den Attributen @keyVal instantiiert.
263  
264 =cut
265  
266 # -----------------------------------------------------------------------------
267  
268 2 ▶sub latex {
Subroutine latex: called CC=44 SCAR=49.6
Pod documented
269 2 my $this = shift;
270 2 my $l = shift;
271  
272 2 ▶ my $self = ref $this? $this: $this->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 2 0 2
273  
274 2 my ($align,$alignA,$border,$cbArguments,$caption,$indent,$label,$language,
275 $multiLine,$postVSpace,$rowA,$rowCb,$titleColor,$titleWrapper,
276 $titleA,$titleCb) = $self->get(qw/align alignments border
277 callbackArguments caption indent label language multiLine postVSpace
278 rows rowCallback titleColor titleWrapper titles titleCallback/);
279  
280 2 ▶ if (!@$titleA && !@$rowA) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$titleA or @$rowA) 1 1 2
Condition66%2/3
@$titleA or @$rowA
l!l&&r!l&&!r
101
MC/DC50%1/2
@$titleA or @$rowA
@$titleA @$rowA
Truth table66%2/3
A or B
A= @$titleAB= @$rowA
ABresult
1X1
011
000
281 1 return '';
282 }
283  
284 1 my $body;
285  
286 # Anzahl Kolumnen
287 1 my $width = @$alignA;
288  
289 # Alignment
290  
291 1 ▶ if ($align) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align) 1 0 1
292 1 $align = substr $align,0,1; # nur erstes Zeichen
293 }
294  
295 # Default-Umrandung
296  
297 1 ▶ if (!defined $border) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $border) 1 0 1
298 1 ▶ $border = $multiLine? 'hvHV': 'hHV';
Branch: 1 branch
BranchTrueFalseTotal
$multiLine ? : 0 1 1
299 }
300  
301 # Linien
302
303 1 ▶ my $tBorder = $border =~ tr/th//? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$border =~ tr/ht// ? : 1 0 1
304 1 ▶ my $hBorder = $border =~ tr/h//? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$border =~ tr/h// ? : 1 0 1
305 1 ▶ my $HBorder = $border =~ tr/H//? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$border =~ tr/H// ? : 1 0 1
306 1 ▶ my $vBorder = $border =~ tr/v//? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$border =~ tr/v// ? : 0 1 1
307 1 ▶ my $VBorder = $border =~ tr/V//? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$border =~ tr/V// ? : 1 0 1
308  
309 # Titelfarbe
310  
311 1 ▶ if ($titleColor && $titleColor !~ s/^#//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($titleColor and not $titleColor =~ s/^#//) 0 1 1
Condition33%1/3
$titleColor and not $titleColor =~ s/^#//
!ll&&!rl&&r
100
MC/DC0%0/2
$titleColor and not $titleColor =~ s/^#//
$titleColor not $titleColor =~ s/^#//
Truth table33%1/3
A and not(B)
A= $titleColorB= not $titleColor =~ s/^#//
ABresult
0X0
100
111
312 0 $self->throw(
313 'LATEX-00001: Only RGB color allowed',
314 Color => $titleColor,
315 );
316 }
317  
318 # Head
319  
320 1 my $titleLine = '';
321 1 ▶ if (@$titleA) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$titleA) 1 0 1
322 1 my @arr;
323 1 ▶ for (my $i = 0; $i < @$titleA; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$titleA) 3 1 4
324 3 my $val = $titleCb->($self,$l,$titleA->[$i],$i,@$cbArguments);
325 # Wir setzen den Titel auch mehrzeilig, wenn $multiLine
326 # nicht gesetzt ist, aber \n in einem Titel vorhanden ist
327 3 ▶ if ($multiLine || grep {m|\n|} @$titleA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiLine or grep {/\n/;} @$titleA) 0 3 3
Condition33%1/3
$multiLine or grep {/\n/;} @$titleA
l!l&&r!l&&!r
003
MC/DC0%0/2
$multiLine or grep {/\n/;} @$titleA
$multiLine grep {/\n/;} @$titleA
Truth table33%1/3
A or B
A= $multiLineB= grep {/\n/;} @$titleA
ABresult
1X1
011
000
328 0 $val =~ s|\n|\\\\|g;
329 0 $val = $l->ci('\makecell[%sb]{%s}',$alignA->[$i],$val);
330 0 ▶ if ($titleColor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($titleColor) 0 0 0
331 # Hack, damit der gesamte Hintergrund farbig wird.
332 # Ist nur bei \makecell nötig.
333 0 $val = $l->ci('{\setlength{\fboxsep}{0pt}'.
334 '\colorbox[HTML]{%s}{%s}}',$titleColor,$val);
335 }
336 }
337 3 ▶ if ($titleWrapper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($titleWrapper) 0 3 3
338 0 $val = $l->ci($titleWrapper,$val);
339 }
340 3 push @arr,$val;
341 }
342 1 ▶ if ($titleColor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($titleColor) 0 1 1
343 0 $titleLine .= $l->ci('\rowcolor[HTML]{%s} ',$titleColor);
344 }
345 1 $titleLine .= join(' & ',@arr).' \\\\';
346 1 ▶ $titleLine .= $tBorder? ' \hline': '';
Branch: 1 branch
BranchTrueFalseTotal
$tBorder ? : 1 0 1
347 1 $titleLine .= "\n";
348 }
349
350 # \firsthead
351  
352 1 ▶ $body .= $HBorder? $l->c('\hline'): '';
Branch: 1 branch
BranchTrueFalseTotal
$HBorder ? : 1 0 1
353 1 $body .= $titleLine; # Leer, wenn kein Titel
354 1 ▶ if ($label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label) 0 1 1
355 0 $body .= $l->c('\label{%s}',$label);
356 }
357 1 $body .= $l->c('\endfirsthead');
358
359 # \endhead
360
361 1 ▶ my $msg = $language eq 'german'? 'Fortsetzung': 'Continuation';
Branch: 1 branch
BranchTrueFalseTotal
$language eq 'german' ? : 1 0 1
362 1 $body .= $l->c('\multicolumn{%s}{r}{\\emph{%s}} \\\\',$width,$msg);
363 1 ▶ $body .= $HBorder? $l->c('\hline'): '';
Branch: 1 branch
BranchTrueFalseTotal
$HBorder ? : 1 0 1
364 1 $body .= $titleLine; # leer, wenn kein Titel
365 1 $body .= $l->c('\endhead');
366  
367 # \endfoot
368  
369 1 ▶ $body .= $HBorder? $l->c('\hline'): '';
Branch: 1 branch
BranchTrueFalseTotal
$HBorder ? : 1 0 1
370 1 ▶ $msg = $language eq 'german'? 'weiter nächste Seite':
Branch: 1 branch
BranchTrueFalseTotal
$language eq 'german' ? : 1 0 1
371 'continued next page';
372 1 $body .= $l->c('\multicolumn{%s}{r}{\\emph{%s}} \\\\',$width,$msg);
373 1 $body .= $l->c('\endfoot');
374  
375 # \endlastfoot
376  
377 1 ▶ if ($caption) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caption) 1 0 1
378 1 my @opt;
379 1 ▶ if ($align ne 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($align ne "c") 0 1 1
380 0 push @opt,'singlelinecheck=off';
381 0 ▶ if ($indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent) 0 0 0
382 0 push @opt,"margin=$indent";
383 }
384 }
385 1 ▶ if (@opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opt) 0 1 1
386 0 $body .= $l->c('\captionsetup{%s}',\@opt);
387 }
388 1 $body .= $l->c('\caption{%s}',$caption);
389 }
390 1 $body .= "\\endlastfoot\n";
391  
392 # Body
393  
394 1 ▶ for (my $i = 0; $i < @$rowA; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$rowA) 4 1 5
395 4 my @arr = $rowCb->($self,$l,$rowA->[$i],$i,@$cbArguments);
396 4 ▶ if ($multiLine) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiLine) 0 4 4
397 0 ▶ for (my $j = 0; $j < @arr; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @arr) 0 0 0
398 0 ▶ if ($arr[$j] =~ s|\n|\\\\|g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr[$j] =~ s/\n/\\\\/g) 0 0 0
399 0 $arr[$j] = $l->ci('\makecell[%st]{%s}',
400 $alignA->[$j],$arr[$j]);
401 }
402 }
403 }
404 4 my $line .= join(' & ',@arr).' \\\\';
405 4 ▶ $line .= $i < @$rowA-1? $hBorder? ' \hline': '':
Branch: 3 branches
BranchTrueFalseTotal
$i < @$rowA - 1 ? : 3 1 4
$hBorder ? : 3 0 3
$HBorder ? : 1 0 1
406 $HBorder? ' \hline': '';
407 4 $line .= "\n";
408 4 $body .= $line;
409 }
410  
411 # Einrückung
412  
413 1 my $code;
414 1 ▶ if ($align ne 'c' && $indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align ne "c" and $indent) 0 1 1
Condition33%1/3
$align ne "c" and $indent
!ll&&!rl&&r
100
MC/DC0%0/2
$align ne "c" and $indent
$align ne "c" $indent
Truth table33%1/3
A and B
A= $align ne "c"B= $indent
ABresult
0X0
100
111
415 0 $code .= $l->c('\setlength{\LTleft}{%s}',$indent);
416 0 $align = undef; # darf nicht gesetzt werden, wenn \LTleft
417 }
418  
419 # Environment
420  
421 1 ▶ my $colSpec = join $vBorder? '|': '',@$alignA;
Branch: 1 branch
BranchTrueFalseTotal
$vBorder ? : 0 1 1
422 1 ▶ if ($VBorder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VBorder) 1 0 1
423 1 $colSpec = "|$colSpec|";
424 }
425  
426 1 $code .= $l->env('longtable',$body,
427 -o => $align,
428 -p => $colSpec,
429 );
430  
431 1 ▶ if (my $postVSpace = $self->postVSpace) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $postVSpace = $self->postVSpace) 0 1 1
432 0 $code .= $l->c('\vspace{%s}','--',$postVSpace);
433 }
434  
435 1 return $code;
436 }
437  
438 # -----------------------------------------------------------------------------
439  
440 =head1 VERSION
441  
442 1.240
443  
444 =head1 AUTHOR
445  
446 Frank Seitz, L<http://fseitz.de/>
447  
448 =head1 COPYRIGHT
449  
450 Copyright (C) 2026 Frank Seitz
451  
452 =head1 LICENSE
453  
454 This code is free software; you can redistribute it and/or modify
455 it under the same terms as Perl itself.
456  
457 =cut
458  
459 # -----------------------------------------------------------------------------
460  
461 1 1;
462  
463 # eof
« Quiq/Schedule.pm ↑ Summary Quiq/Eog.pm »