如何将数字作为表格引用?

问题描述 投票:3回答:2

我知道,这可能不是最明智的想法,但我需要插入一个表格作为图/截图(.png),但在标题中将其称为表格。可能吗?

我的监狱与here基本相同,唯一的区别是我在RStudio工作,有rmarkdown,knitr和bookdown。理想情况下,该解决方案应该适用于PDF和HTML输出(但是,PDF现在对我来说更重要)。

r knitr r-markdown bookdown
2个回答
1
投票

作为一个黑客,你可以创建一个用微型字体大小打印的虚拟表,只是为了得到表格标题,然后在同一个块中添加你想要打印的实际表格图像。例如:

---
title: Document Title
output: 
  bookdown::pdf_document2:
    toc: no
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(xtable)
options(xtable.include.rownames=FALSE, xtable.comment=FALSE)

# Dummy table function
dt = function(label, caption=NULL) {
  print(xtable(setNames(data.frame(x=numeric()), " "),
               caption=caption,
               label=paste0("tab:", label)), 
        hline.after=NULL,
        booktabs=FALSE,
        size="\\fontsize{0.1pt}{0.1pt}\\selectfont")
}
```

Lorem Ipsum is simply dummy text of the printing and typesetting industry. As you can see, Table \@ref(tab:lab1) shows something. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. 

```{r, lab1, results="asis", fig.align="center", out.width="6in"}
dt("lab1", "This is table 1")
include_graphics("tab1.png")
```

Now for some more text and then here is Table \@ref(tab:lab2).

```{r, lab2, results="asis", fig.align="center", out.width="4.5in"}
dt("lab2", "This is table 2")
include_graphics("tab2.png")
```

下面,您可以看到输出文档的外观。正如您所看到的,由于隐形虚拟表占用的垂直空间很小,因此标题和表之间会有一些额外的空间。希望一些对乳胶有更好了解的人可以建议如何摆脱这个空间。


enter image description here


0
投票

我有this same issue,当时没有看到这个问题。如果您仍然可以在顶部和底部使用水平线,那么您可以做的是创建一个只包含您要显示的表格图像的表格,如下所示:

```{r echo=F, warning=F}
temp.df <- data.frame(image="![](mytable.png)")
temp.mat <- as.matrix(temp.df)
colnames(temp.mat) <- NULL
knitr::kable(temp.mat, caption="This is my caption")

```

仍然是一个黑客,但比目前接受的答案少一点工作。

© www.soinside.com 2019 - 2024. All rights reserved.