How to output a gt table object into a rmarkdown document -> PDF?



Split from the related How to output a gt table object into a rmarkdown document?

Thank you for the repex code.

My issue is "how to have the "same" output in html and pdf / latex?"

For instance for the colors strippes.


How to output a gt table object into a rmarkdown document?

Hi, I seem to be experiencing a related issue. I have created a table using gt, and am trying to include it in a PDF created via a template from the rticles package.

The .Rmd file knits successfully to .md, but fails to compile to .tex.

Based on the error message and Inspecting the .tex file, I can see the error is happening in TeX formatting at the beginning of the gt table.

Specifically the error is:

! Undefined control sequence.
l.219 \captionsetup

and indeed when you look at the .tex file the place where the table would be begins with \captionsetup [table]{labelformat=empty,skip=1pt}.

Here is a link to a branch of a repo where I documented a minimal example reproducing this error.

The included PDF compiles with the gt table commented out, and creates the included .tex and .log file with errors when the gt table is included.

Any suggestions are much appreciated, since it'd be great to update this template to take advantage of the new capabilities of gt :slight_smile:



I used the code in the linked post as a test / starting point. I tried it on my win box and the latex / pdf failed on the first run. The process froze in Pandoc for more than a couple of minutes. Yet it worked fine on the second run and all runs after that. Might be due to some Latex loading but I am not convinced. Cold starting Rstudio or the machine did not reproduce the first bug.

The output is as expected but for the table color. I am not literate enough in these components to assert if this is due to a knit, pandoc, markdown, Latex classes, Latex code issues. Or a mixed of those.

I am also looking forward the gt package. It has a huge potential for html and pdf output while offering a lot of features required for complex tables.




I was able to remedy the error by specifying the caption package in the header of the yaml. It might be that the inclusion of the header packages messes with the latex packages invoked by gt.

title: Article template: A reproducible RMarkdown template with examples for beginners 
  - name: Name1 
    affiliation: Uni1
    footnote: Corresponding Author
  - code: Uni1
    address: Research Center, 123 Science Dr

abstract: Enter the text for your abstract here. 

journal: "Building and Environment"
date: "`r Sys.Date()`"
bibliography: Bibliography.bib
    base_format: rticles::elsevier_article
csl: elsevier-with-titles.csl
geometry: "left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm"
- \usepackage{setspace}
- \doublespacing
- \usepackage{caption} # Insert the package used by gt


Thanks! Adding \usepackage{caption} to the yaml header per your example above means the knitted .md successfully converts to PDF.

For what it's worth am running RStudio on Windows, have MikTeX installed, and before I added this fix, the previous error converting to PDF happened every time I included the gt table w/o the added header line, regardless of first or subsequent attempt to run it.

@jm_t, it sounds like you are experiencing a related but different issue, are you able to share a reproducible minimal example of the code that is an issue for you?