r - 居中迎合 table

标签 r pandoc pander

我在 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" 也不起作用

有人知道解决方法吗?

enter image description here

最佳答案

您只需添加常规 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 ,但第一次不要对其求值,第二次也不要回显它。

这是一个例子:

enter image description here


或者,添加带有样式选项的自定义 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/

相关文章:

r - 总和的意外值

knitr - 如何在 Rmarkdown 中制作图形标题?

pdf - BibTeX 引用中的 LaTeX 数学不适用于 pandoc

r - 在 Rstudio 的 RMarkdown 中循环生成 Pander 表

r - 如何查找 data.frame 中 NA 的百分比?

r - 查找行式最小值和返回值及其对应的列名

r - 省略号故障 : Passing . .. 至 lm

r - bookdown:自定义输出文件名

r - 在knitr或pander输出中保留表的名字吗?

r - 调整pandoc.table列宽