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