我在 Rmarkdown 文档中使用 pander
来显示表格。有没有办法让 table 居中?
我尝试了几种不同的方法,但似乎都不起作用。例如:
{r, fig.align="center"}
library(pander)
test <- as.data.frame(matrix(ncol = 5, nrow =5))
test[1] <- 1
pander(test, justify = "center")
添加 fig.align = "center"
不起作用,justify = "center"
也不起作用
有人知道解决方法吗?
最佳答案
您只需添加常规 HTML 标记即可将表格居中(例如 <center>
):
---
title: "test"
output:
html_document: default
pdf_document: default
---
<center>
```{r, fig.align="center"}
library(pander)
test <- as.data.frame(matrix(ncol = 5, nrow =5))
test[1] <- 1
pander(test, justify = "center")
```
</center>
如果您想同时显示代码和居中表格,但不希望代码居中,请重复该 block ,但第一次不要对其求值,第二次也不要回显它。
这是一个例子:
或者,添加带有样式选项的自定义 CSS 文件,并将其添加到 header 中。
示例 CSS(另存为“test.css”):
table {
margin:1em auto;
}
标题示例:
---
title: "test"
output:
html_document:
css: test.css
---
关于r - 居中迎合 table ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42954732/