From: dscott Date: Mon, 14 Dec 2015 02:46:39 +0000 (+0000) Subject: Added sanitization example to the gallery X-Git-Url: https://git.donarmstrong.com/?p=xtable.git;a=commitdiff_plain;h=69a844dcf4e59c44ebe09aba5a1cbb93596b7c3f Added sanitization example to the gallery git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@76 edb9625f-4e0d-4859-8d74-9fd3b1da38cb --- diff --git a/pkg/NEWS b/pkg/NEWS index a6025c4..2def7fb 100644 --- a/pkg/NEWS +++ b/pkg/NEWS @@ -1,6 +1,8 @@ 1.8-1 (NOT YET SUBMITTED TO CRAN) * added function print.xtableMatharray to enable easy creation of LaTeX code to enable an array to be included in a document. + * added example to the gallery using sanitizing headings and row + names to produce large bold headings and italic row names. 1.8-0 * autoformat, xalign, xdigits, xdisplay from Arni Magnusson, added diff --git a/pkg/vignettes/xtableGallery.Rnw b/pkg/vignettes/xtableGallery.Rnw index 4aa6d43..6b7d47b 100644 --- a/pkg/vignettes/xtableGallery.Rnw +++ b/pkg/vignettes/xtableGallery.Rnw @@ -7,7 +7,6 @@ %************************************************************************** \documentclass{article} \usepackage[a4paper,height=24cm]{geometry} % geometry first -\usepackage{listings} \usepackage{array} \usepackage{booktabs} \usepackage{longtable} @@ -21,13 +20,6 @@ \newcommand\code[1]{\texttt{#1}} \newcommand\pkg[1]{\textbf{#1}} \setcounter{tocdepth}{2} - -\lstset{language=R,basicstyle=\ttfamily, - keywordstyle=\ttfamily, - emphstyle=\itshape, - escapeinside = ||, - lineskip=0pt} - \begin{document} \title{The \pkg{xtable} Gallery} @@ -52,6 +44,7 @@ The first step is to load the package and set an option for this document. <>= library(xtable) options(xtable.floating = FALSE) +options(xtable.timestamp = "") @ \section{Gallery} @@ -129,36 +122,6 @@ temp.table <- xtable(temp.ts, digits = 0) temp.table @ -\subsection{Mathematical arrays} -\label{sec:mathematical-arrays} - -Mathematical arrays (as compared to data arrays) can be printed -correctly by declaring the class as \code{xtableMatharray}. -<>= -V <- matrix(c(1.140380e-03, 3.010497e-05, 7.334879e-05, - 3.010497e-05, 3.320683e-04, -5.284854e-05, - 7.334879e-05, -5.284854e-05, 3.520928e-04), nrow = 3) -V -class(V) <- c("xtableMatharray") -class(V) -xtbl <- print(xtable(V, display = rep("E", 4))) -@ %def - -Then \code{xtbl} can be used in an expression such as -$V=\left[\Sexpr{xtbl}\right]$, produced by -\begin{lstlisting} -V=\left[\Sexp||r{xtbl}\right]. -\end{lstlisting} - - -An alternative display can be created using the \texttt{digits} -argument. If \code{xtbl} is defined instead according to - -<>= -xtbl <- print(xtable(V, digits = 6)) -@ %def -then we get\\ -$V=\left[\Sexpr{paste(print(xtable(V, digits = 6)))}\right].$ \newpage <>= @@ -240,6 +203,7 @@ xtable(insane) \p Sometimes you might want to have your own sanitization function. + <>= wanttex <- xtable(data.frame(Column = paste("Value_is $10^{-",1:3,"}$", sep = ""))) @@ -247,6 +211,27 @@ print(wanttex, sanitize.text.function = function(str) gsub("_", "\\_", str, fixed = TRUE)) @ +\p +Sanitization can be useful in formatting column headings and row names: + +<>= +dat <- mtcars[1:3, 1:6] +large <- function(x){ + paste0('{\\Large{\\bfseries ', x, '}}') +} +italic <- function(x){ + paste0('{\\emph{ ', x, '}}') +} +@ %def + +<>= +print(xtable(dat), + sanitize.rownames.function = italic, + sanitize.colnames.function = large, + booktabs = TRUE) +@ %def + + \newpage @@ -276,8 +261,6 @@ money <- matrix(c("$1,000","$900","$100"), ncol = 3, print(xtable(money), sanitize.rownames.function = function(x) {x}) @ - - \section{Format examples} \subsection{Adding a centering environment} <>= @@ -332,27 +315,6 @@ digits(tli.table) <- matrix(0:4, nrow = 10, ncol = ncol(tli)+1) tli.table @ -\subsection{Minus signs} -\label{sec:minus-signs} - -The standard print functions do not render minus signs properly. They -are actually hyphens (-) rather than minus signs ($-$). True minus -signs can be obtained using the \code{math.style.negative} argument. - -\p -Here is the \LaTeX{} which is produced: - -<<>>= -pr1 <- prcomp(USArrests) -print(xtable(pr1), math.style.negative = TRUE) -@ - -And here is the resulting table. -<>= -print(xtable(pr1), math.style.negative = TRUE) -@ - - \newpage \subsection{Suppress row/column names}