gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:58:12 25 4
gpt4 key购买 nike

通常表格顶部有标题。

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

enter image description here

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

enter image description here

如何使表格标题也位于 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...

最佳答案

This thread can shed some light on the problem you're having . 请注意,最新版本的 pandoc (1.13.2) 现在将表格标题放在 pdf 输出的顶部。

以下示例使用 pandoc-1.12.3

不幸的是\usepackage{floatrow}建议不适用于 longtable (由 LaTeX writer for 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

该表产生以下 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}

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

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

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

table 3

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

关于r - 如何使 RMarkdown (.Rmd) 表格标题位于顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25867689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com