LoginSignup
1
1

More than 5 years have passed since last update.

knitr to PDFでの複雑なテーブル(書体を変更する)

Last updated at Posted at 2018-01-09

Rstudio Cloudのリンクはこちらです。

rmakrdownとkableを使ってテーブルをかいてたところ、複雑なテーブルを書く際にkableExtraというパッケージがとても有用だということがわかったのでメモ。

書体を変更できるようにした。

library(knitr)
library(kableExtra)

#Motor Trend Car Road Tests in datasets package is loaded.
dt <- mtcars[1:5, 1:4]

# LaTeX Table
kable(dt, format = "latex", booktabs = T, caption = "Demo Table") %>%
  kable_styling(latex_options = c("striped", "hold_position"),
                full_width = F) %>%
  add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
  add_footnote(c("table footnote"))

Untitled.jpeg

タイトルとフッターの書体を変更


 # LaTeX Table
kable(dt, format = "latex", booktabs = T, caption = "\\em{Demo} \\textnormal{Table}", escape =FALSE) %>%
  kable_styling(latex_options = c("striped", "hold_position"),
                full_width = F) %>%
  add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
  add_footnote(c("\\textsc{table} \\textbf{footnote}"), escape=FALSE)

Untitled.jpeg

行・列名の書体を変更

# LaTeX Table
colnames(dt)[1] <- "\\textit{mpg}"
colnames(dt)[2] <- "\\textbf{cyl}"
colnames(dt)[3] <- "\\texttt{disp}"
colnames(dt)[4] <- "\\textrm{hp}"

rownames(dt)[1] <- "\\textit{Mazda RX4}"
rownames(dt)[2] <- "\\textbf{Mazda RX4 Wag}"
rownames(dt)[3] <- "\\texttt{Datsun 710}"
rownames(dt)[4] <- "\\textrm{Hornet 4 Drive}"
rownames(dt)[5] <- "\\textsc{Hornet Sportabout}"


kable(dt, format = "latex", booktabs = T, caption = "\\em{Demo} \\textnormal{Table}", escape =FALSE) %>%
  kable_styling(latex_options = c("striped", "hold_position"),
                full_width = F) %>%
  add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
  add_footnote(c("\\textsc{table} \\textbf{footnote}"), escape=FALSE)

Untitled.jpeg

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1