Search code examples
rrstudior-markdownpandocbookdown

Cross-referencing of sections in bookdown::word_document2 not working


Title says it all. See below for a minimal (non-)working example.

---
title: "Untitled"
author: "Anonymous"
date: "09/01/2020"
output: bookdown::word_document2
---

# First Section {#sec1}

Etc.


# Second Section

See section \@ref(sec1).

When I knit the above, I get the following warning:

Output created: mre.docx
Warning message:
The label(s) sec1 not found 

The reference in the output is replaced with two question marks (??). When I replace the output option in the YAML header with pdf_document2, there is no error. My template.docx has section numbering enabled for all heading styles and the sections are numbered correctly (just not the references).

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux

Matrix products: default
BLAS:   /usr/lib/libblas.so.3.9.0
LAPACK: /usr/lib/liblapack.so.3.9.0

locale:
 [1] 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      
 [8] LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.6.2  Matrix_1.2-18   bookdown_0.16   htmltools_0.4.0 tools_3.6.2     yaml_2.2.0      reticulate_1.13 Rcpp_1.0.3      rmarkdown_1.17  grid_3.6.2      knitr_1.26      jsonlite_1.6   
[13] digest_0.6.22   xfun_0.11       rlang_0.4.1     lattice_0.20-38 evaluate_0.14  

> pandoc --version
pandoc 2.8
Compiled with pandoc-types 1.20, texmath 0.12, skylighting 0.8.3

Solution

  • I have been struggling with the same issue and I found a workaround

    This is a known issue that the Rstudio group is working on.
    https://github.com/rstudio/bookdown/issues/787

    I was able to work around this problem by using the version of pandoc that is installed with Rstudio (currently 2.7.2). In windows I did this by removing pandoc from the PATH and then restarting my Rstudio session.