knitr::kable cell contents not aligning properly when knitting to PDF

Viewed 49

I am trying to create a table using 'kable' and formatting with 'kableExtra' in a bookdown document. Everything is formatting correctly EXCEPT that some of the cell contents are misaligned.

Here is my script:

---
title: 'test'
author: "author"
date: "`r Sys.Date()`"
output:
  bookdown::pdf_document2: default
header-includes: 
  - \usepackage{float}
  - \usepackage{amsmath}
---
Sys.setenv(TZ="UTC")

knitr::opts_chunk$set(echo = FALSE, message = FALSE, fig.pos = "H", out.extra = "")

require(tidyverse)
require(tinytex)
require(knitr)
require(kableExtra)

site <- c("site1", "unit1", "unit2", "unit3", "site2", "unit1", "unit2", "unit3",
          "site3", "unit1", "unit2", "unit3", "site4", "unit1", "unit2", "unit3",
          "site5", "unit1", "unit2", "unit3")
season1 <- c(NA, "X", "X", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "X", "X", NA, NA, "X",
             "X", NA)
season2 <- rep(NA, times = 20)
season3 <- c(NA, "X", "X", NA, NA, "X", "X", "X", NA, "X", "X", "X", NA, "X", "X", "X", 
             NA, "X", "X", NA)
season4 <- c(NA, "X", "X", NA, NA, "X", "X", "X", NA, "X", "X", "X", NA, "X", "X", "X", 
             NA, "X", "X", NA)
df <- data.frame("Site" = site, "Season1" = season1, "Season2" = season2, "Season3" = season3, "Season4" = season4)
names(df)[5] <- paste0(names(df)[5], 
                       footnote_marker_symbol(1))

options(knitr.kable.NA = '')

kable(df,
      booktabs = TRUE,
      linesep = "",
      align = "lcccc",
      escape = F, 
      caption = "Table caption") %>%
  add_indent(c(2:4, 6:8, 10:12, 14:16, 18:20)) %>%
  kable_styling(full_width = FALSE,
                position = "left",
                bootstrap_options = "condensed",
                latex_options = c("hold_position", font_size = 10)) %>%
  row_spec(c(1,5,9,13,17), background = "#ECECEC") %>%
  column_spec(1, width = "18em") %>%
  footnote(symbol = "Footnote")

When run in the console it works perfectly. However this is what I get when I knit to PDF:

enter image description here

As you can see, not all the contents of the fifth column are aligned properly, despite using "align" in the kable call.

Any and all help with this issue greatly appreciated. I'm fairly new to bookdown and LaTeX.

0 Answers
Related