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

bookdown::pdf_book does NOT process reference inside table #1184

Closed
5 tasks done
rgaiacs opened this issue Jun 22, 2021 · 5 comments
Closed
5 tasks done

bookdown::pdf_book does NOT process reference inside table #1184

rgaiacs opened this issue Jun 22, 2021 · 5 comments
Labels
feature a feature request or enhancement pandoc concerns upstream pandoc

Comments

@rgaiacs
Copy link

rgaiacs commented Jun 22, 2021

Consider the following minimal working example:

---
title: "mwe"
author: "Raniere Silva"
date: "22/06/2021"
bibliography: mwe.bib
---

```{r}
library(tidyverse)
df <- read_csv("mwe.csv")
df %>% knitr::kable(
    caption = 'Foo.'
)
```

with the following dependencies:

$ cat mwe.csv 
Reference
[@crawfordRiskFactorsMusculoskeletal2021]
$ cat mwe.bib 
@article{crawfordRiskFactorsMusculoskeletal2021,
  title = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}: {{How These Vary}} for {{Two}}-{{Year}}-{{Old}} and {{Older Horses}} and with {{Type}} of {{Injury}}},
  shorttitle = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}},
  author = {Crawford, Kylie L. and Finnane, Anna and Phillips, Clive J. C. and Greer, Ristan M. and Woldeyohannes, Solomon M. and Perkins, Nigel R. and Kidd, Lisa J. and Ahern, Benjamin J.},
  date = {2021-02},
  journaltitle = {Animals},
  volume = {11},
  pages = {270},
  publisher = {{Multidisciplinary Digital Publishing Institute}},
  doi = {10.3390/ani11020270},
  url = {https://www.mdpi.com/2076-2615/11/2/270},
  issue = {2},
  number = {2}
}

The output of

$ Rscript -e 'bookdown::render_book("mwe.Rmd", "bookdown::pdf_book")'

is

bookdown

Bookdown failed to convert [@crawfordRiskFactorsMusculoskeletal2021] in Table 1.

This bug doesn't exist in R Markdown! The output of

$ Rscript -e 'bookdown::render_book("mwe.Rmd", "pdf_document")'

is

rmarkdown

Session Info

$ Rscript -e "xfun::session_info('bookdown')"
R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.10

Locale:
  LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
  LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
  LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
  LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
  LC_ADDRESS=C               LC_TELEPHONE=C            
  LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

Package version:
  base64enc_0.1.3   bookdown_0.22     digest_0.6.27     evaluate_0.14    
  glue_1.4.2        graphics_4.0.2    grDevices_4.0.2   highr_0.9        
  htmltools_0.5.1.1 jsonlite_1.7.2    knitr_1.33        magrittr_2.0.1   
  markdown_1.1      methods_4.0.2     mime_0.10         rlang_0.4.11     
  rmarkdown_2.9     stats_4.0.2       stringi_1.6.2     stringr_1.4.0    
  tinytex_0.32      tools_4.0.2       utils_4.0.2       xfun_0.24        
  yaml_2.2.1       

Checklist

When filing a bug report, please check the boxes below to confirm that you have provided us with the information we need. Have you:

  • formatted your issue so it is easier for us to read?

  • included a minimal, self-contained, and reproducible example?

  • pasted the output from xfun::session_info('bookdown') in your issue?

  • upgraded all your packages to their latest versions (including your versions of R, the RStudio IDE, and relevant R packages)?

  • installed and tested your bug with the development version of the bookdown package using remotes::install_github("rstudio/bookdown") ?

@cderv
Copy link
Collaborator

cderv commented Jun 22, 2021

Thanks for the report.

I believe the difference between the format is because of how the table is written in intermediate .md file.

When using rmarkdown::pdf_document, the table will be written in Markdown

Table: Foo.

|Reference                                 |
|:-----------------------------------------|
|[@crawfordRiskFactorsMusculoskeletal2021] |

which will then be processed by Pandoc, and --citeproc will work.

When using bookdown::pdf_book(), the table will be written in LaTeX directly in the .md file. This happens to support some bookdown special feature I believe.

\begin{table}

\caption{(\#tab:unnamed-chunk-1)Foo.}
\centering
\begin{tabular}[t]{l}
\hline
Reference\\
\hline
[@crawfordRiskFactorsMusculoskeletal2021]\\
\hline
\end{tabular}
\end{table}

In this case, the content won't be processed by Pandoc citeproc, mainly because Pandoc does not support Markdown in LaTeX blocks (old feature request ! jgm/pandoc#2453). This is an old issue of Pandoc which leads to issue here because you have Markdown syntax inside a LaTeX table.

One could leverage this explanation to use another citation engine, and another syntax

For example using biblatex and using \autocitein the table (withescape = FALSE`)

Example
---
title: "mwe"
author: "Raniere Silva"
date: "22/06/2021"
output: 
  bookdown::pdf_book: 
    keep_md: true
    citation_package: biblatex
    keep_tex: true
  pdf_document:
    keep_md: true
    citation_package: biblatex
    keep_tex: true
bibliography: mwe.bib
---

```{r}
df <- data.frame(Reference = "\\autocite{crawfordRiskFactorsMusculoskeletal2021}")
knitr::kable(df, caption = 'Foo.', escape = FALSE)
```

```{cat, engine.opts = list(file = "mwe.bib")}
@article{crawfordRiskFactorsMusculoskeletal2021,
  title = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}: {{How These Vary}} for {{Two}}-{{Year}}-{{Old}} and {{Older Horses}} and with {{Type}} of {{Injury}}},
  shorttitle = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}},
  author = {Crawford, Kylie L. and Finnane, Anna and Phillips, Clive J. C. and Greer, Ristan M. and Woldeyohannes, Solomon M. and Perkins, Nigel R. and Kidd, Lisa J. and Ahern, Benjamin J.},
  date = {2021-02},
  journaltitle = {Animals},
  volume = {11},
  pages = {270},
  publisher = {{Multidisciplinary Digital Publishing Institute}},
  doi = {10.3390/ani11020270},
  url = {https://www.mdpi.com/2076-2615/11/2/270},
  issue = {2},
  number = {2}
}
```

However, currently the best supported way would be to use text reference. This mechanism allow to have some markdown syntax processed in some places not already. You would use this to have the citation processed by Pandoc then moved into the table.

This would look like this
---
title: "mwe"
author: "Raniere Silva"
date: "22/06/2021"
output: 
  bookdown::pdf_book: 
    keep_md: true
    keep_tex: true
bibliography: mwe.bib
---

(ref:citation1) [@crawfordRiskFactorsMusculoskeletal2021]

```{r}
df <- data.frame(Reference = "(ref:citation1)")
knitr::kable(df, caption = 'Foo.')
```

```{cat, engine.opts = list(file = "mwe.bib")}
@article{crawfordRiskFactorsMusculoskeletal2021,
  title = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}: {{How These Vary}} for {{Two}}-{{Year}}-{{Old}} and {{Older Horses}} and with {{Type}} of {{Injury}}},
  shorttitle = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}},
  author = {Crawford, Kylie L. and Finnane, Anna and Phillips, Clive J. C. and Greer, Ristan M. and Woldeyohannes, Solomon M. and Perkins, Nigel R. and Kidd, Lisa J. and Ahern, Benjamin J.},
  date = {2021-02},
  journaltitle = {Animals},
  volume = {11},
  pages = {270},
  publisher = {{Multidisciplinary Digital Publishing Institute}},
  doi = {10.3390/ani11020270},
  url = {https://www.mdpi.com/2076-2615/11/2/270},
  issue = {2},
  number = {2}
}
```

Bibliography

But it would only work with bookdown. I acknownledge this is far from ideal.

Having this to work would mean to be sure the table is written as a markdown table for the code to be processed.
So there is a third way of doing this: using a Table framework that writes in Markdown
This leads to a third workaround example

using **pander** package to write the table
---
title: "mwe"
author: "Raniere Silva"
date: "22/06/2021"
output: 
  bookdown::pdf_book: 
    keep_md: true
    keep_tex: true
  pdf_document:
    keep_md: true
    keep_tex: true
bibliography: mwe.bib
---

```{r table, results='asis'}
df <- data.frame(Reference = "[@crawfordRiskFactorsMusculoskeletal2021]")
pander::pandoc.table(df, caption = '(\\#tab:table) Foo.')
```

See table \@ref(tab:table)

```{cat, engine.opts = list(file = "mwe.bib")}
@article{crawfordRiskFactorsMusculoskeletal2021,
  title = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}: {{How These Vary}} for {{Two}}-{{Year}}-{{Old}} and {{Older Horses}} and with {{Type}} of {{Injury}}},
  shorttitle = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}},
  author = {Crawford, Kylie L. and Finnane, Anna and Phillips, Clive J. C. and Greer, Ristan M. and Woldeyohannes, Solomon M. and Perkins, Nigel R. and Kidd, Lisa J. and Ahern, Benjamin J.},
  date = {2021-02},
  journaltitle = {Animals},
  volume = {11},
  pages = {270},
  publisher = {{Multidisciplinary Digital Publishing Institute}},
  doi = {10.3390/ani11020270},
  url = {https://www.mdpi.com/2076-2615/11/2/270},
  issue = {2},
  number = {2}
}
```

# Bibliography

but in this case the labels from referencing should be inserted manually.

So this is not an easy task to cover all cases. Citation in kable() are not so common and this is a limitation of Pandoc which won't process Markdown inside LaTeX block (so inside LaTeX table). We could however see why some LaTeX is generated - maybe using Markdown instead would work now.

Hope it helps

@cderv cderv added feature a feature request or enhancement pandoc concerns upstream pandoc labels Jun 22, 2021
@rgaiacs
Copy link
Author

rgaiacs commented Jun 23, 2021

@cderv Thanks very much for the long explanation and possible work around.

@rgaiacs
Copy link
Author

rgaiacs commented Jun 24, 2021

@cderv Inspired by your example using pander, I was able to have a work around using kable:

---
title: "mwe"
author: "Raniere Silva"
date: "22/06/2021"
output:
  bookdown::pdf_book:
    keep_md: true
    keep_tex: true
  pdf_document:
    keep_md: true
    keep_tex: true
bibliography: mwe.bib
---

```{r table, results='asis'}
df <- data.frame(Reference = "[@crawfordRiskFactorsMusculoskeletal2021]")
knitr::kable(df, "pipe", caption='Caption here')
```

See table \@ref(tab:table)

```{cat, engine.opts = list(file = "mwe.bib")}
@article{crawfordRiskFactorsMusculoskeletal2021,
  title = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}: {{How These Vary}} for {{Two}}-{{Year}}-{{Old}} and {{Older Horses}} and with {{Type}} of {{Injury}}},
  shorttitle = {The {{Risk Factors}} for {{Musculoskeletal Injuries}} in {{Thoroughbred Racehorses}} in {{Queensland}}, {{Australia}}},
  author = {Crawford, Kylie L. and Finnane, Anna and Phillips, Clive J. C. and Greer, Ristan M. and Woldeyohannes, Solomon M. and Perkins, Nigel R. and Kidd, Lisa J. and Ahern, Benjamin J.},
  date = {2021-02},
  journaltitle = {Animals},
  volume = {11},
  pages = {270},
  publisher = {{Multidisciplinary Digital Publishing Institute}},
  doi = {10.3390/ani11020270},
  url = {https://www.mdpi.com/2076-2615/11/2/270},
  issue = {2},
  number = {2}
}
```

# Bibliography

Use the pipe for knitr::kable's format argument did work for me. I have very little disruption of my workflow (avoid \autocite, text reference and pander).

I'm happy to close this issue. I will let you do it if appropriate. Thanks!

@cderv
Copy link
Collaborator

cderv commented Jun 24, 2021

Oh yes I forgot the obvious: you can ask kable() to output markdown also.

Thanks for sharing back !

@cderv cderv closed this as completed Jun 24, 2021
@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 Dec 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement pandoc concerns upstream pandoc
Projects
None yet
Development

No branches or pull requests

2 participants