简体   繁体   English

使用 kable 或 kableExtra 左对齐表格标题

[英]left- align table caption with kable or kableExtra

Is it possible to left-align the table caption?是否可以左对齐表格标题? I am annoyed by the APA6th rules but I have to left-align the table caption somehow.我对 APA6th 规则感到恼火,但我必须以某种方式左对齐表格标题。

Take for example this table:以这张表为例:

library(knitr)
library(kableExtra) 

kable(mtcars[1:10, 1:6], format = "latex", caption = "I need this left-aligned.", booktabs = T) %>% 
      kable_styling(position = "left") %>%
      group_rows("Group 1", 4, 7) %>%
      group_rows("Group 2", 8, 10)

The caption will always be centered above the table, even if I left-align the table position with kable_styling(position = "left") .标题将始终位于表格上方的中心,即使我将表格 position 与kable_styling(position = "left")左对齐。

EDIT: See here for a temporary solution that worked at least for me.编辑:请参阅此处了解至少对我有用的临时解决方案。

You only need to add the Latex package caption in the YAML header section with singlelinecheck=false specified to have the caption of your title left-aligned. You only need to add the Latex package caption in the YAML header section with singlelinecheck singlelinecheck=false specified to have the caption of your title left-aligned. Here is a reproducible example:这是一个可重现的示例:

---
output:
  pdf_document: default
header-includes:
   - \usepackage[singlelinecheck=false]{caption}
---
```{r, echo = FALSE, message = FALSE}
library(knitr)
library(dplyr)
library(kableExtra)
```

```{r, echo = FALSE, message = FALSE}
kable(mtcars[1:10, 1:6], format = "latex", caption = "I need this left-aligned.", booktabs = T) %>% 
  kable_styling(position = "left") %>%
  group_rows("Group 1", 4, 7) %>%
  group_rows("Group 2", 8, 10)
```

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM