2017-10-10 15 views
1

J'utilise bookdown pour générer un document en format html et pdf. Comment pourrais-je insérer une référence à une section du document dans la légende d'une table?Comment insérer une référence dans une légende de table dans un document de réservation qui fonctionne à la fois pour les sorties pdf et html

L'utilisation \\ref{sec:FirstSection} fonctionne très bien avec pdf_book (mais pas gitbook):

--- 
title: "Test" 
output: bookdown::pdf_book 
--- 

# A section {#sec:FirstSection} 
The dataset in Table \@ref(tab:aTable) contains some data. 

# Another section 
```{r, aTable, echo = FALSE} 
knitr::kable(
    cars[1:5, ], 
    caption = "See Section \\ref{sec:FirstSection}." 
) 
``` 

tout en utilisant \\@ref(sec:FirstSection) fonctionne très bien avec gitbook (mais pas pdf_book)

--- 
title: "Test" 
output: bookdown::gitbook 
--- 

# A section {#sec:FirstSection} 
The dataset in Table \@ref(tab:aTable) contains some data. 

# Another section 
```{r, aTable, echo = FALSE} 
knitr::kable(
    cars[1:5, ], 
    caption = "See Section \\@ref(sec:FirstSection)." 
) 
    ``` 

Répondre

2

Vous pouvez utiliser text references, une extension Markdown fournie par bookdown.

--- 
title: "Test" 
output: bookdown::gitbook 
--- 

# A section {#sec:FirstSection} 

The dataset in Table \@ref(tab:aTable) contains some data. 

# Another section 

(ref:aTable-caption) See Section \@ref(sec:FirstSection). 

```{r, aTable, echo = FALSE} 
knitr::kable(
    cars[1:5, ], 
    caption = "(ref:aTable-caption)" 
) 
``` 
0

Cela fonctionne pour les pdf et html, mais il pourrait y avoir un moyen plus facile.

--- 
title: "Test" 
output: bookdown::gitbook 
--- 

# A section {#sec:FirstSection} 
The dataset in Table \@ref(tab:aTable) contains some data. 

# Another section 
```{r, aTable, echo = FALSE} 
txt <- ifelse(knitr:::is_latex_output(), "\\ref{sec:FirstSection}",  
       "\\@ref(sec:FirstSection)") 

knitr::kable(
    cars[1:5, ], 
    caption = paste0("See Section ", txt, ".") 
) 
```