3

我正在将一个 Rmd 文件编织到 docx(客户端需要)并且遇到了一个奇怪的问题:如果我编织到 HTML,我已经包含了内部锚点工作(所以他们可以点击“表 1”,它会带他们到它。但是,如果我编织到 .docx,链接会出现在文本中,但锚点似乎已经消失了。如果我编织到 .pdf,两者都不会出现。

链接和锚点在一个函数 ( tabRef()) 中生成,该函数自动为每个表编号,并为其应用标题。

可以将以下内容复制到 Rmd 文件中以演示我的意思:

---
title: "Pander Test"
author: "Matt"
date: "Wednesday, January 07, 2015"
output:
  html_document:
    keep_md: yes
  pdf_document: default
  word_document: default
---

```{r, echo=FALSE, message=FALSE}
require(pander)

tabRef <- local({
    tag <- numeric()
    created <- logical()
    used <- logical()
    function(label, caption, prefix = options("tabcap.prefix"), 
             sep = options("tabcap.sep"), 
             prefix.highlight = options("tabcap.prefix.highlight")) {
                i <- which(names(tag) == label)
                if (length(i) == 0) {
                    i <- length(tag) + 1
                    tag <<- c(tag, i)
                    names(tag)[length(tag)] <<- label
                    used <<- c(used, FALSE)
                    names(used)[length(used)] <<- label
                    created <<- c(created, FALSE)
                    names(created)[length(created)] <<- label
                }
                if (!missing(caption)) {
                    # Create Anchor
                    created[label] <<- TRUE
                    sprintf('<a name="%s"/> %s', 
                            paste0("Table", i), 
                            paste0(prefix.highlight, prefix, i, sep, prefix.highlight, " ", caption))
                } else {
                    # Create Link to Anchor
                    used[label] <<- TRUE
                    sprintf('[%s](#%s)', 
                            paste0(prefix, tag[label]), 
                            paste0("Table", tag[label]))
                }
            }
})
options(tabcap.prefix = "Table I.", tabcap.sep = ".", tabcap.prefix.highlight = "**")
```

Reference the tables:

Here is the first `r tabRef("MLtab")`.  You should also see `r tabRef("USAtab")`.  

Oh, and this one is also pretty cool: `r tabRef("MLtab2")`.

Make the tables:

```{r, echo=FALSE}
ml <- with(lm(mpg ~ hp + wt), data = mtcars)

set.caption(tabRef("MLtab", "Linear regression with mtcars."))
pander(ml)
```
Add figure for some white space.

```{r}
plot(cars)
```

Here is another:

```{r, echo=FALSE}
pr <- prcomp(USArrests)
set.caption(tabRef("USAtab", "Principal Components Analysis."))
pander(pr)
```

Add figure for some white space.

```{r}
plot(cars)
```

Add figure for some white space.

```{r}
plot(cars)
```

Add figure for some white space.

```{r}
plot(cars)
```

```{r, echo=FALSE}
st <- table(mtcars$am, mtcars$gear)
set.caption(tabRef("MLtab2", "Simple Table."))
pander(st)
```

What about linking backward?  Let's try to see `r tabRef("MLtab")`.

任何建议将不胜感激!

4

0 回答 0