2

I am trying to use longtable and lanscape together for a wide table that spans multiple pages. When I do this, the table caption goes from the full width of the page to just squished in the middle of the page.

I have tried using the following suggestions to no avail. R Markdown table caption width with kable and longtable

I am new to r markdown, and I'm not at all familiar with latek so following the above instructions has been confusing for me, though I have tried all the options that I think are correct. Can someone give me very explicit step-by-step instructions for where and what to put in the YAML to fix this issue? Or does anyone have another work around? Thank you for your help

test <- data.frame(col1=rep("MyLongWordsareLong",5),
               col2=rep("MyLongWordsareLong",5),
               col3=rep("MyLongWordsareLong",5),
               col4=rep("MyLongWordsareLong",5),
               col5=rep("MyLongWordsareLong",5),
               col6=rep("MyLongWordsareLong",5))

kable(test,format='latex',booktabs=TRUE,
caption="This is my example caption. See how, when I don't use 
longtable, it extends the full width of the table, but when I use the 
longtable option, it compresses down to only a portion of the table's width. 
Is this weird or is it just me?") %>% 
 landscape()

kable(test,longtable=TRUE,format='latex',booktabs=TRUE,caption="This is my 
example caption. See how, when I don't use longtable, it extends the full 
width of the table, but when I use the longtable option, it compresses down 
to only a portion of the table's width. Is this weird or is it just me?") 
%>% 
landscape()

**edit: I'm knitting to PDF!!

Michelle
  • 193
  • 1
  • 1
  • 6

1 Answers1

1
---
title: "Untitled"
author: "anonymous"
date: "14/12/2020"
header-includes:
  - \usepackage{caption}
output:
  pdf_document:
    keep_tex: yes
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(kableExtra)
```

```{r}
test <- data.frame(col1=rep("MyLongWordsareLong", 5),
               col2=rep("MyLongWordsareLong",5),
               col3=rep("MyLongWordsareLong",5),
               col4=rep("MyLongWordsareLong",5),
               col5=rep("MyLongWordsareLong",5),
               col6=rep("MyLongWordsareLong",5))

kable(test, booktabs=TRUE, caption="This is my example caption. See how, when I don't use  longtable, it extends the full width of the table, but when I use the longtable option, it compresses down to only a portion of the table's width.  Is this weird or is it just me?") %>% 
 landscape()

kable(test, longtable=TRUE, booktabs=TRUE, caption="This is my example caption. See how, when I don't use longtable, it extends the full  width of the table, but when I use the longtable option, it compresses down to only a portion of the table's width. Is this weird or is it just me?") %>% 
landscape()
```

Solution as per R Markdown table caption width with kable and longtable

Paul
  • 527
  • 5
  • 17
  • I'm not exactly sure how what you have changed from what I have? Note that I'm knitting to PDF. – Michelle Dec 15 '20 at 13:36
  • When I add that to my YAML, I get the following error: Error in yaml::yaml.load(..., eval.expr = TRUE) : Parser error: while parsing a block mapping at line 1, column 1 did not find expected key at line 7, column 3 Calls: ... parse_yaml_front_matter -> yaml_load -> Execution halted – Michelle Dec 15 '20 at 13:58
  • I think that's an indenting issue in the YAML. I've tried to fix that in the answer now. – Paul Dec 15 '20 at 14:02
  • Here's my full YAML producing the error. Hopefully this is helpful: --- title: 'Report' author: Michelle L. Stantial date: "`r format(Sys.time(), '%d %B %Y')`" output: header-includes: - \usepackage{caption} pdf_document: keep_tex: yes toc: yes fig_caption: yes includes: in_header: my_header.tex word_document: toc: yes html_document: toc: yes toc_float: collapsed: no fig_width: 6 fig_height: 4 --- – Michelle Dec 15 '20 at 14:05
  • Sorry. Sloppy testing. I've edited again, copy-pasted and knitted successfully so should now be correct. – Paul Dec 15 '20 at 14:11
  • Thanks! I think this *will be* the solution, but now I'm getting some other errors that seem to be related to connecting to the CTAN to download the caption package. But this is progress!!! Much appreciated! – Michelle Dec 15 '20 at 17:53
  • Yeah, just install `caption` using your TeX package manager. – Paul Dec 15 '20 at 18:09