2

I am using pander in my Rmarkdown document to display tables. Is there away to center the table?

I have tried a few different methods, but none of them seem to work. For example:

{r, fig.align="center"}
library(pander)
test <- as.data.frame(matrix(ncol = 5, nrow =5))
test[1] <- 1
pander(test, justify = "center")

Adding fig.align = "center" does not work and neither does justify = "center"

Does anyone know of a workaround ?

enter image description here

iskandarblue
  • 7,208
  • 15
  • 60
  • 130

1 Answers1

4

You could just add regular HTML tags to center the table (like <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>

If you want to show both the code and the centered table, but don't want the code centered, repeat the block, but don't evaluate it the first time, and then don't echo it the second time.

Here's an example:

enter image description here


Alternatively, add a custom CSS file with your styling options and add that to your header.

Example CSS (saved as "test.css"):

table {
   margin:1em auto;
}

Example header:

---
title: "test"
output:
  html_document: 
    css: test.css
---
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485