Skip to content

Commit

Permalink
padding option for niceTables
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Jordan committed Jun 13, 2023
1 parent c171267 commit cb7e59b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
29 changes: 24 additions & 5 deletions macros/ui/niceTables.pl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ =head3 All output formats
Can be C<'top'>, C<'middle'>, or C<'bottom'>. Applies to all rows.
See below to override for an individual row.
=item C<padding =E<gt> [ , ]>
An array of two non-negative numbers used to define cell-padding. The first is
for top-down padding, the second for left-right padding. In HTML, each padding
is the value multiplied by 0.85rem. In LaTeX, the left-right padding is the
value multiplied by 10pt, and the top-down padding is implemented by setting
C<\arraystretch> to the value. (0.85rem and 10pt are default font sizes at the
time of this feature's introduction.) The default for a DataTable is C<[0,0.5]>
and the default for a LayoutTable is C<[1,1]>. C<padding> may also be entered
as a single nonnegative number to describe both top-down and left-right
padding at the same time.
=back
=head3 HTML output
Expand Down Expand Up @@ -509,8 +521,8 @@ sub TableEnvironment {
my $tabularwidth = $hasX ? "$tableOpts->{Xratio}\\linewidth" : '';
$rows = latexEnvironment($rows, $tabulartype, [ $tabularwidth, '[t]', $tableOpts->{texalignment} ], ' ');
$rows = prefix($rows, '\centering%') if $tableOpts->{center};
$rows = prefix($rows, '\renewcommand{\arraystretch}{2}', '')
if $tableOpts->{LaYoUt};
$rows = prefix($rows, '\renewcommand{\arraystretch}{' . ($tableOpts->{padding}[0] + 1) . '}', '');
$rows = prefix($rows, '\setlength{\tabcolsep}{' . ($tableOpts->{padding}[1] * 10) . 'pt}', '');
$rows = suffix(
$rows,
"\\captionsetup{textfont={sc},belowskip=12pt,aboveskip=4pt}\\captionof*{table}{$tableOpts->{caption}}",
Expand Down Expand Up @@ -994,7 +1006,8 @@ sub Row {
$scope = 'row' if ($cellOpts->{header} =~ /^(rh|row)$/i);
$scope = 'col' if ($cellOpts->{header} =~ /^(ch|col|column)$/i);
do { $t = 'td'; $scope = ''; } if ($cellOpts->{header} =~ /^td$/i);
my $css = '';
my $css =
css('padding', $tableOpts->{padding}[0] * 0.85 . 'rem ' . $tableOpts->{padding}[1] * 0.85 . 'rem');

# col level
$css .= css('text-align', 'left')
Expand Down Expand Up @@ -1065,15 +1078,13 @@ sub Row {
my $cellvalign = $tableOpts->{valign};
$cellvalign = $valign if ($valign);
$css = css('vertical-align', $cellvalign) . $css;
$css = css('padding', '12pt') . $css;
if ($cellAlign->{tex} =~ /\\columncolor(\[HTML\])?\{(.*?)[\}!]/) {
$css = css('background-color', ($1 ? '#' : '') . $2) . $css;
}
$css =
css('border-right', getRuleCSS($cellAlign->{right})) . $css;
$cell = tag($cell, 'div', { style => $css });
} else {
$css = css('padding', '0pt 6pt') . $css;
$cell = tag(
$cell, $t,
{
Expand Down Expand Up @@ -1235,7 +1246,9 @@ sub TableOptions {
booktabs => 1,
headerrules => 1,
LaYoUt => 0,
padding => [ 0, 0.5 ],
);

%outHash = %supportedOptions;
my %userOptions = @_;
for my $key (keys(%supportedOptions)) {
Expand All @@ -1250,6 +1263,12 @@ sub TableOptions {
$outHash{horizontalrules} = $userOptions{midrules}
if (defined($userOptions{midrules}) && !$outHash{horizontalrules});

# expand scalar padding to array
$outHash{padding} = [ ($outHash{padding}) x 2 ] if (ref $outHash{padding} ne 'ARRAY');

# change padding for layout table
$outHash{padding} = [ 1, 1 ] if $outHash{LaYoUt} && !$userOptions{padding};

return \%outHash;
}

Expand Down
2 changes: 1 addition & 1 deletion t/output_macros/niceTables.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ loadMacros('niceTables.pl', 'PGstandard.pl');
use Data::Dumper;

my $tab = DataTable([ [ 1, 2, 3 ], [ 4, 5, 6 ] ]);
my $std_pad = 'padding:0pt 6pt;';
my $std_pad = 'padding:0rem 0.425rem;';
my $talign_center = 'text-align:center;';

is $tab, qq{<table style="margin:auto;">
Expand Down

0 comments on commit cb7e59b

Please sign in to comment.