toLatex.numeric.RdFormats real numbers, possibly in scientific notation, with a given number of digits after the decimal point. Output can be used in LaTeX math mode, e.g., for printing numbers in a table, where each number has to be printed with the same number of digits after the decimal point, even if the last digits are zeros.
# S3 method for class 'numeric'
toLatex(object, digits = format.info(object)[2],
scientific = format.info(object)[3] > 0, times = "\\cdot", ...)a numeric vector.
number of digits after the decimal point (for the
mantissa if scientific). The default behaves the same as R's
format().
logical indicating if scientific notation a *
10^k should be used. The default behaves the same as R's
format().
character string indicating the LaTeX symbol to be used for the ‘times’ sign.
unused; for compatibility with toLatex.
We use digits for round, i.e., round after
the decimal point on purpose, rather than signif()icant
digit rounding as used by print() or
format().
a character vector of the same length as object,
containing the formatted numbers.
pretty10exp which gives expressions
similar to our scientific=TRUE.
toLatex with other methods.
xx <- pi * 10^(-9:9)
format(xx)
#> [1] "3.14e-09" "3.14e-08" "3.14e-07" "3.14e-06" "3.14e-05" "3.14e-04"
#> [7] "3.14e-03" "3.14e-02" "3.14e-01" "3.14e+00" "3.14e+01" "3.14e+02"
#> [13] "3.14e+03" "3.14e+04" "3.14e+05" "3.14e+06" "3.14e+07" "3.14e+08"
#> [19] "3.14e+09"
formatC(xx)
#> [1] "3.142e-09" "3.142e-08" "3.142e-07" "3.142e-06" "3.142e-05" "0.0003142"
#> [7] "0.003142" "0.03142" "0.3142" "3.142" "31.42" "314.2"
#> [13] "3142" "3.142e+04" "3.142e+05" "3.142e+06" "3.142e+07" "3.142e+08"
#> [19] "3.142e+09"
toLatex(xx) #-> scientific = TRUE is chosen
#> [1] "3.14 \\cdot 10^{-9}" "3.14 \\cdot 10^{-8}" "3.14 \\cdot 10^{-7}"
#> [4] "3.14 \\cdot 10^{-6}" "3.14 \\cdot 10^{-5}" "3.14 \\cdot 10^{-4}"
#> [7] "3.14 \\cdot 10^{-3}" "3.14 \\cdot 10^{-2}" "3.14 \\cdot 10^{-1}"
#> [10] "3.14 \\cdot 10^{0}" "3.14 \\cdot 10^{1}" "3.14 \\cdot 10^{2}"
#> [13] "3.14 \\cdot 10^{3}" "3.14 \\cdot 10^{4}" "3.14 \\cdot 10^{5}"
#> [16] "3.14 \\cdot 10^{6}" "3.14 \\cdot 10^{7}" "3.14 \\cdot 10^{8}"
#> [19] "3.14 \\cdot 10^{9}"
toLatex(xx, scientific=FALSE)
#> [1] "0.00" "0.00" "0.00" "0.00"
#> [5] "0.00" "0.00" "0.00" "0.03"
#> [9] "0.31" "3.14" "31.42" "314.16"
#> [13] "3141.59" "31415.93" "314159.27" "3141592.65"
#> [17] "31415926.54" "314159265.36" "3141592653.59"
sapply(xx, toLatex)
#> [1] "3.14 \\cdot 10^{-9}" "3.14 \\cdot 10^{-8}" "3.14 \\cdot 10^{-7}"
#> [4] "3.14 \\cdot 10^{-6}" "3.14 \\cdot 10^{-5}" "0.000314"
#> [7] "0.00314" "0.0314" "0.314"
#> [10] "3.14" "31.4" "314"
#> [13] "3142" "31416" "314159"
#> [16] "3141593" "31415927" "3.14 \\cdot 10^{8}"
#> [19] "3.14 \\cdot 10^{9}"
sapply(xx, toLatex, digits = 2)
#> [1] "3.14 \\cdot 10^{-9}" "3.14 \\cdot 10^{-8}" "3.14 \\cdot 10^{-7}"
#> [4] "3.14 \\cdot 10^{-6}" "3.14 \\cdot 10^{-5}" "0.00"
#> [7] "0.00" "0.03" "0.31"
#> [10] "3.14" "31.42" "314.16"
#> [13] "3141.59" "31415.93" "314159.27"
#> [16] "3141592.65" "31415926.54" "3.14 \\cdot 10^{8}"
#> [19] "3.14 \\cdot 10^{9}"