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

一曲冷凌霜 提交于 2019-12-01 05:03:21

问题


Usually tables have captions at the top.

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

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

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

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

回答1:


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.

The following examples are with 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.

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

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}

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

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.



来源:https://stackoverflow.com/questions/25867689/how-to-make-rmarkdown-rmd-table-captions-go-at-the-top

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!