简体   繁体   English

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

[英]How to make RMarkdown (.Rmd) table captions go at the top

Usually tables have captions at the top. 通常表格顶部有标题。

However, RMarkdown always places the caption at the bottom for pdf_document outputs: 但是,RMarkdown始终将标题放在pdf_document输出的底部:

在此输入图像描述

This is strange because in html docs the caption is automatically placed at the top: 这很奇怪,因为在html文档中,标题会自动放在顶部:

在此输入图像描述

How do I make table captions go to the top in the pdf documents also? 如何将表格标题放在pdf文档的顶部呢?

Reproducible example (replace pdf_document with html_document to see both) - the contents of my file tables.Rmd: 可重复的示例(用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...

This thread can shed some light on the problem you're having . 这个主题可以解释你遇到的问题 Note that the latest version of pandoc (1.13.2) now places table captions on top in pdf output. 请注意,最新版本的pandoc(1.13.2)现在将表格标题放在pdf输出的顶部。

The following examples are with pandoc-1.12.3 以下示例使用pandoc-1.12.3

Unfortunately the \\usepackage{floatrow} suggestion doesn't work for longtable (the table environment generated by the LaTeX writer for pandoc ), because it is not a float environment. 不幸的是, \\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

This table produces the following latex: 该表生成以下乳胶:

\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}

Which makes the table you described -- the caption does not respond to the \\floatsetup in the yaml header). 这使你描述的表 - 标题不响应yaml标头中的\\floatsetup )。

表格1

To place the caption at the top, \\caption{} can be moved. 要将标题放在顶部,可以移动\\caption{} I don't personally know an easy way to force a longtable caption to the top (but I'm not a LaTeX expert). 我个人并不知道一种简单的方法可以强制使用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}

表2

You can use the xtable package to generate tables that are in a table environment that responds to the \\floatsetup in the preamble (though the package also gives you the option to place the caption at the top). 您可以使用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')
```

表3

The caveat to all of this is that all of the raw LaTeX that is fed to pandoc will be removed if you decide to compile to html... bummer. 所有这一点的警告是,如果您决定编译为html,那么所有输入到pandoc的原始LaTeX都将被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM