Aligning tables in the center?

Slidy Presentations all seem to cluster everything tight to the left. How do I align things in the center or increase the margin sizes so that stuff doesn't look so tight to the left? Note, I'm not talking about using the align argument to xtable(). It's not that I want the text of the tables center aligned, I want the table physically in the center of the slide. Can I include html code in an RMarkdown document? Or do I need to modify the CSS?


---
output: slidy_presentation
---

## R table
How do I get this table aligned in the center?

```{r results='asis'}
library(xtable)
tab1<-c(seq(1,10,1))
print(xtable(table(tab1)), type='html')```


## Bullet lists
- And how do I align this in the center
- Or just add some spacing on the left?

## Regression Table
```{r echo=TRUE}
mod<-lm(temperature~pressure, data=pressure)```


## stargazer table
- And how do I align this in the center?

```{r results='asis', echo=F, warning=F, message=F}
library(stargazer)
stargazer(mod, type = 'html')```


1 Like

Ahoy,

First, be sure to be kind to folks here by asking your questions as a reproducible example (looks like you had a newline+backticks issue).

I personally like to use kableExtra for simple table styling. So your line with the table will be:

```{r}
library(kableExtra)
library(dplyr)
tab1<-c(seq(1,10,1))
table(tab1) %>% 
  kable('html') %>% 
  kable_styling(bootstrap_options = "striped", full_width = F)
```

Check out more options here: Create Awesome HTML Table with knitr::kable and kableExtra Hao Zhu 2018-01-15

If you want super granular control over options, check out the custom css options you have with rmarkdown and slidy.

I think I am more interested in in modifying the css options, but I can't say it is overly clear. I don't understand how the arguments relate to the slide header, for example. Do they come just after, on a second line? do they have to refer back to the header's name? I mean in the slidy help documentation, this is the example

`## Next Steps {#nextsteps .emphasized}
Would enable you to apply CSS to all of it’s content using either of the following CSS selectors:

#nextsteps {
color: blue;
}

.emphasized {
font-size: 1.2em;
}
`

So, where would I find the arguments for horizontal alignment? And why is the slide header the same as the css code? ## Next Steps {#nextsteps .emphasized}

In pixiedust, I add the margin:auto CSS tag to a table to center justify it. I can't tell you if that will work in slidy presentations, though.

1 Like

As for the first table, you can easily do something like this :

## R table
How do I get this table aligned in the center?
<div align="center">
```{r echo=FALSE, results='asis'}
library(xtable)
tab1<-c(seq(1,10,1))
print(xtable(table(tab1)), type='html')
``` </div>

and the table is rendered in the center.

As for the bullets you can play with html and css to get them to the center :

## Bullet lists
<div align="center">
<div style="width: 60%; margin:0 left;text-align: left;">
<li>And how do I align this in the center</li>
<li>Or just add some spacing on the left?</li>
</div>
</div>

I hope this helps !

6 Likes