I am using a Rmarkdown with KableExtra to create an Invoice table with collapse_rows and pack_rows. Executing this chunk locally generates the wished table layout:
As soon I try to render this markdown with: rmarkdown::render()
I receive the following error:
output file: billing.knit.md
/usr/bin/pandoc +RTS -K512m -RTS billing.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc6d77420b47ac.tex --self-contained --highlight-style tango --latex-engine pdflatex --variable graphics --include-in-header /tmp/RtmpoyiqX9/rmarkdown-str6d777c393955.html
! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate
I can only prevent this error by skipping the collapse_rows
& pack_rows
.
kbl(tot_cost3[,2:ncol(tot_cost3)], format = "latex", booktabs = TRUE, longtable = TRUE, col.names = c("", "analysis type", "samples", "unit price", "sum", "sub total in CHF"), align = "lllcccc") %>%
kable_styling(latex_options = c("repeat_header")) %>%
collapse_rows(columns = 6, valign = "bottom", latex_hline = "major", longtable_clean_cut = T) %>%
pack_rows(index = table(tot_cost$User_name)) %>%
row_spec(nrow(tot_cost3), bold=TRUE) %>%
kable_paper(full_width = T)
I don't really understand what is going on here, because also a rollback to an earlier, working version of the *.Rmd didn't help. - Still it seems to be somehow related to the pack_rows and collapse_rows function...
Any hint towards a potential solution is highly appreciated!
I've figured out what caused the problem:
There was a linebreak \n in one of the cells of "tot_cost3" causing the table format to break apart when using pack_rows or collapse_rows!
LaTeX, somehow shows surprisingly high tolerance on that, which is quite unusual. As a result, it won’t throw an error if you are just using kable to make some simple tables. However, when you use kableExtra to make some advanced modification, it will start to throw some bugs
from Hao Zhou's "Create Awesome LaTeX Table with knitr::kable and kableExtra" (http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf) Tutorial provided me with the hint to look for formatting issues in the table content itself...