2017-10-10 146 views
1

我使用bookdown以html和pdf生成文档。我怎样才能在表格的标题中插入对文档部分的引用?如何在可同时用于pdf和html输出的bookdown文档的表格标题中插入引用

使用\\ref{sec:FirstSection}正常工作与pdf_book(但不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}." 
) 
``` 

同时使用\\@ref(sec:FirstSection)正常工作与gitbook(但不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)." 
) 
    ``` 

回答

2

您可以使用text references,通过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

本工程为PDF和HTML两种,但可能会有一个更简单的方法。

--- 
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, ".") 
) 
``` 
相关问题