2

I am including gt tables in a Rmarkdown Tufte-style HTML. I want to align the gt tables in the center of the main column, rather than in the center of the HTML spanning both the main column and margin. I have tried using the fig.align knitr option as well as tab.align command. Here is a repo with an rmd that shows the problem: https://github.com/cassidybargell/gt-tufte. The gt table spans the main column as well as the margin.

Example code of a gt table being used in the Tufte-HTML:

tibble(subject = "Joe",
       ytreat = "13",
       ycontrol = "9",
       ydiff = "+4") %>%  
  gt()
  • Make your own css file is one way to fix it. Here are the lines of code: .gt_table { margin-left: 0 !important; margin-right: 0 !important; width: 55% !important; } Thank you to Yaodong Yu for the help!! – Cassidy Bargell Jun 28 '20 at 22:39

2 Answers2

1

Make your own css file is one way to fix it. Here are the lines of code:

.gt_table {
  margin-left: 0 !important;
  margin-right: 0 !important;
  width: 55% !important;
}

Thank you to Yaodong Yu for the help!!

1

An alternative is to define a hook function:

knitr::knit_hooks$set(gtbl = function(before, options){
if (before) {
paste( '<style> .gt_table {width: 100% !important;} </style>',
       '<div class="figure"><p class="caption marginnote shownote">', 
       options$fig.cap, 
       '</p>', 
       sep="")
} else { "</div>" }
})

inside a chunk. Then the chunk option gtbl can be used when a gt table is required as for example:

```{r gtbl=TRUE, fig.cap="*Table 1* Energy Trends"}
tblTrends()
```

where fig.cap supplies the caption right-aligned.

Hook functions are documented in the rmarkdown cookbook

peter2108
  • 5,580
  • 6
  • 24
  • 18