如何使 RMarkdown (.Rmd) 表格标题位于顶部 [英] How to make RMarkdown (.Rmd) table captions go at the top

查看:95
本文介绍了如何使 RMarkdown (.Rmd) 表格标题位于顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常表格顶部有标题.

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

However, RMarkdown always places the caption at the bottom for pdf_document outputs:

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

This is strange because in html docs the caption is automatically placed at the top:

如何让表格标题也出现在 pdf 文档的顶部?

How do I make table captions go to the top in the pdf documents also?

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

Reproducible example (replace pdf_document with html_document to see both) - the contents of my file 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...

推荐答案

这个线程可以说明一些问题关于您遇到的问题.请注意,最新版本的 pandoc (1.13.2) 现在将表格标题放在 pdf 输出的顶部.

以下示例使用 pandoc-1.12.3

不幸的是,\usepackage{floatrow} 建议不适用于 longtable(由 LaTeX writer for pandoc),因为它不是 float环境.

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.

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

该表产生以下乳胶:

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

这使得您描述的表格 - 标题不响应 yaml 标头中的 \floatsetup).

Which makes the table you described -- the caption does not respond to the \floatsetup in the yaml header).

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

To place the caption at the top, \caption{} can be moved. I don't personally know an easy way to force a longtable caption to the top (but I'm not a LaTeX expert).

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

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

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).

```{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')
```

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

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.

这篇关于如何使 RMarkdown (.Rmd) 表格标题位于顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆