]> git.donarmstrong.com Git - xtable.git/commitdiff
Added two examples to xtableGallery plus hyperref.
authordscott <dscott@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Thu, 9 Oct 2014 09:05:58 +0000 (09:05 +0000)
committerdscott <dscott@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Thu, 9 Oct 2014 09:05:58 +0000 (09:05 +0000)
git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@63 edb9625f-4e0d-4859-8d74-9fd3b1da38cb

pkg/vignettes/xtableGallery.Rnw

index b2132a787774f3c9cf7629445c914b6234942f8d..21f30b3e80ac3678c17c049fcf5a0bd1eebd2e2e 100644 (file)
 \usepackage{booktabs}\r
 \usepackage{longtable}\r
 \usepackage{rotating}\r
+\usepackage{array}\r
 \usepackage{tabularx}\r
-\newcommand\code[1]{\texttt{#1}}\r
 \newcommand\p{\vspace{2ex}}\r
 \setcounter{tocdepth}{2}\r
+%% Define \code \proglang and \pkg commands\r
+\newcommand\code[1]{\texttt{#1}}\r
+\newcommand{\pkg}[1]{{\fontseries{b}\selectfont #1}}\r
+\newcommand\proglang[1]{\textsf{#1}}\r
+%% url and hyperref\r
+\usepackage[hyphens]{url}\r
+\usepackage{hyperref}\r
+\r
+\usepackage{listings}\r
+\lstset{language = TeX, basicstyle = \ttfamily,\r
+        keywordstyle = \ttfamily,\r
+        emphstyle = \itshape,\r
+        emph = {options},\r
+        lineskip = 0pt}\r
+\r
+\r
 \begin{document}\r
 \r
-\title{The xtable Gallery}\r
+\title{The \pkg{xtable} Gallery}\r
 \author{Jonathan Swinton and others}\r
 \maketitle\r
 \r
 \r
 \newpage\r
 \r
+\section{Introduction}\r
+\label{sec:introduction}\r
+\r
+This document gives a gallery of tables which can be made using the\r
+\pkg{xtable} package to create \LaTeX\ output. It doubles as a\r
+regression check for the package.\r
+\r
+\r
 <<include=FALSE>>=\r
 library(knitr)\r
 opts_chunk$set(fig.path='figdir/fig', debug=TRUE, echo=TRUE)\r
 @\r
 \r
-\section{Gallery}\r
-\subsection{Data frame}\r
+The first step is to load the package and set an option for this document.\r
 <<results='asis'>>=\r
 library(xtable)\r
 options(xtable.floating = FALSE)\r
+@\r
+\r
+\r
+\section{Gallery}\r
+\subsection{Data frame}\r
+<<results='asis'>>=\r
 data(tli)\r
 xtable(tli[1:10, ])\r
 @\r
@@ -47,13 +76,13 @@ design.matrix <- model.matrix(~ sex*grade, data = tli[1:10, ])
 xtable(design.matrix, digits = 0)\r
 @\r
 \r
+\newpage\r
 \subsection{aov}\r
 <<results='asis'>>=\r
 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)\r
 xtable(fm1)\r
 @\r
 \r
-\newpage\r
 \r
 \subsection{lm}\r
 <<results='asis'>>=\r
@@ -72,6 +101,7 @@ fm2b <- lm(tlimth ~ ethnicty, data = tli)
 xtable(anova(fm2b, fm2))\r
 @\r
 \r
+\newpage\r
 \subsection{glm}\r
 <<results='asis'>>=\r
 fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial)\r
@@ -99,6 +129,7 @@ xtable(summary(pr1))
 # xtable(pr2)\r
 @\r
 \r
+\newpage\r
 \subsection{Time series}\r
 <<results='asis'>>=\r
 temp.ts <- ts(cumsum(1 + round(rnorm(100), 0)),\r
@@ -127,7 +158,6 @@ temp.table
 # }\r
 @\r
 \r
-\newpage\r
 \r
 \section{Helper functions for formatting}\r
 The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are useful for\r
@@ -176,7 +206,7 @@ print(wanttex, sanitize.text.function =
       function(str) gsub("_", "\\_", str, fixed = TRUE))\r
 @\r
 \r
-\newpage\r
+\r
 \r
 \subsection{Markup in tables}\r
 Markup can be included in tables, including in column and row names, by using\r
@@ -190,8 +220,9 @@ mat <- xtable(mat)
 print(mat, sanitize.text.function = function(x) {x})\r
 @\r
 \r
+\newpage\r
 % By David Dahl to demonstrate contribution from David Whitting, 2007-10-09.\r
-\p\r
+\r
 You can also have sanitize functions that are specific to column or\r
 row names.  In the table below, the row name is not sanitized but\r
 column names and table elements are.\r
@@ -377,29 +408,67 @@ print(x.big, hline.after=c(-1, 0), tabular.environment = 'longtable')
 %% The column name alignment is off in the following example.\r
 %% It needs some revision before exposing it. - CR, 7/2/2012\r
 %\r
-%\subsubsection{Long tables with the header on each page}\r
-%The \code{add.to.row} argument can be used to display the header\r
-%for a long table on each page, and to add a "continued" footer\r
-%on all pages except the last page.\r
-%\r
-%<<results=tex>>=\r
-%library(xtable)\r
-%x<-matrix(rnorm(1000), ncol = 10)\r
-%addtorow<-list()\r
-%addtorow$pos<-list()\r
-%addtorow$pos[[1]]<-c(0)\r
-%addtorow$command<-c(paste(\r
-%    "\\hline \n",\r
-%    "  \\endhead \n",\r
-%    "  \\hline \n",\r
-%    "  {\\footnotesize Continued on next page} \n",\r
-%    "  \\endfoot \n",\r
-%    "  \\endlastfoot \n", sep = ""))\r
-%x.big2 <- xtable(x, label = "tabbig2",\r
-%    caption = "Example of longtable with the header on each page")\r
-%print(x.big2, tabular.environment = "longtable",\r
-%include.rownames = FALSE, add.to.row = addtorow, hline.after = c(-1))\r
-%@\r
+%% Tried to fix this and got some of the way, but \hlines are wrong\r
+%% and first column is too wide. - DJS 4/10/2014\r
+%% \subsubsection{Long tables with the header on each page}\r
+%% The \code{add.to.row} argument can be used to display the header\r
+%% for a long table on each page, and to add a "continued" footer\r
+%% on all pages except the last page.\r
+\r
+%% \newcommand{\head}[1]{\centercell{\bfseries#1}}\r
+\r
+%% <<results='asis'>>=\r
+%% x <- matrix(rnorm(1000), ncol = 10)\r
+%% hdr <-  paste(paste("\\multicolumn{1}{c}{",1:9,"} & ", collapse = ""),\r
+%%               "\\multicolumn{1}{c}{10}\\\\")\r
+%% addtorow <- list()\r
+%% addtorow$pos <- list()\r
+%% addtorow$pos[[1]] <- c(0)\r
+%% addtorow$command <- c(paste(\r
+%%     hdr,\r
+%%     "  \\hline \n",\r
+%%     "  \\endhead \n",\r
+%%     "  \\hline \n",\r
+%%     "  {\\footnotesize Continued on next page} \n",\r
+%%     "  \\endfoot \n",\r
+%%     "  \\endlastfoot \n", sep = ""))\r
+%% x.big2 <- xtable(x, label = "tabbig2", align = "lrrrrrrrrrr",\r
+%%                  caption = "Example of longtable with the header on each page")\r
+%% print(x.big2, tabular.environment = "longtable",\r
+%%       include.rownames = FALSE, include.colnames = FALSE,\r
+%%       add.to.row = addtorow)\r
+%% @\r
+\r
+\subsection{Use of \code{add.to.row} argument}\r
+Here is an example of the use of the \code{add.to.row} argument, taken\r
+from a question on Stack Exchange:\r
+\url{http://stackoverflow.com/questions/19846796/adding-titles-to-xtable}.\r
+\r
+<<addtorow>>=\r
+Grade3 <- c("A","B","B","A","B","C","C","D","A","B",\r
+            "C","C","C","D","B","B","D","C","C","D")\r
+Grade6 <- c("A","A","A","B","B","B","B","B","C","C",\r
+            "A","C","C","C","D","D","D","D","D","D")\r
+Cohort <- table(Grade3,Grade6)\r
+rownames(Cohort) <- 1:4\r
+colnames(Cohort) <- 5:8\r
+addtorow <- list()\r
+addtorow$pos <- list()\r
+addtorow$pos[[1]] <- 0\r
+addtorow$pos[[2]] <- 0\r
+addtorow$command <- c('& \\multicolumn{4}{c}{Grade 6} \\\\\n',\r
+                      "Grade 3 & A & B & C & D \\\\\n")\r
+print(xtable(Cohort, caption = 'My Title'), caption.placement = 'bottom',\r
+      add.to.row = addtorow, include.colnames = FALSE,\r
+      floating = TRUE)\r
+@\r
+\r
+\r
+<<printaddtorow, echo = FALSE, results = 'asis'>>=\r
+print(xtable(Cohort, caption = 'My Title'), caption.placement = 'bottom',\r
+      add.to.row = addtorow, include.colnames = FALSE,\r
+      floating = TRUE)\r
+@ %def\r
 \r
 \subsection{Sideways tables}\r
 Requires \verb|\usepackage{rotating}| in the LaTeX\r
@@ -422,6 +491,72 @@ x.rescale <- xtable(x)
 print(x.rescale, scalebox = 0.7)\r
 @\r
 \r
+\subsection{Aligning Fixed Width Columns}\r
+\r
+Note that using specifications such as \lstinline+p{2cm}+ always\r
+produces a \textbf{left aligned} column. What if some other alignment\r
+is desired?\r
+\r
+This is not really a problem with \pkg{xtable} but with the formatting\r
+of tables with fixed width columns and different alignments using\r
+standard \LaTeX.\r
+\r
+This question appears on Stack Exchange:\\\r
+\url{http://tex.stackexchange.com/questions/12703/how-to-create-fixed-width-table-columns-with-text-raggedright-centered-raggedlef}\r
+\r
+The response is to use the \lstinline+array+ package, defining new\r
+column formats.\r
+\r
+\begin{lstlisting}\r
+\newcolumntype{L}[1]{>{\raggedright\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{C}[1]{>{\centering\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}\r
+\end{lstlisting}\r
+\r
+These allow for very sophisticated cell formatting, namely\r
+left-aligned, centred, or right-aligned text, with recognition of line\r
+breaks for the first three new column types. If these lines are\r
+included along with \lstinline+\usepackage{array}+, then the following\r
+is possible.\r
+\r
+\newcolumntype{L}[1]{>{\raggedright\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{C}[1]{>{\centering\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}\r
+\r
+<<arrayexample>>=\r
+df <- data.frame(name = c("A","B"),\r
+                 right = c(1.4, 34.6),\r
+                 left = c(1.4, 34.6),\r
+                 text = c("txt1","txt2"))\r
+@ %def\r
+\r
+Here is the output produced by printing an\r
+\lstinline+"xtable"+ object.\r
+\r
+<<arraytable>>=\r
+print(xtable(df, align = c("|l", "|c", "|R{3cm}","|L{3cm}","| p{3cm}|")),\r
+      floating = FALSE, include.rownames = FALSE)\r
+@ %def\r
+\r
+\r
+Using the \pkg{knitr} option \code{'asis'} we obtain the table.\r
+\r
+<<arrayprint, results = 'asis'>>=\r
+print(xtable(df, align = c("|l", "|c", "|R{3cm}","|L{3cm}","| p{3cm}|")),\r
+      floating = FALSE, include.rownames = FALSE)\r
+@ %def\r
+\r
+\r
+\r
+\r
 \subsection{Table width}\r
 The \code{tabularx} environment is for typesetting tables whose overall width is\r
 fixed. The column alignment code \code{X} denotes columns that will be stretched\r
@@ -458,10 +593,10 @@ x.ltx
 @\r
 \r
 \section{Acknowledgements}\r
-Most of the examples in this gallery are taken from the \code{xtable}\r
+Most of the examples in this gallery are taken from the \pkg{xtable}\r
 documentation.\r
 \r
-\section{R session information}\r
+\section{\proglang{R} session information}\r
 <<results='asis'>>=\r
 toLatex(sessionInfo())\r
 @\r