]> git.donarmstrong.com Git - xtable.git/commitdiff
Support math.style.exponents math_style_exponents
authorDon Armstrong <don@donarmstrong.com>
Wed, 23 Dec 2015 00:39:32 +0000 (16:39 -0800)
committerDon Armstrong <don@donarmstrong.com>
Wed, 23 Dec 2015 00:39:32 +0000 (16:39 -0800)
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⁵)

pkg/R/print.xtable.R
pkg/man/print.xtable.Rd
pkg/vignettes/xtableGallery.Rnw

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