Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table captions are mismatched when kable and rmarkdown table styles are interleaved in RStudio #895

Closed
WilDoane opened this issue Nov 20, 2014 · 6 comments
Labels
won't fix Won't fix due to lack of time/interest or there exist workarounds
Milestone

Comments

@WilDoane
Copy link

There seems to be an issue with table numbering when a mixture of knitr::kable(...) and either the |:---| table format or the ----- ----- table formatting are interleaved.

In the example below, the second table (alpha-beta) gets the "Third table" caption; the third table gets the fourth table's caption; and the fourth table, which should have a caption, doesn't.

Note that knitting to PDF, DOCX, or HTML all produce the same mismatched tables and captions.

Reproducible Example


---
date: "2014 November 19"
output: pdf_document

---

```{r echo = FALSE}
library(devtools)
knitr::kable(head(mtcars, 1), caption = "First table")

```

| alpha | beta |
|:----- |:---- |
| 11    | aa   |
| 22    | bb   |
| 33    | cc   |


```{r echo = FALSE}
knitr::kable(head(BOD, 1), caption = "Third table")

```

```{r echo = FALSE}
knitr::kable(head(CO2, 1), caption = "Fourth table")

```

```{r echo = FALSE}
session_info()
```

## Session info--------------------------------------------------------------
##  setting  value                       
##  version  R version 3.1.2 (2014-10-31)
##  system   x86_64, darwin13.4.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       America/New_York
## Packages------------------------------------------------------------------
##  package    * version date       source                            
##  devtools   * 1.6.1   2014-10-07 CRAN (R 3.1.1)                    
##  digest       0.6.4   2013-12-03 CRAN (R 3.1.0)                    
##  evaluate     0.5.5   2014-04-29 CRAN (R 3.1.0)                    
##  formatR      1.0     2014-08-25 CRAN (R 3.1.1)                    
##  htmltools    0.2.6   2014-09-08 CRAN (R 3.1.1)                    
##  knitr        1.8.2   2014-11-20 Github (yihui/knitr@27e76ed)      
##  rmarkdown    0.3.10  2014-11-09 Github (rstudio/rmarkdown@b96214b)
##  rstudioapi   0.1     2014-03-27 CRAN (R 3.1.0)                    
##  stringr      0.6.2   2012-12-06 CRAN (R 3.1.0)                    
##  yaml         2.1.13  2014-06-12 CRAN (R 3.1.0)

Sample output

First sample caption
mpg cyl disp    hp  drat    wt  qsec    vs  am  gear    carb
Mazda RX4   21  6   160 110 3.9 2.62    16.46   0   1   4   4

Third sample caption
alpha   beta
11  aa
22  bb
33  cc

Fourth sample caption
Time    demand
1   8.3

Plant   Type    Treatment   conc    uptake
Qn1 Quebec  nonchilled  95  16
@WilDoane
Copy link
Author

Reproduced on Win7

Session info--------------------------------------------------------------
## setting value
## version R version 3.1.2 (2014-10-31)
## system i386, mingw32
## ui RTerm
## language (EN)
## collate English_United States.1252
## tz America/New_York
## Packages------------------------------------------------------------------
## package * version date source
## devtools * 1.6.1 2014-10-07 CRAN (R 3.1.1)
## digest 0.6.4 2013-12-03 CRAN (R 3.1.0)
## evaluate 0.5.5 2014-04-29 CRAN (R 3.1.0)
## formatR 1.0 2014-08-25 CRAN (R 3.1.1)
## htmltools 0.2.6 2014-09-08 CRAN (R 3.1.1)
## knitr 1.8.2 2014-11-20 local (/@)
## rmarkdown 0.3.10 2014-11-04 Github (rstudio/rmarkdown@b96214b)
## rstudioapi 0.1 2014-03-27 CRAN (R 3.1.1)
## stringr 0.6.2 2012-12-06 CRAN (R 3.1.0)
## yaml 2.1.13 2014-06-12 CRAN (R 3.1.0

@WilDoane
Copy link
Author

The problem does NOT occur when echo=TRUE

@yihui
Copy link
Owner

yihui commented Nov 27, 2014

Sorry I cannot do anything on knitr's side. This is a pandoc issue, and I do not think it is a pandoc bug, either, since the caption can be placed either above or below the table. It is ambiguous which table the second caption Third table belongs to. You have to put something after the second table to separate the second caption from it.

Table: First table

             mpg   cyl   disp    hp   drat     wt    qsec   vs   am   gear   carb
----------  ----  ----  -----  ----  -----  -----  ------  ---  ---  -----  -----
Mazda RX4     21     6    160   110    3.9   2.62   16.46    0    1      4      4

| alpha | beta |
|:----- |:---- |
| 11    | aa   |
| 22    | bb   |
| 33    | cc   |



Table: Third table

 Time   demand
-----  -------
    1      8.3


Table: Fourth table

Plant   Type     Treatment     conc   uptake
------  -------  -----------  -----  -------
Qn1     Quebec   nonchilled      95       16

@yihui yihui closed this as completed Nov 27, 2014
@yihui yihui added this to the v1.9 milestone Nov 27, 2014
@yihui yihui added the won't fix Won't fix due to lack of time/interest or there exist workarounds label Nov 27, 2014
@WilDoane
Copy link
Author

It's not simply that the output is visually ambiguous, but rather that the caption actually jumps from one table to another under certain conditions.

Given

```{r echo = FALSE}
library(devtools) 
knitr::kable(head(mtcars, 1), caption = "First sample caption")
```

| alpha | beta |
|:----- |:---- |
| 11    | aa   |
| 22    | bb   |
| 33    | cc   |

```{r echo = FALSE}
knitr::kable(head(BOD, 1), caption = "Third sample caption")
```

I think the screenshot, below, demonstrates that (a) the captions are appearing below the tables in the generated PDF and that (b) what should be the caption for table 3 has been incorrectly labeled as table 2 and attached to table 2. I don't think there's any question that the caption is attached to the wrong table.

caption2-on-wrong-table


The ambiguity would seem to be introduced in the intermediate .md file (which is pre-pandoc, of course):

Table: First sample caption

             mpg   cyl   disp    hp   drat     wt    qsec   vs   am   gear   carb
----------  ----  ----  -----  ----  -----  -----  ------  ---  ---  -----  -----
Mazda RX4     21     6    160   110    3.9   2.62   16.46    0    1      4      4


| alpha | beta |
|:----- |:---- |
| 11    | aa   |
| 22    | bb   |
| 33    | cc   |



Table: Third sample caption

 Time   demand
-----  -------
    1      8.3

It's ambiguous in the above .md with which table the Table: Third sample caption should associate. It seems to associate with the earliest table that lacks a caption.


We can see this definitively in this excerpt from the intermediate .tex file:

\begin{longtable}[c]{@{}ll@{}}
\toprule\addlinespace
alpha & beta
\\\addlinespace
\midrule\endhead
11 & aa
\\\addlinespace
22 & bb
\\\addlinespace
33 & cc
\\\addlinespace
\bottomrule
\addlinespace
\caption{Third sample caption}
\end{longtable}

SOLUTION...? (hack!!)
One possible solution is in the .Rmd --> .md stage, for any table that doesn't have an explicit caption, inject a non-printing placeholder <!-- --> (there may be other options that achieve the same result). For example, if the intermediate .md were generated as

Table: First sample caption

             mpg   cyl   disp    hp   drat     wt    qsec   vs   am   gear   carb
----------  ----  ----  -----  ----  -----  -----  ------  ---  ---  -----  -----
Mazda RX4     21     6    160   110    3.9   2.62   16.46    0    1      4      4


| alpha | beta |
|:----- |:---- |
| 11    | aa   |
| 22    | bb   |
| 33    | cc   |

<!-- -->

Table: Third sample caption

 Time   demand
-----  -------
    1      8.3

I believe that forces the Table: Third sample caption to associate with the correct, third table without changing the rendering of the document otherwise. I've confirmed this by manually knitting the .Rmd to .md to .tex to .pdf ... surely there are other use cases I'm not considering and haven't tested.

@yihui
Copy link
Owner

yihui commented Nov 27, 2014

Yep, when I said "put something after the second table", <!-- --> is one possible thing. Thanks for sharing the solution/hack :)

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
won't fix Won't fix due to lack of time/interest or there exist workarounds
Projects
None yet
Development

No branches or pull requests

2 participants