How to save all rows of a table on one page in RMarkdown when rendering a PDF file?

LaTex will store all table rows on one page, if possible. However, I found that if I make an RMarkdown document into a PDF file, the table can span two pages if it is closer to the end of the page. This is strange for me because I believe that the RMarkdown file was actually converted to a LaTex file before creating 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 using render("temp.Rmd", output_file="temp.pdf") , the first twelve lines appear on the first page and the remaining lines appear on page 2:

A table on two pages

Is it possible to ask render (or pandoc?) To add additional rows in front of the table, if necessary, so that all rows of the table are displayed on one page?

+7
r r-markdown
source share
2 answers

As suggested in the comments, the problem is that the default LaTeX template for pandoc uses longtable (regular LaTeX tables are not paginated). If you don’t want to create your own template, you can simply change the default value.

Vanilla pandoc

You can use knitr to create a normal Markdown file. You can then use pandoc to create a PDF / TeX file using another LaTeX template using

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

The easiest way to set up a new template is to change the default value, which you can get pandoc to send to the console:

 pandoc --print-default-template=latex 

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

If you are on OS X or Linux, you can use sed and redirect the output to directly create the template without longtable :

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

Rstudio

If you are doing this from RStudio, the easiest option is to change the default template. (The latest releases of the RStudio pandoc package and therefore use things differently from the system pandoc.) If you look at the R Markdown build / status window, you will 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 made this example on a Mac, on Windows or Linux, it will look different.) The template is specified in the command, which you can change as described above. This, of course, will change the behavior for all documents created through RStudio. As far as I know, there is currently no public option to change the template used, but this can change, since document templates seem to be an area of ​​active work in recent releases.

EDIT (2016-05-05):

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

Save the associated python script and

Vanilla pandoc

add the --filter path/to/filter.py to your pandoc call.

Rstudio

change your YAML block for additional pandoc arguments:

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

As noted in the link above, this will lead to the creation of simple LaTeX tables, which means there is no support for footnotes in the tables.

+5
source share

The cleanest way is to add a page break ( \newpage or \pagebreak ) in front of the table, although this is unreasonable if you are editing text that will move the position of the table. I assume that at this point you will need to finish editing the document and after the test output (to check for ugly breaks) right before generating the final output.

This answer to the corresponding question is already included in SO. Also, obviously \pagebreak :

actually the LaTeX team, not Markdown, but most ... markdown-to-pdf engines ... use LaTex and accept it.

+4
source share

All Articles