From: Don Armstrong <don@donarmstrong.com>
Date: Wed, 23 Dec 2015 00:39:32 +0000 (-0800)
Subject: Support math.style.exponents
X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=refs%2Fheads%2Fmath_style_exponents;p=xtable.git

Support math.style.exponents

this option to print.xtable supports the values TRUE or $$ which makes
1e5 like $1\times 10^{5}$ or a value of "ensuremath" which uses
\ensuremath instead of $$ or a value of "UTF-8" or "UTF8", which uses
UTF-8 to approximate the LaTeX typesetting (e.g., 1×10⁵)
---

diff --git a/pkg/R/print.xtable.R b/pkg/R/print.xtable.R
index ce942c7..75bf09e 100644
--- a/pkg/R/print.xtable.R
+++ b/pkg/R/print.xtable.R
@@ -396,10 +396,30 @@ print.xtable <- function(x,
                     result[i] <- gsub("-", "$-$", result[i], fixed = TRUE)
                 }
             }
-            if ( math.style.exponents ) {
+            if (is.logical(math.style.exponents) && ! math.style.exponents ) {
+            } else if (is.logical(math.style.exponents) && math.style.exponents ||
+                       math.style.exponents == "$$"
+                       ) {
                 for(i in 1:length(x)) {
-                     result[i] <- gsub("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
-                                       "\\\\ensuremath{\\1\\2 \\\\times 10^{\\3\\4}}", result[i])
+                    result[i] <- gsub("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
+                                      "$\\1\\2 \\\\times 10^{\\3\\4}$", result[i])
+                }
+            } else if (math.style.exponents == "ensuremath") {
+                for(i in 1:length(x)) {
+                    result[i] <- gsub("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
+                                      "\\\\ensuremath{\\1\\2 \\\\times 10^{\\3\\4}}", result[i])
+                }
+            } else if (math.style.exponents == "UTF8" ||
+                       math.style.exponents == "UTF-8") {
+                for(i in 1:length(x)) {
+                    ## this code turns 1e5 into 1×10⁵x
+                    if (all(grepl("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",result[i]))) {
+                        temp <- strsplit(result[i],"eE",result[i])
+                        result[i] <-
+                            paste0(temp[1],
+                                   "\u00d710",
+                                   chartr("-1234567890","\u207b\u00b9\u00b2\u00b3\u2074\u2075\u20746\u20747\u20748\u20749\u2070",temp[2]))
+                    }
                 }
             }
             return(result)
diff --git a/pkg/man/print.xtable.Rd b/pkg/man/print.xtable.Rd
index d4cccb0..4120599 100644
--- a/pkg/man/print.xtable.Rd
+++ b/pkg/man/print.xtable.Rd
@@ -30,6 +30,7 @@
   sanitize.colnames.function = getOption("xtable.sanitize.colnames.function",
                                          sanitize.text.function),
   math.style.negative = getOption("xtable.math.style.negative", FALSE),
+  math.style.exponents = getOption("xtable.math.style.exponents", FALSE),
   html.table.attributes = getOption("xtable.html.table.attributes",
                                     "border=1"),
   print.results = getOption("xtable.print.results", TRUE),
@@ -138,6 +139,12 @@
   \item{math.style.negative}{In a LaTeX table, if \code{TRUE}, then use
     $-$ for the negative sign (as was the behavior prior to version 1.5-3).
     Default value is \code{FALSE}.}
+  \item{math.style.exponents}{In a LaTeX table, if \code{TRUE} or
+    \code{"$$"}, then use \code{$5 \times 10^{5}$} for 5e5. If
+    \code{"ensuremath"}, then use \code{\ensuremath{5 \times 10^{5}}}
+    for 5e5. If \code{"UTF-8"} or \code{"UTF-8"}, then use UTF-8 to
+    approximate the LaTeX typsetting for 5e5.
+    Default value is \code{FALSE}.}
   \item{html.table.attributes}{In an HTML table, attributes associated
     with the \code{<TABLE>} tag.
     Default value is \code{"border=1"}.}
diff --git a/pkg/vignettes/xtableGallery.Rnw b/pkg/vignettes/xtableGallery.Rnw
index 8491e12..a3897ca 100644
--- a/pkg/vignettes/xtableGallery.Rnw
+++ b/pkg/vignettes/xtableGallery.Rnw
@@ -187,6 +187,24 @@ autoformat(x)
 
 \newpage
 
+\subsection{Math-Style Exponents}
+If you prefer $5 \times 10^5$ in your tables to 5e5, the
+\code{math.style.exponents} option to \code{print.xtable} is useful:
+
+<<results='asis'>>=
+print(xtable(data.frame(text=c("foo","bar"),
+                        googols=c(10e10,50e10),
+                        small=c(8e-24,7e-5),
+                        row.names=c("A","B")),
+             display=c("s","s","g","g")),
+      math.style.exponents=TRUE
+      )
+@ 
+
+this option also supports the values \code{ensuremath} which uses
+\code{\char`\\ensuremath} instead of \code{\$\$} and \code{UTF-8}
+which uses UTF-8 to approximate the \LaTeX typesetting.
+
 \section{Sanitization}
 <<results='asis'>>=
 insane <- data.frame(Name = c("Ampersand","Greater than","Less than",