Skip to content

Commit

Permalink
Merge pull request #753 from ben-schwen/master
Browse files Browse the repository at this point in the history
add feature to scale_up and scale_down latex tables
  • Loading branch information
haozhu233 committed Dec 11, 2023
2 parents d798bf1 + 2a2a64e commit 2a50bdb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
22 changes: 16 additions & 6 deletions R/kable_styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' `bordered`, `hover`, `condensed`, `responsive` and `none`.
#' @param latex_options A character vector for LaTeX table options. Please see
#' package vignette for more information. Possible options include
#' `basic`, `striped`, `hold_position`, `HOLD_position`, `scale_down` & `repeat_header`.
#' `basic`, `striped`, `hold_position`, `HOLD_position`, `scale_down`, `scale_up` & `repeat_header`.
#' `striped` will add alternative row colors to the table. It will imports
#' `LaTeX` package `xcolor` if enabled. `hold_position` will "hold" the floating
#' table to the exact position. It is useful when the `LaTeX` table is contained
Expand Down Expand Up @@ -337,7 +337,7 @@ pdfTable_styling <- function(kable_input,

latex_options <- match.arg(
latex_options,
c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "repeat_header"),
c("basic", "striped", "hold_position", "HOLD_position", "scale_down", "scale_up", "repeat_header"),
several.ok = T
)

Expand All @@ -362,7 +362,11 @@ pdfTable_styling <- function(kable_input,
}

if ("scale_down" %in% latex_options) {
out <- styling_latex_scale_down(out, table_info)
out <- styling_latex_scale(out, table_info, "down")
}

if ("scale_up" %in% latex_options) {
out <- styling_latex_scale(out, table_info, "up")
}

if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Expand Down Expand Up @@ -442,16 +446,22 @@ styling_latex_HOLD_position <- function(x) {
}
}

styling_latex_scale_down <- function(x, table_info) {
styling_latex_scale <- function(x, table_info, dir=c("down", "up")) {
# You cannot put longtable in a resizebox
# http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
if (table_info$tabular == "longtable") {
warning("Longtable cannot be resized.")
return(x)
}
if (dir=="down") {
d <- ">"
} else {
d <- "<"
}

x <- sub(table_info$begin_tabular,
paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{\n",
table_info$begin_tabular),
paste0("\\\\resizebox{\\\\ifdim\\\\width\\", d,"\\\\linewidth\\\\linewidth\\\\else\\\\width\\\\fi\\}\\{\\!\\}\\{\n",
table_info$begin_tabular),
x)
sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
}
Expand Down
2 changes: 1 addition & 1 deletion man/kable_styling.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion vignettes/awesome_table_in_pdf.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ kbl(dt, caption = "Demo table", booktabs = T) %>%
If you find `hold_position` is not powerful enough to literally PIN your table in the exact position, you may want to use `HOLD_position`, which is a more powerful version of this feature. For those who are familiar with LaTeX, `hold_position` uses `[!h]` and `HOLD_position` uses `[H]` and the `float` package.

### Scale down
When you have a wide table that will normally go out of the page, and you want to scale down the table to fit the page, you can use the `scale_down` option here. Note that, if your table is too small, it will also scale up your table. It was named in this way only because scaling up isn't very useful in most cases. You should also note that `scale_down` does not work with `longtable`. If you `longtable` is too wide, you should manually adjust your font size or switch to landscape layout.
When you have a wide table that will normally go out of the page, and you want to scale down the table to fit the page, you can use the `scale_down` option here. Similarly if you want scale up a table to use the full page width you can use the `scale_up` option. Having both options available ensures that your table is only scaled in the direction you intended to scale it. You should also note that `scale_down` does not work with `longtable`. If your `longtable` is too wide, you should manually adjust your fontsize or switch to landscape layout.
```{r}
kbl(cbind(dt, dt, dt), booktabs = T) %>%
kable_styling(latex_options = c("striped", "scale_down"))
Expand All @@ -161,6 +161,10 @@ kbl(cbind(dt, dt, dt), booktabs = T) %>%
kbl(cbind(dt), booktabs = T) %>%
kable_styling(latex_options = c("striped", "scale_down"))
```
```{r}
kbl(cbind(dt), booktabs = T) %>%
kable_styling(latex_options = c("striped", "scale_up"))
```

### Repeat header in longtable
In `kableExtra` 0.3.0 or above, a new option `repeat_header` was introduced into `kable_styling`. It will add header rows to longtables spanning multiple pages. For table captions on following pages, it will append *"continued"* to the caption to differentiate. If you need texts other than *"(continued)"* (for example, other languages), you can specify it using `kable_styling(..., repeat_header_text = "xxx")`. If you want to completely replace the table caption instead of appending, you can specify it in the option `repeat_header_method`.
Expand Down

0 comments on commit 2a50bdb

Please sign in to comment.