Skip to content

Referencing custom tables #372

Closed
Closed

Description

@mbojan

Hi!

I wrote some code that generates LaTeX or HTML code for custom-made tables. I would very much like to take advantage of \@referencing syntax of bookdown.

It was fairly simple with LaTeX as I had to simply throw in \label{} somewhere in the output.

I am trying to understand how does it work with HTML output. In the md file generated with bookdown::html_document2 a table produced with knitr::kable inserts something like

Table: (\#tab:knitrtable)This is knitr kable table.

Variable    num  ch   f    logical 
---------  ----  ---  ---  --------
A             1  a    a    FALSE   
B             2  b    b    TRUE    
C             3  c    c    TRUE    
D             4  d    d    TRUE    
E             5  e    e    FALSE   

See table \@ref(tab:knitrtable).

I tried inserting <caption> tags with the similar structure as the corresponding HTML output from bookdown::html_document2 but it does not work. I am not sure at which stage of document processing bookdown resolves the references.

Any suggestions how could I make it work?

Activity

  1. yihui commented on Mar 20, 2017

    @yihui
    Collaborator

    It will be nice if you can provide a minimal example of what you tried exactly so I don't have to guess. Anyway, I think if your table caption is of this form <caption>(#tab:label), \\@ref(label) should work.

  2. mbojan commented on Mar 20, 2017

    @mbojan
    ContributorAuthor

    Yes, sorry about the lack of example. The original code would be overwhelming with details so I started to distill a simple example. While doing that I was able to get it to work :D

    Thanks for the suggestion! I was mislead by the \ in the Markdown code above.

    Here is what works for me (essentially):

    The code

    library(magrittr)
    
    test_table <- function(caption, label) {
      require(magrittr)
      m <- matrix(1:2, 4, 3)
      lab <- paste0("(#tab:", label, ")")
      out <- c(
        "<table style='width: 50%;'>",
        paste0("<caption>", lab, caption, "</caption>"),
        structure( paste0("<td>", m, "</td>"), dim=dim(m) ) %>%
          apply(1, paste, collapse="") %>%
          paste0("<tr>", ., "</tr>"),
        "</table>"
      )
      cat(out, sep="\n")
    }

    and later in Rmarkdown

       Blah blah
       ```{r tableone, results="asis"}
       test_table("This is a table", label="tableone")
       ```
       
       See \@ref(tab:tableone).
    
  3. added this to the v0.4 milestone on Mar 20, 2017
  4. yihui commented on Mar 20, 2017

    @yihui
    Collaborator

    Okay, great!

  5. mbojan commented on Mar 20, 2017

    @mbojan
    ContributorAuthor

    Well, in fact, I'm still fighting with it. I am trying to hack htmlTable. See this:

    tab <- withr::with_options(
      list(htmlTableCompat="html"),
      htmlTable::htmlTable(matrix(1:12, 4 ,3), caption="(#tab:tabletwo) A table")
    )
    cat(unclass(tab))

    gives the following HTML:

    <table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
    <caption style='caption-side: top'>
    (#tab:tabletwo) A table</caption>
    <thead>
    </thead>
    <tbody>
    <tr>
    <td style='text-align: center;'>1</td>
    <td style='text-align: center;'>5</td>
    <td style='text-align: center;'>9</td>
    </tr>
    <tr>
    <td style='text-align: center;'>2</td>
    <td style='text-align: center;'>6</td>
    <td style='text-align: center;'>10</td>
    </tr>
    <tr>
    <td style='text-align: center;'>3</td>
    <td style='text-align: center;'>7</td>
    <td style='text-align: center;'>11</td>
    </tr>
    <tr>
    <td style='border-bottom: 2px solid grey; text-align: center;'>4</td>
    <td style='border-bottom: 2px solid grey; text-align: center;'>8</td>
    <td style='border-bottom: 2px solid grey; text-align: center;'>12</td>
    </tr>
    </tbody>
    </table>

    When run in RMarkdown \@ref(tab:tabletwo) seems to give correct hyperlinked reference with number, but the table caption does not work and in browser looks like (#tab:tabletwo) A table.

    Any ideas?

  6. mbojan commented on Mar 20, 2017

    @mbojan
    ContributorAuthor

    I did a bit of debugging...

    It seems that the caption fails to be updated because the regexp in parse_fig_labels (

    if (length(grep('^<caption>', content[i - 0:1])) == 0) next
    ) is looking for "^<caption>" so assumes that there are no tag attributes. This fails in my code because the <caption> tag has attributes.

    Am I correct that the search is done only to check if the (#tab:label) piece is mentioned within HTML <caption>?

    Perhaps the regexp could just catch "^<caption"?

  7. mbojan commented on Mar 20, 2017

    @mbojan
    ContributorAuthor

    I forked, tried, and it fixes the problem. I'm not 100% sure if it does not break anything, but so far so good.

    Want a PR?

  8. added a commit that references this issue on Mar 21, 2017
    f427fdf
  9. yihui commented on Mar 21, 2017

    @yihui
    Collaborator

    I don't know if it is going to break anything, either, but I have merged it anyway. If I discover problems in the future, I may revert it. Thanks!

  10. CarolineXGao commented on Sep 26, 2020

    @CarolineXGao

    Hi Yihui,

    I have a similar issue. Referencing seems to work for html and word but not pdf. Do you know where things might be wrong ?

    Thanks

    Caroline

    
    ---
    title: "Test"
    output:
        bookdown::pdf_document2 
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE,warning=FALSE,message=FALSE)
    

    This is a reference to Table @ref(tab:tableone).

    cat("<caption> Table (\\#tab:tableone)  A Caption </caption>")
    
    
  11. andreapierucci commented on Jan 7, 2021

    @andreapierucci

    HI, @yihui and @mbojan I have a problem similar to Caroline.

    When I use \cite{} in my table in bookdown::pdf_book: like

    \begin{tabular}{|l|l|l|l|}
    \hline
    reference & Charactersitic 1 & Charactersitic 2 & Charactersitic 3 \
    \hline
    \cite{stefc2009partII} & method xx & method xy & method xz \
    \hline
    \cite{stefc2009partII} & method yx & method yy & method yz \
    \hline
    \end{tabular}

    it works only for :word_document2: but not for bookdown::pdf_book:

    I also tried with @cite{stefc2009partII} ; \cite[@stefc2009partII]}; \cite[@stefc2009partII]} ; \@cite(stefc2009partII) no one works...

    I'm very new to LaTeX, Rmd ,and bookdown, I'm using those for writing down my PhD Thesis.

    Could you help me, please?

    .bib
    @Article{stefc2009partII,
    title={Report of the SGMED-09-03 Working Group on
    the Mediterranean Part II},
    author={Massimiliano Cardinale, Anna Cheilari, Hans-Joachim Rätz},
    journal={EUR - Scientific and Technical Research Reports (STEFC) },
    year={2009},
    publisher={JRC}
    }

  12. cderv commented on Apr 1, 2021

    @cderv
    Collaborator

    Hi @andreapierucci Can you open a new issue following the issue guide https://yihui.org/issue/ ?

    Or ask in one of the QA site before if not already done in case you are not sure this is an issue ? Thanks!

  13. github-actions commented on Sep 29, 2021

    @github-actions

    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.

  14. locked as resolved and limited conversation to collaborators on Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions