New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeat the header row on every page when longtable is used #1463

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
2 participants
@zeehio
Copy link

zeehio commented Nov 21, 2017

kable() allows to generate tables in LaTeX using the longtable package. The longtable package has advantages with respect to the tabular environment as it allows to easily split tables across multiple pages.

A "nice to have" feature for multipage tables is to print the column names at the beginning of each page. This pull request adds the \endhead command after the header. According to the longtable documentation, this command tells longtable to repeat the header of the table on each page. Currently I am manually setting this option on each of my tables, feel free to merge if you feel it is a more sensible default (and feel free to close this if you prefer how things are right now 馃槂).

Minimal example:

---
title: "Untitled"
author: "Sergio Oller"
date: "2017-11-21"
output:
  pdf_document
header-includes:
  - \usepackage{booktabs}
  - \usepackage{longtable}
---

```{r}
data(iris)
knitr::kable(iris, format = "latex", booktabs = TRUE, longtable = TRUE)
```

See the presence of a column header in the second page in the images below:

Before this PR there is no header on the second page:
imatge

After this PR we have a header 馃槂
imatge

@@ -212,7 +212,9 @@ kable_latex = function(
vline = getOption('knitr.table.vline', if (booktabs) '' else '|'),
toprule = getOption('knitr.table.toprule', if (booktabs) '\\toprule' else '\\hline'),
bottomrule = getOption('knitr.table.bottomrule', if (booktabs) '\\bottomrule' else '\\hline'),
midrule = getOption('knitr.table.midrule', if (booktabs) '\\midrule' else '\\hline'),
midrule = getOption('knitr.table.midrule', paste0(c(if (booktabs) "\\midrule" else "\\hline",
if (longtable) "\\endhead" else NULL),

This comment has been minimized.

@yihui

yihui Nov 21, 2017

Owner

Shouldn't \\endhead be added as the top rule instead of the middle rule?

Anyway, I don't want to force this feature, but you can opt-in by setting the global option, e.g.,

options(knitr.table.toprule = "\\endhead")

Thanks!

This comment has been minimized.

@zeehio

zeehio Nov 21, 2017

Author

The \endhead has to go after the header, so it needs to be after the \midrule:

  • Top rule
  • Column names
  • Middle rule
  • Header ends here
  • Actual table rows
  • Bottom rule

I will close the PR, thanks to you for your kind and fast answer!

This comment has been minimized.

@yihui

yihui Nov 21, 2017

Owner

Oh that is right. My brain was dead. Thanks for the contribution!

@zeehio zeehio closed this Nov 21, 2017

@yihui yihui added this to the v0.18 milestone Nov 21, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment