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

纵饮孤独 提交于 2019-12-07 00:23:03

问题


I am new to knitr and I have had some very basic latex knowledge in the past, so I googled already hoping to find a solution that was already posted somewhere. However, I was not able to solve my problem. I am hoping someone will be kind enough to provide help.

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

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

I used some of the hints there. However, the table is not placed properly. The rightmost column(s) are cut-off at the right edge of the page. The table does not have "Continued" word at the end of the page. I am posting my code and the picture here.

I am after a solution to place the longtables properly on the page, if I am missing anything obvious please do not shoot :) I am 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}


回答1:


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 doesn't support resizebox, you cannot use the "scale_down" option in latex_options. I tried to reduce the font size to 7 and it looks pretty well.



来源:https://stackoverflow.com/questions/32265676/longtable-in-a-knitr-pdf-document-using-xtable-or-kable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!