最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Problem with formatting table with LaTex format - Stack Overflow

programmeradmin2浏览0评论

I have a table with squirrel sightings, where I have species, year, total abundance and mean_abundance, like below:

library(tibble)

set.seed(42)  

abundance_sum <- tibble(
  Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
  Year = sample(2015:2023, 100, replace = TRUE),
  Total_abundance = round(runif(100, 1, 500), 1),
  n_observations = sample(1:10, 100, replace = TRUE))

print(abundance_sum)

I love the latex aesthetic for table, and when I knit my markdown file it works, but it is too long, so I can't have my whole table on one page.

abundance_sum %>%
  arrange(Species, Year) %>%
  select(Year, Total_abundance, n_observations) %>%
  kbl(format="latex", booktabs = T) %>%
  kable_styling(latex_options = c("striped", "hold_position")) %>%
  pack_rows(index = table(abundance_sum$Species), label_row = TRUE)
  #%>% 
  #save_kable("kable.pdf")
  
  # this is what I tried to save my table in latex and get it in pdf format

I tried to do a save_kable, but I doesn't work when I precise format = "latex". It gives me this message :

Error: LaTeX failed to compile /Users/klervilecorre/Desktop/Stage\ M2/Data/kable.tex. See  for debugging tips.

So I went to the link, tried to debug tinytex, tried to uninstall Latex, then reinstall it, nothing worked... Maybe it is because I work on Mac ?

With other formats (html, simple...) it works, but I don't like the aesthetics of the table.

Is there another way to save it with an aesthetic like the table in this image ?

I have a table with squirrel sightings, where I have species, year, total abundance and mean_abundance, like below:

library(tibble)

set.seed(42)  

abundance_sum <- tibble(
  Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
  Year = sample(2015:2023, 100, replace = TRUE),
  Total_abundance = round(runif(100, 1, 500), 1),
  n_observations = sample(1:10, 100, replace = TRUE))

print(abundance_sum)

I love the latex aesthetic for table, and when I knit my markdown file it works, but it is too long, so I can't have my whole table on one page.

abundance_sum %>%
  arrange(Species, Year) %>%
  select(Year, Total_abundance, n_observations) %>%
  kbl(format="latex", booktabs = T) %>%
  kable_styling(latex_options = c("striped", "hold_position")) %>%
  pack_rows(index = table(abundance_sum$Species), label_row = TRUE)
  #%>% 
  #save_kable("kable.pdf")
  
  # this is what I tried to save my table in latex and get it in pdf format

I tried to do a save_kable, but I doesn't work when I precise format = "latex". It gives me this message :

Error: LaTeX failed to compile /Users/klervilecorre/Desktop/Stage\ M2/Data/kable.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.

So I went to the link, tried to debug tinytex, tried to uninstall Latex, then reinstall it, nothing worked... Maybe it is because I work on Mac ?

With other formats (html, simple...) it works, but I don't like the aesthetics of the table.

Is there another way to save it with an aesthetic like the table in this image ?

Share Improve this question asked Feb 6 at 9:03 KlerviKlervi 112 bronze badges 3
  • I understand, you want to output your long table as pdf because you like the style. What will this (PDF)-Output be used for? Is it okay to split the long table over multiple pages? Are there any page height restrictions? – dog Commented Feb 6 at 10:18
  • It is for a slideshow, is it possible to simply split the table? Because I haven't found a way to do it without splitting my data first... And I don't really have any height restrictions – Klervi Commented Feb 6 at 14:30
  • Ok, thanks! Yes, you can generate a "splitted" table using longtable in LaTeX in conjunction with a Quarto Document. I will post an answer. – dog Commented Feb 6 at 15:09
Add a comment  | 

2 Answers 2

Reset to default 0

While your code is not reproducible, I did manage to get the LaTeX output using kableExtra and running it in LaTeX. Please do the following to get (closest possible) what you want:

  1. Get the .tex file from the kableExtra in R.
  2. Input the table TeX file in your main document.
  3. Use a package like longtable or xltabular in LaTeX to get your eventual output.
library(tibble)
library(kableExtra)
set.seed(42)  

abundance_sum <- tibble(
  Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
  Year = sample(2015:2023, 100, replace = TRUE),
  Total_abundance = round(runif(100, 1, 500), 1),
  n_observations = sample(1:10, 100, replace = TRUE))


# use `kableExtra` to generate the tex code
tbl_df <-
  abundance_sum %>%
  arrange(Species, Year) %>%
  select(Year, Total_abundance, n_observations) %>%
  kbl(format="latex", booktabs = T) %>%
  kable_styling(latex_options = 
                  c("striped", "hold_position")) %>%
  pack_rows(index = table(abundance_sum$Species),
            label_row = TRUE)

# save the table to tex file stored to a folder called `tabs` within your project
writeLines(tbl_df, "tabs/table_ex.tex")

Normally, for long tables you would use longtable in LaTeX, which devides them into subtables. This would be a fix for your problem, but I am not sure, if you want the table to be devided over many pages. If you do, you can create an R-Quarto Document of type pdf and paste the following code:

---
title: "Untitled"
format: pdf
editor: visual
---

```{r}
#| echo: false
#| warning: false
library(tibble)
library(kableExtra)
library(tidyverse)
abundance_sum <- tibble(
  Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
  Year = sample(2015:2023, 100, replace = TRUE),
  Total_abundance = round(runif(100, 1, 500), 1),
  n_observations = sample(1:10, 100, replace = TRUE)) 

kbl(abundance_sum %>%
      arrange(Species, Year) %>%
      select(Year, Total_abundance, n_observations), 
    format = "latex", 
    booktabs = TRUE,
    longtable = T,
    font_size = 8) %>%  # Reduce font size
  kable_styling(
    latex_options = c("striped","repeat_header"),
    full_width = FALSE) %>%  # This helps with scaling
  pack_rows(index = table(abundance_sum$Species)) 
```

If you just want the table output on one page

we could just make a really long page...

---
title: "Untitled"
format: 
  pdf: 
    geometry:
        - top=15mm
        - paperheight=40 in 
editor: visual
---

```{r}
#| echo: false
#| warning: false
library(tibble)
library(kableExtra)
library(tidyverse)
abundance_sum <- tibble(
  Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
  Year = sample(2015:2023, 100, replace = TRUE),
  Total_abundance = round(runif(100, 1, 500), 1),
  n_observations = sample(1:10, 100, replace = TRUE)) 

kbl(abundance_sum %>%
      arrange(Species, Year) %>%
      select(Year, Total_abundance, n_observations), 
    format = "latex", 
    booktabs = TRUE,
    font_size = 8) %>%  # Reduce font size
  kable_styling(
    latex_options = c("striped"),
    full_width = FALSE) %>%  # This helps with scaling
  pack_rows(index = table(abundance_sum$Species)) 
```
发布评论

评论列表(0)

  1. 暂无评论