Skip to content
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

wants to merge 1 commit into from


Copy link

@zeehio 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"
  - \usepackage{booktabs}
  - \usepackage{longtable}

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:

After this PR we have a header 馃槂

@@ -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 Nov 21, 2017

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


This comment has been minimized.


zeehio Nov 21, 2017

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 Nov 21, 2017

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
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants