]> git.donarmstrong.com Git - xtable.git/blob - pkg/vignettes/xtableGallery.Rnw
807e8f672d6b8fac2abac122b4b602d708dc02ac
[xtable.git] / pkg / vignettes / xtableGallery.Rnw
1 %\VignetteIndexEntry{xtable Gallery}\r
2 %\VignetteDepends{xtable}\r
3 %\VignetteKeywords{LaTeX, HTML, table}\r
4 %\VignettePackage{xtable}\r
5 % !Rnw weave = knitr\r
6 % \VignetteEngine{knitr::knitr}\r
7 %**************************************************************************\r
8 \documentclass{article}\r
9 \usepackage[a4paper,height=24cm]{geometry} % geometry first\r
10 \usepackage{array}\r
11 \usepackage{booktabs}\r
12 \usepackage{longtable}\r
13 \usepackage{parskip}\r
14 \usepackage{rotating}\r
15 \usepackage{tabularx}\r
16 \usepackage{titlesec}\r
17 \usepackage{hyperref} % hyperref last\r
18 \titleformat\subsubsection{\bfseries\itshape}{}{0pt}{}\r
19 \newcommand\p{\vspace{2ex}}\r
20 \newcommand\code[1]{\texttt{#1}}\r
21 \newcommand\pkg[1]{\textbf{#1}}\r
22 \setcounter{tocdepth}{2}\r
23 \begin{document}\r
24 \r
25 \title{The \pkg{xtable} Gallery}\r
26 \author{Jonathan Swinton and others}\r
27 \maketitle\r
28 \r
29 \tableofcontents\r
30 \r
31 \newpage\r
32 \r
33 \section{Introduction}\r
34 This document gives a gallery of tables which can be made using the\r
35 \pkg{xtable} package to create \LaTeX\ output. It doubles as a\r
36 regression check for the package.\r
37 \r
38 <<include=FALSE>>=\r
39 library(knitr)\r
40 opts_chunk$set(fig.path='figdir/fig', debug=TRUE, echo=TRUE)\r
41 set.seed(1234)\r
42 @\r
43 \r
44 The first step is to load the package and set an option for this document.\r
45 <<results='asis'>>=\r
46 library(xtable)\r
47 options(xtable.floating = FALSE)\r
48 options(xtable.timestamp = "")\r
49 @\r
50 \r
51 \section{Gallery}\r
52 \subsection{Data frame}\r
53 <<results='asis'>>=\r
54 data(tli)\r
55 xtable(tli[1:10, ])\r
56 @\r
57 \r
58 \subsection{Matrix}\r
59 <<results='asis'>>=\r
60 design.matrix <- model.matrix(~ sex*grade, data = tli[1:10, ])\r
61 xtable(design.matrix, digits = 0)\r
62 @\r
63 \r
64 \newpage\r
65 \subsection{aov}\r
66 <<results='asis'>>=\r
67 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)\r
68 xtable(fm1)\r
69 @\r
70 \r
71 \subsection{lm}\r
72 <<results='asis'>>=\r
73 fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)\r
74 xtable(fm2)\r
75 @\r
76 \r
77 \subsubsection{Anova table (one model)}\r
78 <<results='asis'>>=\r
79 xtable(anova(fm2))\r
80 @\r
81 \r
82 \subsubsection{Anova table (two models)}\r
83 <<results='asis'>>=\r
84 fm2b <- lm(tlimth ~ ethnicty, data = tli)\r
85 xtable(anova(fm2b, fm2))\r
86 @\r
87 \r
88 \newpage\r
89 \subsection{glm}\r
90 <<results='asis'>>=\r
91 fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial)\r
92 xtable(fm3)\r
93 @\r
94 \r
95 \subsubsection{Analysis of deviance}\r
96 <<results='asis'>>=\r
97 xtable(anova(fm3))\r
98 @\r
99 \r
100 \subsection{prcomp}\r
101 <<results='asis'>>=\r
102 pr1 <- prcomp(USArrests)\r
103 xtable(pr1)\r
104 @\r
105 \r
106 \p\r
107 <<results='asis'>>=\r
108 xtable(summary(pr1))\r
109 @\r
110 \r
111 <<include=FALSE>>=\r
112 # pr2 <- princomp(USArrests)\r
113 # xtable(pr2)\r
114 @\r
115 \r
116 \newpage\r
117 \r
118 \subsection{Time series}\r
119 <<results='asis'>>=\r
120 temp.ts <- ts(cumsum(1 + round(rnorm(100), 0)),\r
121               start = c(1954, 7), frequency = 12)\r
122 temp.table <- xtable(temp.ts, digits = 0)\r
123 temp.table\r
124 @\r
125 \r
126 \subsection{Flat tables}\r
127 \label{sec:flat-tables}\r
128 \r
129 See the \textbf{Details} section of the help for \code{ftable} for a\r
130 description of these tables, which are flat versions of\r
131 multi-dimensional contingency tables. They require special methods to\r
132 enable them to be printed using \pkg{xtable}\r
133 \r
134 \r
135 <<ftable>>=\r
136 data(mtcars)\r
137 mtcars$cyl <- factor(mtcars$cyl, levels = c("4","6","8"),\r
138                      labels = c("four","six","eight"))\r
139 tbl <- ftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear,\r
140               row.vars = c(2, 4),\r
141               dnn = c("Cylinders", "V/S", "Transmission", "Gears"))\r
142 \r
143 \r
144 @ %def\r
145 \r
146 \p\r
147 <<ftablecheck>>=\r
148 xftbl <- xtableFtable(tbl, method = "compact")\r
149 print.xtableFtable(xftbl, booktabs = TRUE)\r
150 @ %def\r
151 <<ftable1, results = 'asis'>>=\r
152 xftbl <- xtableFtable(tbl)\r
153 print.xtableFtable(xftbl)\r
154 @ %def\r
155 \r
156 \p\r
157 <<ftable2, results = 'asis'>>=\r
158 xftbl <- xtableFtable(tbl, method = "row.compact")\r
159 print.xtableFtable(xftbl, rotate.colnames = TRUE)\r
160 @ %def\r
161 \r
162 \p\r
163 <<ftable3, results = 'asis'>>=\r
164 xftbl <- xtableFtable(tbl, method = "col.compact")\r
165 print.xtableFtable(xftbl, rotate.rownames = TRUE)\r
166 @ %def\r
167 \r
168 \p\r
169 Booktabs is incompatible with vertical lines in tables, so the\r
170 vertical dividing line is removed.\r
171 <<ftable4, results = 'asis'>>=\r
172 xftbl <- xtableFtable(tbl, method = "compact")\r
173 print.xtableFtable(xftbl, booktabs = TRUE)\r
174 @ %def\r
175 \r
176 \newpage\r
177 \r
178 <<include=FALSE>>=\r
179 # ## Demonstrate saving to file\r
180 # for(i in c("latex", "html")) {\r
181 #   outFileName <- paste("xtable.", ifelse(i=="latex", "tex", i), sep = "")\r
182 #   print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
183 #         latex.environments = NULL)\r
184 #   print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
185 #         latex.environments = "")\r
186 #   print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
187 #         latex.environments = "center")\r
188 #   print(xtable(anova(glm.D93, test = "Chisq")),\r
189 #         type = i, file = outFileName,\r
190 #         append = TRUE)\r
191 #   print(xtable(anova(glm.D93)), hline.after = c(1),\r
192 #         size = "small", type = i,\r
193 #         file = outFileName, append = TRUE)\r
194 #   # print(xtable(pr2), type = i, file = outFileName, append = TRUE)\r
195 # }\r
196 @\r
197 \r
198 \section{Automatic formatting}\r
199 \subsection{Suggest alignment, digits, and display}\r
200 The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are\r
201 useful for formatting tables in a sensible way. Consider the output\r
202 produced by the default formatting.\r
203 \r
204 <<results='asis'>>=\r
205 dat <- mtcars[1:3, 1:6]\r
206 x <- xtable(dat)\r
207 x\r
208 @\r
209 \r
210 \p\r
211 Now change the default alignment, digits and display using helper functions\r
212 \code{xalign}, \code{xdigits}, and \code{xdisplay}. This produces a better\r
213 format as shown below.\r
214 \r
215 <<results='asis'>>=\r
216 align(x) <- xalign(x)\r
217 digits(x) <- xdigits(x)\r
218 display(x) <- xdisplay(x)\r
219 x\r
220 @\r
221 \r
222 \subsection{Shorthand notation}\r
223 For convenience, the three `autoformat' functions (\code{xalign},\r
224 \code{xdigits}, and \code{xdisplay}) can be applied together when an\r
225 \code{xtable} is created, using the \code{auto} argument:\r
226 \r
227 <<results='asis'>>=\r
228 xtable(dat, auto = TRUE)\r
229 @\r
230 \r
231 \p\r
232 Similarly, the \code{autoformat} function can be used to postprocess an\r
233 existing \code{xtable}:\r
234 \r
235 <<results='asis'>>=\r
236 x <- xtable(dat)\r
237 autoformat(x)\r
238 @\r
239 \r
240 \newpage\r
241 \r
242 \subsection{Math-Style Exponents}\r
243 If you prefer $5 \times 10^5$ in your tables to 5e5, the\r
244 \code{math.style.exponents} option to \code{print.xtable} is useful:\r
245 \r
246 <<results='asis'>>=\r
247 print(xtable(data.frame(text = c("foo","bar"),\r
248                         googols = c(10e10,50e10),\r
249                         small = c(8e-24,7e-5),\r
250                         row.names = c("A","B")),\r
251              display = c("s","s","g","g")),\r
252       math.style.exponents = TRUE)\r
253 @\r
254 \r
255 this option also supports the values \code{ensuremath} which uses\r
256 \code{\char`\\ensuremath} instead of \code{\$\$} and \code{UTF-8}\r
257 which uses UTF-8 to approximate the \LaTeX typesetting.\r
258 \r
259 \r
260 \section{Sanitization}\r
261 <<results='asis'>>=\r
262 insane <- data.frame(Name = c("Ampersand","Greater than","Less than",\r
263                             "Underscore","Per cent","Dollar",\r
264                             "Backslash","Hash","Caret","Tilde",\r
265                             "Left brace","Right brace"),\r
266                      Character = I(c("&",">","<","_","%","$",\r
267                                      "\\","#","^","~","{","}")))\r
268 colnames(insane)[2] <- paste(insane[, 2], collapse = "")\r
269 xtable(insane)\r
270 @\r
271 \r
272 \p\r
273 Sometimes you might want to have your own sanitization function.\r
274 \r
275 \r
276 <<results='asis'>>=\r
277 wanttex <- xtable(data.frame(Column =\r
278                              paste("Value_is $10^{-",1:3,"}$", sep = "")))\r
279 print(wanttex, sanitize.text.function =\r
280       function(str) gsub("_", "\\_", str, fixed = TRUE))\r
281 @\r
282 \r
283 \p\r
284 Sanitization can be useful in formatting column headings and row names:\r
285 \r
286 <<sanitize3>>=\r
287 dat <- mtcars[1:3, 1:6]\r
288 large <- function(x){\r
289   paste0('{\\Large{\\bfseries ', x, '}}')\r
290 }\r
291 italic <- function(x){\r
292   paste0('{\\emph{ ', x, '}}')\r
293 }\r
294 @ %def\r
295 \r
296 <<sanitize4, results = 'asis'>>=\r
297 print(xtable(dat),\r
298       sanitize.rownames.function = italic,\r
299       sanitize.colnames.function = large,\r
300       booktabs = TRUE)\r
301 @ %def\r
302 \r
303 \r
304 \r
305 \newpage\r
306 \r
307 \subsection{Markup in tables}\r
308 Markup can be included in tables, including in column and row names,\r
309 by using a custom \code{sanitize.text.function}.\r
310 \r
311 <<results='asis'>>=\r
312 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)\r
313 rownames(mat) <- "$y_{t-1}$"\r
314 colnames(mat) <- c("$R^2$", "$\\bar{x}$", "F-stat", "S.E.E", "DW")\r
315 mat <- xtable(mat)\r
316 print(mat, sanitize.text.function = function(x) {x})\r
317 @\r
318 \r
319 % By David Dahl to demonstrate contribution from David Whitting, 2007-10-09.\r
320 \p\r
321 You can also have sanitize functions that are specific to column or\r
322 row names.  In the table below, the row name is not sanitized but\r
323 column names and table elements are.\r
324 \r
325 <<results='asis'>>=\r
326 money <- matrix(c("$1,000","$900","$100"), ncol = 3,\r
327                 dimnames = list("$\\alpha$",\r
328                                 c("Income (US$)","Expenses (US$)",\r
329                                   "Profit (US$)")))\r
330 print(xtable(money), sanitize.rownames.function = function(x) {x})\r
331 @\r
332 \r
333 \section{Format examples}\r
334 \subsection{Adding a centering environment}\r
335 <<results='asis'>>=\r
336 print(xtable(anova(fm3), caption = "\\tt latex.environments = \"\""),\r
337       floating = TRUE, latex.environments = "")\r
338 print(xtable(anova(fm3), caption = "\\tt latex.environments = \"center\""),\r
339       floating = TRUE, latex.environments = "center")\r
340 @\r
341 \r
342 \newpage\r
343 \r
344 \subsection{Column alignment}\r
345 <<results='asis'>>=\r
346 tli.table <- xtable(tli[1:10, ])\r
347 align(tli.table) <- rep("r", 6)\r
348 tli.table\r
349 @\r
350 \r
351 \subsubsection{Left aligned strings with column lines}\r
352 <<results='asis'>>=\r
353 align(tli.table) <- "|rrl|l|lr|"\r
354 tli.table\r
355 @\r
356 \r
357 \subsubsection{Fixed width columns}\r
358 <<results='asis'>>=\r
359 align(tli.table) <- "|rr|lp{3cm}l|r|"\r
360 tli.table\r
361 @\r
362 \r
363 \newpage\r
364 \r
365 \subsection{Number of digits}\r
366 One number for all columns,\r
367 <<results='asis'>>=\r
368 display(tli.table)[c(2,6)] <- "f"\r
369 digits(tli.table) <- 3\r
370 tli.table\r
371 @\r
372 \r
373 \p\r
374 or one for each column, including the row names,\r
375 <<results='asis'>>=\r
376 digits(tli.table) <- 1:(ncol(tli)+1)\r
377 tli.table\r
378 @\r
379 \r
380 \p\r
381 or as a full matrix.\r
382 <<results='asis'>>=\r
383 digits(tli.table) <- matrix(0:4, nrow = 10, ncol = ncol(tli)+1)\r
384 tli.table\r
385 @\r
386 \r
387 \newpage\r
388 \r
389 \subsection{Suppress row/column names}\r
390 \subsubsection{Suppress row names}\r
391 <<results='asis'>>=\r
392 tli.table <- xtable(tli[1:10, ])\r
393 print(tli.table, include.rownames = FALSE)\r
394 @\r
395 \r
396 \p\r
397 If you want a vertical line on the left, you need to change the \code{align}\r
398 attribute.\r
399 <<results='asis'>>=\r
400 align(tli.table) <- "|r|r|lp{3cm}l|r|"\r
401 print(tli.table, include.rownames = FALSE)\r
402 @\r
403 \r
404 \p\r
405 Revert the alignment to what is was before.\r
406 <<>>=\r
407 align(tli.table) <- "|rr|lp{3cm}l|r|"\r
408 @\r
409 \r
410 \newpage\r
411 \r
412 \subsubsection{Suppress column names}\r
413 <<results='asis'>>=\r
414 print(tli.table, include.colnames = FALSE)\r
415 @\r
416 \r
417 \p\r
418 Note the doubled header lines which can be suppressed.\r
419 <<results='asis'>>=\r
420 print(tli.table, include.colnames = FALSE,\r
421       hline.after = c(0,nrow(tli.table)))\r
422 @\r
423 \r
424 \subsubsection{Suppress row and column names}\r
425 <<results='asis'>>=\r
426 print(tli.table, include.colnames = FALSE, include.rownames = FALSE)\r
427 @\r
428 \r
429 \newpage\r
430 \r
431 \subsection{Rotate row/column names}\r
432 The \code{rotate.rownames} and \code{rotate.colnames} arguments can be\r
433 used to rotate the row and/or column names. This requires\r
434 \verb|\usepackage{rotating}| in the \LaTeX\ preamble.\r
435 \r
436 <<results='asis'>>=\r
437 print(tli.table, rotate.rownames = TRUE, rotate.colnames = TRUE)\r
438 @\r
439 \r
440 \newpage\r
441 \r
442 \subsection{Horizontal lines}\r
443 \subsubsection{Line locations}\r
444 Use the \code{hline.after} argument to specify the position of the\r
445 horizontal lines.\r
446 \r
447 <<results='asis'>>=\r
448 print(xtable(anova(fm3)), hline.after = c(1))\r
449 @\r
450 \r
451 \subsubsection{Line styles}\r
452 Specifying \code{booktabs = TRUE} will generate three line types. By\r
453 default, when no value is given for \code{hline.after}, a\r
454 \verb|\toprule| will be drawn above the table, a \verb|\midrule| after\r
455 the table headings and a \verb|\bottomrule| below the table. This\r
456 requires \verb|\usepackage{booktabs}| in the \LaTeX\ preamble.\r
457 \r
458 \p\r
459 \r
460 The top and bottom rules are slightly thicker than the mid rule. The\r
461 thickness of the lines can be set via the \LaTeX\ lengths\r
462 \verb|\heavyrulewidth| and \verb|\lightrulewidth|.\r
463 \r
464 <<results='asis'>>=\r
465 tli.table <- xtable(tli[1:10, ])\r
466 print(tli.table, include.rownames = FALSE, booktabs = TRUE)\r
467 @\r
468 \r
469 \p\r
470 \r
471 If \code{hline.after} includes \code{-1}, a \verb|\toprule| will be\r
472 drawn above the table. If \code{hline.after} includes the number of\r
473 rows in the table, a \verb|\bottomrule| will be drawn below the\r
474 table. For any other values specified in \code{hline.after}, a\r
475 \verb|\midrule| will be drawn after that line of the table.\r
476 \r
477 \p\r
478 The following table has more than one \verb|\midrule|.\r
479 \r
480 <<results='asis'>>=\r
481 bktbs <- xtable(matrix(1:10, ncol = 2))\r
482 hlines <- c(-1, 0, 1, nrow(bktbs))\r
483 print(bktbs, booktabs = TRUE, hline.after = hlines)\r
484 @\r
485 \r
486 \subsection{Table level commands}\r
487 <<results='asis'>>=\r
488 print(xtable(anova(fm3)), size = "large")\r
489 @\r
490 \r
491 \p\r
492 <<results='asis'>>=\r
493 print(xtable(anova(fm3)), size = "\\setlength{\\tabcolsep}{12pt}")\r
494 @\r
495 \r
496 \subsection{Long tables}\r
497 Requires \verb|\usepackage{longtable}| in the \LaTeX\ preamble.\r
498 \r
499 <<results='asis'>>=\r
500 x <- matrix(rnorm(1000), ncol = 10)\r
501 x.big <- xtable(x, caption = "A \\code{longtable} spanning several pages")\r
502 print(x.big, hline.after=c(-1, 0), tabular.environment = "longtable")\r
503 @\r
504 \r
505 Extra features of the \pkg{longtable} \LaTeX{} package can typically\r
506 be activated using \code{add.to.row}, as shown below.\r
507 \r
508 <<results='asis'>>=\r
509 add.to.row <- list(pos = list(0), command = NULL)\r
510 command <- paste0("\\hline\n\\endhead\n",\r
511                   "\\hline\n",\r
512                   "\\multicolumn{", dim(x)[2] + 1, "}{l}",\r
513                   "{\\footnotesize Continued on next page}\n",\r
514                   "\\endfoot\n",\r
515                   "\\endlastfoot\n")\r
516 add.to.row$command <- command\r
517 print(x.big, hline.after=c(-1), add.to.row = add.to.row,\r
518       tabular.environment = "longtable")\r
519 @\r
520 \r
521 \r
522 \newpage\r
523 \r
524 \subsection{Use of \code{add.to.row} argument}\r
525 The following frequency table has outer dimnames: \code{Grade3} and\r
526 \code{Grade6}.\r
527 \r
528 <<>>=\r
529 Grade3 <- c("A","B","B","A","B","C","C","D","A","B",\r
530             "C","C","C","D","B","B","D","C","C","D")\r
531 Grade6 <- c("A","A","A","B","B","B","B","B","C","C",\r
532             "A","C","C","C","D","D","D","D","D","D")\r
533 Cohort <- table(Grade3, Grade6)\r
534 Cohort\r
535 @\r
536 \r
537 \p\r
538 The default behavior of \code{print.xtable} is to strip outer dimnames.\r
539 <<results='asis'>>=\r
540 xtable(Cohort)\r
541 @\r
542 \r
543 \p\r
544 The desired column labels can be created using \code{add.to.row}, in this case\r
545 applying two commands to ``row number zero'' while suppressing the basic column\r
546 names.\r
547 \r
548 <<results='asis'>>=\r
549 addtorow <- list()\r
550 addtorow$pos <- list(0, 0)\r
551 addtorow$command <- c("& \\multicolumn{4}{c}{Grade 6} \\\\\n",\r
552                       "Grade 3 & A & B & C & D \\\\\n")\r
553 print(xtable(Cohort), add.to.row = addtorow, include.colnames = FALSE)\r
554 @\r
555 \r
556 \subsection{Sideways tables}\r
557 Requires \verb|\usepackage{rotating}| in the LaTeX\r
558 preamble.  Sideways tables can't be forced in place with the \code{[H]}\r
559 specifier, but you can use the \verb|\clearpage| command to get them\r
560 fairly nearby.\r
561 \r
562 <<results='asis'>>=\r
563 x <- x[1:30, ]\r
564 x.side <- xtable(x, caption = "A sideways table")\r
565 print(x.side, floating = TRUE, floating.environment = "sidewaystable")\r
566 @\r
567 \clearpage\r
568 \r
569 \subsection{Rescaled tables}\r
570 Specify a \code{scalebox} value to rescale the table.\r
571 <<results='asis'>>=\r
572 x <- x[1:20, ]\r
573 x.rescale <- xtable(x)\r
574 print(x.rescale, scalebox = 0.7)\r
575 @\r
576 \r
577 \subsection{Aligning fixed width columns}\r
578 Note that using specifications such as \verb|p{2cm}| always\r
579 produces a \textbf{left aligned} column. What if some other alignment\r
580 is desired?\r
581 \r
582 This is not really a problem with \pkg{xtable} but with the formatting\r
583 of tables with fixed width columns and different alignments using\r
584 standard \LaTeX.\r
585 \r
586 One solution is to use the \verb|array| package, defining new\r
587 column formats.\r
588 \r
589 \begin{verbatim}\r
590 \newcolumntype{L}[1]{>{\raggedright\let\newline\\\r
591     \arraybackslash\hspace{0pt}}m{#1}}\r
592 \newcolumntype{C}[1]{>{\centering\let\newline\\\r
593     \arraybackslash\hspace{0pt}}m{#1}}\r
594 \newcolumntype{R}[1]{>{\raggedleft\let\newline\\\r
595     \arraybackslash\hspace{0pt}}m{#1}}\r
596 \newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}\r
597 \end{verbatim}\r
598 \r
599 These allow for very sophisticated cell formatting, namely\r
600 left-aligned, centred, or right-aligned text, with recognition of line\r
601 breaks for the first three new column types. If these lines are\r
602 included along with \verb|\usepackage{array}|, then the following is\r
603 possible.\r
604 \r
605 \newcolumntype{L}[1]{>{\raggedright\let\newline\\\r
606     \arraybackslash\hspace{0pt}}m{#1}}\r
607 \newcolumntype{C}[1]{>{\centering\let\newline\\\r
608     \arraybackslash\hspace{0pt}}m{#1}}\r
609 \newcolumntype{R}[1]{>{\raggedleft\let\newline\\\r
610     \arraybackslash\hspace{0pt}}m{#1}}\r
611 \newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}\r
612 \r
613 <<results='asis'>>=\r
614 df <- data.frame(name = c("A","B"), right = c(1.4, 34.6),\r
615                  left = c(1.4, 34.6), text = c("txt1","txt2"))\r
616 print(xtable(df, align = c("l", "|c", "|R{3cm}", "|L{3cm}", "| p{3cm}|")),\r
617       floating = FALSE, include.rownames = FALSE)\r
618 @\r
619 \r
620 \newpage\r
621 \r
622 \subsection{Table width}\r
623 The \code{tabularx} environment is for typesetting tables whose\r
624 overall width is fixed. The column alignment code \code{X} denotes\r
625 columns that will be stretched to achieve the desired table\r
626 width. Requires \verb|\usepackage{tabularx}| in the \LaTeX\ preamble.\r
627 \r
628 <<results='asis'>>=\r
629 df.width <- data.frame(One = c("item 1", "A"), Two = c("item 2", "B"),\r
630                        Three = c("item 3", "C"), Four = c("item 4", "D"))\r
631 x.width <- xtable(df.width)\r
632 align(x.width) <- "|l|X|l|l|l|"\r
633 print(x.width, tabular.environment = "tabularx", width = "\\textwidth")\r
634 @\r
635 \r
636 \section{Suppressing printing}\r
637 By default the \code{print} method will print the \LaTeX\ or HTML to\r
638 standard output and also return the character strings invisibly.  The\r
639 printing to standard output can be suppressed by specifying\r
640 \code{print.results = FALSE}.\r
641 \r
642 <<>>=\r
643 x.out <- print(tli.table, print.results = FALSE)\r
644 @\r
645 \r
646 Formatted output can also be captured without printing with the\r
647 \code{toLatex} method.  This function returns an object of class\r
648 \code{"Latex"}.\r
649 \r
650 <<>>=\r
651 x.ltx <- toLatex(tli.table)\r
652 class(x.ltx)\r
653 x.ltx\r
654 @\r
655 \r
656 \r
657 \newpage\r
658 \r
659 \section{Acknowledgements}\r
660 Most of the examples in this gallery are taken from the \pkg{xtable}\r
661 documentation. Two examples (\code{add.to.row} and `Aligning fixed width\r
662 columns') are from Stack Exchange.\r
663 \r
664 \section{Session information}\r
665 <<results='asis'>>=\r
666 toLatex(sessionInfo())\r
667 @\r
668 \r
669 \end{document}\r