[英]Aligning text in kableExtra table
我正在用 R markdown 制作 Latex pdf,但是在制作包含帶有 kableExtra 包的文本的表格時遇到問題。
當我在 Kable() 中使用 'align =' 參數時,它只對齊標題而不是全文。 返回的表格如下所示,主體的第一行左對齊,下一行居中對齊,最后一行右對齊!!
請幫我把它全部中心對齊!
代碼:
library(knitr)
library(kableExtra)
df <- data.frame(c("Title","Title", "Title"),
linebreak(c("The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text")),
linebreak(c("The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text")),
linebreak(c("The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text")))
kable(df, align="c", col.names = c("","Title","Title", "Title"), escape = FALSE) %>%
kable_styling(latex_options="HOLD_position")
編輯:
這是用yaml復制的代碼,
---
title:
author:
date:
abstract:
output:
pdf_document:
toc: yes
toc_depth: 2
editor_options:
markdown:
wrap: 72
---
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(kableExtra)
df <- data.frame(c("Title","Title", "Title"),
linebreak(c("The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text")),
linebreak(c("The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text")),
linebreak(c("The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text")))
kable(df, align="c", col.names = c("","Title","Title", "Title"), escape = FALSE) %>%
kable_styling(latex_options="HOLD_position")
我認為問題出在您的df
規范上。 函數linebreak
在創建df
時設置了一個align
參數。 創建df
時需要指定對齊方式
df <- data.frame(c("Title","Title", "Title"),
linebreak(c("The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text"), align = 'c'),
linebreak(c("The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text"), align = 'c'),
linebreak(c("The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text", "The Main Text\n Main Text\n Text"), align = 'c'))
此外,根據kableExtra
的文檔,建議在使用 LaTex 表時添加booktabs = T
和escape = F
例如:
df %>% kbl(booktabs = T, escape = F,
col.names = c("","Title","Title", "Title"), align = 'c')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.