Reputation: 39
I have tried to look into other posts but did not find anything that works
Basically when I print as pdf the html file obtained with Rmarkdown the tables end up being really huge and after trying several codes I am not really able to resize them, example below
also, when using export_summs
the spacing between the lines is ridiculously wide, and I cant resize that either
Is there a universal way to resize tables?
something like {r out.width="50%", out.height="50%"}
?
Thanks, Andriy
Upvotes: 0
Views: 2431
Reputation: 403
It would be helpful if you could provide a small, reproducible example. I've tried to recreate your conditions the best I could here:
---
title: "PDF Table | Example"
author: "Alexander P. Christensen"
date: "2/4/2022"
output:
pdf_document:
extra_dependencies: ["adjustbox"]
---
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
## Load packages
{r load packages, echo = TRUE, eval = TRUE, comment = NA, message = FALSE, warning = FALSE}
library(jtools)
library(knitr)
library(kableExtra)
## Example Table
{r example, echo = TRUE, eval = TRUE, comment = NA, message = FALSE, warning = FALSE}
kable(
head(iris), booktabs = TRUE, linesep = ""
)
## Example Smaller Table (shrinks vertically)
\renewcommand{\arraystretch}{.5}
{r example_smaller, echo = TRUE, eval = TRUE, comment = NA, message = FALSE, warning = FALSE}
kable(
head(iris), booktabs = TRUE, linesep = ""
) %>%
kable_styling(latex_options = "hold_position")
\newpage
## Example Even Smaller Table (shrinks vertically more)
\renewcommand{\arraystretch}{.25}
{r example_even_smaller, echo = TRUE, eval = TRUE, comment = NA, message = FALSE, warning = FALSE}
kable(
head(iris), booktabs = TRUE, linesep = ""
) %>%
kable_styling(latex_options = "hold_position")
## Obtain LaTeX output for adjustbox (next example)
{r example_latex, echo = TRUE, eval = FALSE, comment = NA, message = FALSE, warning = FALSE}
kable(
head(iris), booktabs = TRUE, linesep = "", format = "latex"
) %>%
kable_styling(latex_options = "hold_position")
This code will output:
\begin{table}[!h]
\begin{tabular}{rrrrl}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
\midrule
5.1 & 3.5 & 1.4 & 0.2 & setosa\\
4.9 & 3.0 & 1.4 & 0.2 & setosa\\
4.7 & 3.2 & 1.3 & 0.2 & setosa\\
4.6 & 3.1 & 1.5 & 0.2 & setosa\\
5.0 & 3.6 & 1.4 & 0.2 & setosa\\
5.4 & 3.9 & 1.7 & 0.4 & setosa\\
\bottomrule
\end{tabular}
\end{table}
## Adjust Full Table
\renewcommand{\arraystretch}{1}
\begin{table}[!h]
\begin{adjustbox}{width = 3in, totalheight = 3in, center}
\begin{tabular}{rrrrl}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
\midrule
5.1 & 3.5 & 1.4 & 0.2 & setosa\\
4.9 & 3.0 & 1.4 & 0.2 & setosa\\
4.7 & 3.2 & 1.3 & 0.2 & setosa\\
4.6 & 3.1 & 1.5 & 0.2 & setosa\\
5.0 & 3.6 & 1.4 & 0.2 & setosa\\
5.4 & 3.9 & 1.7 & 0.4 & setosa\\
\bottomrule
\end{tabular}
\end{adjustbox}
\end{table}
\renewcommand{\arraystretch}{.5}
will adjust your table vertically and can be placed before any kable()
code. I assume this works the same for jtools::export_summs
.
\begin{adjustbox}{width = 3in, totalheight = 3in, center}
will adjust your table to your desired dimensions automatically.
Using adjustbox
requires a few more steps. First, you'll need to setup your YAML with extra dependencies:
output:
pdf_document:
extra_dependencies: ["adjustbox"]
Second, you'll need to output a LaTeX table from kable()
with the format = "latex"
option:
kable(
head(iris), booktabs = TRUE, linesep = "", format = "latex"
) %>%
kable_styling(latex_options = "hold_position")
Finally, you'll need to insert your table chunk to replace the ...
in the code below:
\begin{table}[!h]
\begin{adjustbox}{width = 3in, totalheight = 3in, center}
\begin{tabular}{rrrrl}
\toprule
...
\end{tabular}
\end{adjustbox}
\end{table}
Change the width
and totalheight
to your desired size
Upvotes: 2