Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep all rows of a table on the same page in RMarkdown when rendering a PDF file?

Tags:

r

r-markdown

LaTex will keep all rows of a table on the same page if possible. However, I found that, if I render a RMarkdown document into a PDF file, a table may span two pages if it is near the end of a page. This is odd to me because I believe the RMarkdown file is actually converted to a LaTex file before generating the PDF file.

  ---
  title       : "Table"
  output      : 
    pdf_document
  ---

  # Section 1

  # Section 2

  # Section 3

  # Section 4

  # Section 5

  # Section 6

  # Section 7

  # Section 8

  # Section 9

  # Section 10

  # Section 11

  # Section 12

  # Section 13

  Column 1          |     Column 2 |
  -------------     | -------------|
  1) Cell           |     Cell     |
  2) Cell           |     Cell     |
  3) Cell           |     Cell     |
  4) Cell           |     Cell     |
  5) Cell           |     Cell     |
  6) Cell           |     Cell     |
  7) Cell           |     Cell     |
  8) Cell           |     Cell     |
  9) Cell           |     Cell     |
  10) Cell          |     Cell     |
  11) Cell          |     Cell     |
  12) Cell          |     Cell     |
  13) Cell          |     Cell     |
  14) Cell          |     Cell     |
  15) Cell          |     Cell     |
  16) Cell          |     Cell     |
  17) Cell          |     Cell     |
  18) Cell          |     Cell     |

If this is saved in temp.Rmd and then converted to a PDF file by render("temp.Rmd", output_file="temp.pdf"), the first twelve rows appear on page one and the remaining rows appear on page 2:

A table on two pages

Is it possible to ask render (or pandoc?) to add additional lines before a table if necessary such that all rows of a table will appear on the same page?

like image 695
sfcheung Avatar asked Dec 30 '14 08:12

sfcheung


People also ask

How do I embed a table in R Markdown?

To use it, open a Rmd or R document and select “Addins –> Insert Table”.

How do I make a PDF in R Markdown?

You can do this by clicking the Knit PDF button above the text. Your PDF file will be generated and opened as shown below. You're now ready to start writing and processing your own R Markdown files and generate awesome PDF-files using knitr and LaTeX!

What are the three types of sections in an R Markdown document select three options?

The following sections dive into the three components of an R Markdown document in more details: the markdown text, the code chunks, and the YAML header.


2 Answers

The cleanest way would be to add a page break (\newpage or \pagebreak) before the table, although this is unintelligent if you're editing text that would move the position of the table. I guess the stage to do this would be when you're finished editing the document and after a test output (to check for ugly breaks), right before generating the final output.

This answer to a related question is already on SO. Also, apparently \pagebreak is:

actually a LaTeX command, rather than a Markdown one, but most … markdown-to-pdf engines … use LaTex and will accept it.

like image 146
Dave Everitt Avatar answered Sep 26 '22 23:09

Dave Everitt


As was suggested in the comments, the problem is that the default LaTeX template for pandoc uses longtable (normal LaTeX tables don't split over pages). If you don't feel up to creating your own template, you can just modify the default.

Vanilla Pandoc

You can use knitr to produce a normal Markdown file. Then, you can use pandoc to produce the PDF/TeX file using another LaTeX template via

pandoc --template=mytemplate.xex -o myfile.pdf myfile.md

The easiest way to set up a new template is by modifying the default one, which you can get pandoc to dump to the console for you:

pandoc --print-default-template=latex

Then you need to change the line \usepackage{longtable,booktabs} to \usepackage{booktabs}.

If you're on OS X or Linux, then you can use sed and output redirection to directly generate a template without longtable:

pandoc --print-default-template=latex | sed 's/longtable,//' > mytemplate.tex

RStudio

If you're doing this from RStudio, then the easiest option is probably to just change the default template. (Recent releases of RStudio bundle pandoc and so use things differently than system pandoc.) If you look in the "R Markdown" build/status window, you'll see something like this:

output file: rmarkdown.knit.md

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc rmarkdown.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output rmarkdown.pdf --template /Library/Frameworks/R.framework/Versions/3.0/Resources/library/rmarkdown/rmd/latex/default.tex --highlight-style tango --latex-engine /usr/texbin/pdflatex --variable 'geometry:margin=1in' 

Output created: rmarkdown.pdf

(I did this example on a Mac, on Windows or Linux, this will look different.) The template is listed there in the command, which you can then modify as above. This will of course change the behavior for all documents produced via RStudio. To my knowledge, there's currently no publicly facing option to change the template used, but this may change as document templates seem to be an area of active work in recent releases.

EDIT (2016-05-05):

It seems that the use of longtable is hard coded in the recent versions of pandoc, so removing longtable from the preamble will generate some errors. You can get around this by using a filter.

Save the linked python script and

Vanilla Pandoc

add the --filter path/to/filter.py flag to your your pandoc invocation.

RStudio

modify your YAML block for the extra pandoc args:

---
title       : "Table"
pandoc_args : --filter path/to/filter.py
output      : 
    pdf_document
---

As noted in the link above, this will produce plain LaTeX tables, which means no support for footnotes in tables.

like image 43
Livius Avatar answered Sep 22 '22 23:09

Livius