如何使 RMarkdown (.Rmd) 表格标题位于顶部

Rob*_*ace 4 r pandoc r-markdown

通常表格顶部有标题。

但是,RMarkdown 总是将标题放在 pdf_document 输出的底部:

在此处输入图片说明

这很奇怪,因为在 html 文档中,标题会自动放置在顶部:

在此处输入图片说明

如何使表格标题也位于 pdf 文档的顶部?

可重现的示例(用 html_document 替换 pdf_document 以查看两者) - 我的文件 tables.Rmd 的内容:

---
title: "tables"
author: "Robin Lovelace"
date: "09/16/2014"
output: pdf_document
---

text...

Table: This is a table

| id| age|sex | zone|
|--:|---:|:---|----:|
|  1|  59|m   |    2|
|  2|  54|m   |    2|
|  4|  73|f   |    2|

text...

| id| age|sex | zone|
|--:|---:|:---|----:|
|  1|  59|m   |    2|
|  2|  54|m   |    2|
|  4|  73|f   |    2|

Table: This is a table

texts...
Run Code Online (Sandbox Code Playgroud)

Eri*_*ric 6

该线程可以阐明您遇到的问题请注意,最新版本的 pandoc (1.13.2) 现在将表格标题放在 pdf 输出的顶部。

以下示例使用pandoc-1.12.3

不幸的是,该\usepackage{floatrow}建议不适用于longtable(由LaTeX 编写器为 pandoc生成的表环境),因为它不是一个float环境。

---
header-includes: 
  - \usepackage{booktabs}
  - \usepackage{longtable}
  - \usepackage{floatrow}
  - \floatsetup[table]{capposition=top}
output: pdf_document
---

| id| age|sex | zone|
|--:|---:|:---|----:|
|  1|  59|m   |    2|
|  2|  54|m   |    2|
|  4|  73|f   |    2|

Table: This is a table
Run Code Online (Sandbox Code Playgroud)

该表产生以下乳胶:

\begin{longtable}[c]{@{}rrlr@{}}
\toprule\addlinespace
id & age & sex & zone
\\\addlinespace
\midrule\endhead
1 & 59 & m & 2
\\\addlinespace
2 & 54 & m & 2
\\\addlinespace
4 & 73 & f & 2
\\\addlinespace
\bottomrule
\addlinespace
\caption{This is a table}
\end{longtable}
Run Code Online (Sandbox Code Playgroud)

这使得您描述的表格 - 标题不响应\floatsetupyaml 标头中的 )。

表格1

将标题放在顶部,\caption{}可以移动。我个人不知道强制将longtable标题置于顶部的简单方法(但我不是 LaTeX 专家)。

\begin{longtable}[c]{@{}rrlr@{}}
\caption{This is a table} \\
\toprule\addlinespace
id & age & sex & zone
\\\addlinespace
\midrule\endhead
1 & 59 & m & 2
\\\addlinespace
2 & 54 & m & 2
\\\addlinespace
4 & 73 & f & 2
\\\addlinespace
\bottomrule
\end{longtable}
Run Code Online (Sandbox Code Playgroud)

表2

您可以使用该xtable包在table响应\floatsetup序言中的环境中生成表格(尽管该包还为您提供了将标题放置在顶部的选项)。

```{r results = 'asis'}
library(xtable)
# Preset some options for printing your xtables
options(xtable.caption.placement = 'bottom', # notice \floatsetup overrides
        xtable.include.rownames = FALSE,
        xtable.comment = FALSE,
        xtable.booktabs = TRUE)

xtable(
  data.frame(
    id = c(1L, 2L, 4L),
    age = c(59L, 54L, 73L),
    sex = c('m', 'm', 'f'),
    zone = rep(2L, 3)),
  caption = 'This is a table')
```
Run Code Online (Sandbox Code Playgroud)

表3

对所有这些的警告是,如果您决定编译为 html ,那么所有提供给 pandoc 的原始 LaTeX 都将被删除......无赖。