Skip to content

Commit 73554e8

Browse files
committed
fix: padding.left and padding.right are now supported in PDF/LaTeX
1 parent f66effe commit 73554e8

File tree

8 files changed

+56
-9
lines changed

8 files changed

+56
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: flextable
33
Title: Functions for Tabular Reporting
4-
Version: 0.9.11
4+
Version: 0.9.12.001
55
Authors@R: c(
66
person("David", "Gohel", , "david.gohel@ardata.fr", role = c("aut", "cre")),
77
person("ArData", role = "cph"),

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# flextable 0.9.12
2+
3+
## issues
4+
5+
- `padding.left` and `padding.right` are now supported in PDF/LaTeX output.
6+
7+
18
# flextable 0.9.11
29

310
## new features

R/latex_str.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,22 @@ gen_raw_latex <- function(x, lat_container = latex_container_none(),
128128
# latex for multirow ----
129129
augment_multirow_fixed(cell_properties_df)
130130

131+
# paragraph padding ----
132+
cell_properties_df[, c("padding_left_str", "padding_right_str") := list(
133+
ifelse(.SD$padding.left > 0,
134+
sprintf("\\advance\\leftskip by %.1fpt\\relax ", .SD$padding.left),
135+
""),
136+
ifelse(.SD$padding.right > 0,
137+
sprintf("\\advance\\rightskip by %.1fpt\\relax ", .SD$padding.right),
138+
"")
139+
)]
140+
131141
# paste everything ----
132142
cell_properties_df[, c("txt") := list(
133143
paste0(
134144
.SD$multirow_left,
145+
.SD$padding_left_str,
146+
.SD$padding_right_str,
135147
.SD$text_direction_left,
136148
.SD$txt,
137149
.SD$text_direction_right,

R/printers.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,8 @@ print.flextable <- function(x, preview = "html", align = "center", ...) {
581581
#' @section PDF limitations:
582582
#'
583583
#' The following properties are not supported in PDF output:
584-
#' padding, `line_spacing` and row `height`. Justified text is
585-
#' converted to left-aligned.
584+
#' `padding.top`, `padding.bottom`, `line_spacing` and row `height`.
585+
#' Justified text is converted to left-aligned.
586586
#'
587587
#' To use system fonts, set `latex_engine: xelatex` in the YAML
588588
#' header (the default `pdflatex` engine does not support them).

R/styles.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,10 @@ font <- function(
578578
#' @title Set paragraph paddings
579579
#' @description Change the padding of selected rows and columns of a flextable.
580580
#' @note
581-
#' Padding is not implemented in PDF due to technical infeasibility but
582-
#' it can be replaced with `set_table_properties(opts_pdf = list(tabcolsep = 1))`.
581+
#' In PDF output, only `padding.left` and `padding.right` are supported.
582+
#' `padding.top` and `padding.bottom` are ignored due to LaTeX limitations.
583+
#' Global horizontal spacing can also be set with
584+
#' `set_table_properties(opts_pdf = list(tabcolsep = 1))`.
583585
#' @inheritParams args_selectors_with_all
584586
#' @param padding padding (shortcut for top, bottom, left and right), unit is pts (points).
585587
#' @param padding.top padding top, unit is pts (points).

man/knit_print.flextable.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/padding.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-latex.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,27 @@ test_that("font warning fires for Quarto + pdflatex", {
339339

340340
knitr::opts_knit$set("quarto.version" = NULL, "out.format" = NULL)
341341
})
342+
343+
test_that("padding.left and padding.right produce advance leftskip/rightskip in latex", {
344+
ft <- flextable(data.frame(a = c("hello", "world"), b = 1:2))
345+
ft <- padding(ft, padding.left = 0, padding.right = 0, part = "all")
346+
ft <- padding(ft, i = 1, j = 1, padding.left = 20)
347+
ft <- padding(ft, i = 2, j = 1, padding.right = 15)
348+
latex_str <- flextable:::gen_raw_latex(ft)
349+
350+
# row 1 col 1: \advance\leftskip by 20.0pt\relax
351+
expect_match(latex_str, "\\advance\\leftskip by 20.0pt\\relax", fixed = TRUE)
352+
353+
# row 2 col 1: \advance\rightskip by 15.0pt\relax
354+
expect_match(latex_str, "\\advance\\rightskip by 15.0pt\\relax", fixed = TRUE)
355+
356+
# only 1 leftskip and 1 rightskip (other cells have padding = 0)
357+
expect_equal(
358+
length(gregexpr("advance\\\\leftskip", latex_str, perl = TRUE)[[1]]),
359+
1L
360+
)
361+
expect_equal(
362+
length(gregexpr("advance\\\\rightskip", latex_str, perl = TRUE)[[1]]),
363+
1L
364+
)
365+
})

0 commit comments

Comments
 (0)