Search code examples
rknitrr-markdownxtable

Rotated xtable colnames with Tufte_handout in RMarkdown


I need to rotate the column names in a Tufte_handout in RMarkdown, and I think "\usepackage{rotating}" is missing. But no matter how I try, I can't get it in without errors.

Here is a working example (reduced version of template in RStudio). The two first tables are plotted (without rotated colnames), but when "#" is removed in the last two tables with rotated colnames, it fails.

---
title: "Tufte Handout with rotated tables"
output: rmarkdown::tufte_handout
---

# Table from RStudio template

```{r, results='asis'}
library(xtable)
options(xtable.comment = FALSE)
options(xtable.booktabs = TRUE)
xtable(head(mtcars[,1:6]), caption = "Ok. Template from RStudio.")
```

# Modified table (working)

```{r, results='asis'}
library(xtable)
options(xtable.comment = FALSE)
options(xtable.booktabs = TRUE)
the.table <- xtable(head(mtcars[,1:6]), caption = "Also ok.")
print((the.table), rotate.colnames=FALSE)
```

# Modified table (not working)

```{r, results='asis'}
library(xtable)
options(xtable.comment = FALSE)
options(xtable.booktabs = TRUE)
options(xtable.rotate.colnames = TRUE)
# Next row will fail
#xtable(head(mtcars[,1:6]), caption = "Not ok.")
```

# Modified table (not working)

```{r, results='asis'}
library(xtable)
options(xtable.comment = FALSE)
options(xtable.booktabs = TRUE)
the.table <- xtable(head(mtcars[,1:6]), caption = "Not ok.")
# Next row will fail.
#print((the.table), rotate.colnames=TRUE)
```

Update: Thanks to @Jonathan's patient support, I finally got it working. These are the steps:

  1. Created new project in RStudio: enhancedtufte (as a package)

  2. Created diretory inst under enhancedtufte

  3. Created directory rmarkdown under inst

  4. Created directory templates under rmarkdown

  5. Ran command "system.file("rmarkdown/templates/tufte_handout", package="rmarkdown")" to find where original tufte_handout is, and copied directory "tufte_handout" (from under templates) to the templates-directory created in previous step

  6. Edited line in template.yaml from "name: Tufte Handout" to "name: Tufte Handout 2"

  7. Clicked "Build & Reload" in RStudio

  8. Selected File -> New File -> R Markdown..., and then From Template -> Tufte Handout 2 {enhancedtufte}

  9. In new document, changed line "output: rmarkdown::tufte_handout" to "output:enhanced_tufte::tufte_handout"

  10. Copied all files from https://github.com/rstudio/rmarkdown/tree/master/R to enhancedtufte/R

  11. Edited package name to "enhancedtufte in tufte_handout.R "# get the tufte handlout template template <- system.file( "rmarkdown/templates/tufte_handout/resources/tufte-handout.tex", package = "enhancedtufte" )"

  12. Added "\usepackage{rotating}" in tufte-handout.tex

  13. Build & Reload

  14. Knit works with sideways table (remove # in example above)

Solution

  • The \usepackage directive needs to go into the TeX template that Pandoc uses.

    Unfortunately it's not extremely easy to change the template, but it's not impossible. Here's what you need to do:

    1. Create a new package in RStudio (say, enhanced_tufte).
    2. Copy rmarkdown's Tufte handout template to your package. You can find the location of the Tufte handout template with this command: system.file("rmarkdown/templates/tufte_handout", package="rmarkdown"); you'll want to copy this folder to your package's inst/rmarkdown folder.
    3. Modify your package's copy of tufte-handout.tex to include \usepackage{rotating}; build and load your package.
    4. Modify your YAML header to use your new template: output: enhanced_tufte::tufte_handout.

    sideways headers

    You can also just modify RMarkdown's template directly to include \usepackage{rotating}, but of course that'll get reverted the next time you install the rmarkdown package.