Search code examples
rbookdown

Thesisdown: knitting to PDF fails when loading huxtable package and creating tables


I want to create tables with the huxtable package in documents created with the thesisdow package. Knitting to html works well with HTML. But knitting but to PDF fails with the following error as soon as a table is included after loading huxtable:

 ! LaTeX Error: Environment threeparttable undefined.

There seems to be a clash with LaTex packages in the background. Below a MWE for the thesisdown index.file. Interestingly, in a simple markdown file knitting both to html and PDF works without problems. Since thesisdown is built on bookdown it is possible that the error also occurs when knitting bookdown files to PDF.

index.Rmd


    ---
    knit: "bookdown::render_book"
    site: bookdown::bookdown_site
    output: 
     thesisdown::thesis_pdf: default
      # thesisdown::thesis_gitbook: default
    ---

    ```{r include_packages, include = FALSE}
    # Ensure that the thesisdown package is installed and loaded
    if(!require(devtools))
      install.packages("devtools", repos = "http://cran.rstudio.com")
    if(!require(thesisdown))
      devtools::install_github("ismayc/thesisdown")
    library(thesisdown)
    ```

    # Random Test Output - *before* calling "library(huxtable)"
    a table with pressure data
    ```{r pressure-table, echo=FALSE}
    pressure
    ```

    ```{r echo=FALSE}
    library(huxtable)
    ```

    # Random Test Output - *after* calling "library(huxtable)"
    a table with pressure data
    ```{r pressure-table2, echo=FALSE}
    pressure
    ```

MWE summary:

  • knitting to HTML works always
  • knitting to PDF works only if there are no tables after "library(huxtable)", i.e. when the code chunk "pressure-table2" is not executed

UPDATE:

(1) If working with thesisdown, including the LaTex packages required by huxtable in the YAML-header as proposed by @dash2 works well.

(2) However, if working with huskydown, the issue unfortunately remains.

1. index.Rmd (thesisdown)


    ---
    knit: "bookdown::render_book"
    site: bookdown::bookdown_site
    output: 
     thesisdown::thesis_pdf: default
      # thesisdown::thesis_gitbook: default
    header-includes:
      - \usepackage{array}
      - \usepackage{caption}
      - \usepackage{graphicx}
      - \usepackage{siunitx}
      - \usepackage{colortbl}
      - \usepackage{multirow}
      - \usepackage{hhline}
      - \usepackage{calc}
      - \usepackage{tabularx}
      - \usepackage{threeparttable}
      - \usepackage{wrapfig}
    ---
    ...

2. index.Rmd (huskydown)


    ---
    # UW thesis fields
    title: "My thesis title - edit in index.Rmd"
    author: "My Name"
    year: "2017"
    program: "My Department"
    chair: "Name of my committee chair"
    chairtitle: "Title of my chair"
    signature1: "person 1"
    signature2: "person 2"
    signature3: "person 3"
    abstract: |
      "Here is my abstract"
    acknowledgments: |
      "My acknowledgments"
    dedication: |
      "My dedication"
    knit: "bookdown::render_book"
    site: bookdown::bookdown_site
    output: 
      huskydown::thesis_pdf: 
        latex_engine: xelatex
    bibliography: bib/thesis.bib
    csl: csl/apa.csl
    lot: true
    lof: true
    header-includes:
      - \usepackage{tikz}
      - \usepackage{array}
      - \usepackage{caption}
      - \usepackage{graphicx}
      - \usepackage{siunitx}
      - \usepackage{colortbl}
      - \usepackage{multirow}
      - \usepackage{hhline}
      - \usepackage{calc}
      - \usepackage{tabularx}
      - \usepackage{threeparttable}
      - \usepackage{wrapfig}
    ---


    ```{r include_packages, include = FALSE}
    # This chunk ensures that the huskydown package is
    # installed and loaded. This huskydown package includes
    # the template files for the thesis.
    if(!require(devtools))
      install.packages("devtools", repos = "http://cran.rstudio.com")
    if(!require(huskydown))
      devtools::install_github("benmarwick/huskydown")
    library(huskydown)
    ```

    # Random Test Output - *before* calling "library(huxtable)"
    a table with pressure data
    ```{r pressure-table, echo=FALSE}
    pressure
    ```

    ```{r echo=FALSE}
    library(huxtable)
    ```

    # Random Test Output - *after* calling "library(huxtable)"
    a table with pressure data
    ```{r pressure-table2, echo=FALSE}
    pressure
    ```

Despite loading LaTex packages, knitting the index.Rmd for huskydown again throws the error:

 ! LaTeX Error: Environment threeparttable undefined.

Are any LaTex packages loaded with header-includes potentially ignored by huskydown?


Solution

  • Solution as recommended by @dash2: include LaTex packages manually

    "LaTex packages loaded with header-includes ignored by huskydown?" - it seems that this presumption is actually the case. Hence, the LaTex packages can not be included in the YAML header of the index.Rmd file.

    The huskydown thesis template includes the file "template.tex" which defines the documentclass and sources several LaTex packages. Manually adding all LaTex packages required by huxtable to this file solves the issue:

     %-------------------------------
     % Huxtable
     %-------------------------------
     % load LaTex packages needed for huxtable 
     \usepackage{array}
     \usepackage{caption}
     \usepackage{graphicx}
     \usepackage{siunitx}
     \usepackage{colortbl}
     \usepackage{multirow}
     \usepackage{hhline}
     \usepackage{calc}
     \usepackage{tabularx}
     \usepackage{tabulary}
     \usepackage{threeparttable}
     \usepackage{wrapfig}