Longtable in knitr document (PDF): using xtable (or kable)

I am new to knitr and in the past I had some basic knowledge of latex, so I googled already hoping to find a solution that has already been posted somewhere. However, I could not solve my problem. I hope someone is kind enough to help.

I have a data frame of 14 columns and many rows, say 60. Using the data, I need to create a PDF report in the landscape layout and present this data frame as a table.

The closest solution I found is here tex.stackexchange.com: LaTex Longtable spans several pages

I used some hints. However, the table is not set up properly. The far right column (s) are clipped to the right edge of the page. There is no “Continue” at the end of the page at the end of the table. I post my code and picture here.

After deciding to correctly place long tables on the page, if I am missing anything obvious, please do not shoot :) I'm really new to this.

\documentclass[a4paper, landscape]{article}
\usepackage[a4paper, margin=1in, hmarginratio=1:1, landscape]{geometry}
\usepackage{longtable}
\usepackage{graphicx}
\usepackage{xcolor}
\definecolor{myblue}{RGB}{24,57,121}
\usepackage{lipsum}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{array}
\usepackage{rotating}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0.5pt}
\setlength\headheight{40mm} 
\begin{document}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\renewcommand*{\arraystretch}{1.0}
%
\section{My Long Table}
%\begin{center}
%\begin{small}
%\setlongtables
%\begin{longtable}
<<echo=FALSE, eval=TRUE, results='asis'>>=
library(knitr)
library(xtable)
df <- data.frame(replicate(13, sample(1000000:9000000, 60,replace=TRUE)))
df$Sum <- rowSums(df)
totals <- colSums(df)
df <- rbind(df, totals)
names(df) <- c("Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014", "May  2014", "Jun 2014", "Jul 2014",
            "Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014", "Jan 2015", "Sum")
#
dtable <- xtable( x = df)
print ( dtable
          #, table.placement = "H"
          , table.placement = "!htp"
          , caption.placement = "top"
          , include.rownames = TRUE
          , include.colnames = TRUE
          , size = "footnotesize"
          , tabular.environment = 'longtable'
          , floating = FALSE
          #, scalebox = 0.7
          #, width = 0.8
          , add.to.row = list(pos = list(0),command = 
                  paste("\\hline  \\endfirsthead"  ,                          # First caption
                  "\\caption[]{My Caption should be here} \\label{tab:The Table} \\\\ \\hline", # Additional captions
                  paste("&", names(df), collapse=" "),
                  "\\\\ \\hline ",
                  "\\endhead", 
                  "\\hline \\multicolumn{11}{r}{\\textit{Continued}} \\                    
                  \\endfoot
                  \\endlastfoot",collapse=" ")))
@
%\end{longtable}
%\end{small}

%\end{center}
\end{document}

enter image description here enter image description here

+4
source share
1 answer

I think I basically solved this problem in the dev version of kableExtra.

library(knitr)
library(kableExtra)
kable(df, "latex", longtable = T, booktabs = T) %>%
  kable_styling(latex_options = c("repeat_header"), font_size = 7) %>%
  landscape()

since longtable does not support resizebox, you cannot use the "scale_down" parameter in latex_options. I tried to reduce the font size to 7, and it looks pretty good.

+1

All Articles