编织器(PDF)文档中的Longtable:使用xtable(或kable) [英] Longtable in a knitr (PDF) document: using xtable (or kable)

查看:173
本文介绍了编织器(PDF)文档中的Longtable:使用xtable(或kable)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是knitr的新手,过去我已经掌握了一些非常基本的乳胶知识,因此我在Google上搜索以希望找到已经发布到某处的解决方案.但是,我无法解决我的问题.我希望有人能够提供帮助.

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.

我有一个由14列和许多行组成的数据框,比方说60.使用这些数据,我需要以横向布局生成PDF报告,并以表格形式显示此数据框.

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.

我找到的最接近的解决方案在这里 tex.stackexchange.com: LaTex Longtable跨越数个页面

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}

推荐答案

我认为我基本上在kableExtra的开发版本中解决了这个问题.

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

由于longtable不支持resizebox,因此不能在latex_options中使用"scale_down"选项.我试图将字体大小减小到7,看起来还不错.

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.

这篇关于编织器(PDF)文档中的Longtable:使用xtable(或kable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆