Chart by Jeff Cruzan is licensed under CC BY-NC-SA
3.0
The notation we use to represent large and small numbers depends on
the context of our communications. In a computer script, for example, we
might encode Avogadro’s number as 6.0221*10^23
. A computer
printout of this number would typically use E-notation, as in
6.0221E+23
.
In professional technical communications, however, Avogadro’s number
would be typeset using power of ten notation, \(\small 6.0221 \times 10^{23}\). Computer
syntax is generally avoided—the asterisk (*
) and carat
(^
) in 6.0221*10^23
communicate
instructions to a computer, not syntactical
mathematics. And while scientific E-notation
(6.0221E+23
) has currency in some discourse communities,
the general convention in technical communications is to format large
and small numbers using powers-of-ten notation of the form,
\[ \small a \times 10^n, \]
The \(\times\) multiplication symbol, often avoided in other contexts, is conventional syntax in powers-of-ten notation. Also, the notation has two forms in general use: scientific and engineering (Chase 2021, 63–67).
Scientific. \(\small n\) is an integer and \(\small a \in Re: 1\leq{|a|}<10\). For example, \(\small 6.022 \times 10^{23}\)
Engineering. \(\small n\) is a multiple of 3 and \(\small a \in Re: 1\leq{|a|}<1000\). For example, \(\small 602.2 \times 10^{21}\)
Exceptions. When exponents are in the neighborhood of zero, for example, \(\small n \in \{-1, 0, 1, 2\}\), decimal notation may be preferred to power of ten notation. Decimal values such as \(\small 0.1234\), \(\small 1.234\), \(\small 12.34\), and \(\small 123.4\) might be printed as-is. The range of exponents to include in this set is discretionary.
Notes on syntax. We have to distinguish between computer syntax, math syntax, and markup syntax.
Anyone who has written a line of code involving multiplication is
familiar with the asterisk as the multiplication operator, e.g.,
x = a * b
or y = a * (b + c)
. In written
mathematics, however, these expressions are written without
multiplication symbols, that is, \(\small
x=ab\) and \(\small y=a(b+c)\).
Moreover, the asterisk has a completely different meaning in
mathematics, representing the convolution operation between two
functions, as in \(\small \left(f * g
\right)(t)\).
The carat symbol (^
), to the best of my knowledge,
is a programming operator only; mathematically it has no
meaning.
In our markup language, \times
is the product symbol
and ^
indicates a superscript or exponent.
In R Markdown and Quarto Markdown, we use an inline equation markup
delimited by $ ... $
to create a math expression in the
output document (\( ... \)
is also an option). For example,
the markup for Avogadro’s number is given by,
$6.0221 \times 10^{23}$
rendered in an R markdown document as \(\small 6.0221 \times 10^{23}\). To program the markup, however, we enclose the markup as a character string, that is,
"$6.0221 \\times 10^{23}$"
which requires the backward slash \
to be “escaped”,
hence the product symbol is \\times
. This is the form of
the output produced by format_power()
.
format_power()
Given a number, a numerical vector, or a numerical column from a data
frame, format_power()
converts the numbers to character
strings of the form,
"$a \\times 10^{n}$"
where a
is the coefficient and n
is the
exponent. The user can specify the number of significant digits and
scientific or engineering format. Unless otherwise specified, numbers
with exponents in the neighborhood of zero are excluded from power of
ten notation and are converted to character strings of the form,
"$a$"
where a
is the number in decimal notation to the
specified number of significant digits.
Arguments.
x Numerical vector to be formatted. Can be a scalar, a vector, or a column from a data frame.
digits Positive nonzero integer to specify the number of significant digits. Default is 4.
format Possible values are “engr” (default)
for engineering notation and “sci” for scientific notation. Can also be
set as a global option, for example,
options(formatdown.power.format = "sci")
, that can be
overwritten in an individual function call.
size Font size. Possible values are
“scriptsize”, “small” (default), “normalsize”, “large”, or “huge”. Can
also be set as a global option, for example,
options(formatdown.font.size = "normalsize")
, that can be
overwritten in an individual function call.
omit_power Numeric vector of length two (or
NULL). Determines the range of exponents between which power-of-ten
notation is excluded. Default is c(-1, 2)
.
set_power Integer assigned as the fixed power-of-ten, overriding scientific or engineering formatting. Default NULL.
delim Character vector of length one or two
defining the math markup delimiters. Possible values include
"$"
or "\\("
, both of which create appropriate
left and right delimiters. Custom delimiters can be assigned if required
by one’s TeX processor.
If you are writing your own script to follow along, we use these packages in this vignette:
Equivalent usage. The first two arguments do not have to be named if the argument order is maintained.
# Numerical value
avogadro <- 6.0221E+23
# Arguments named
(x <- format_power(x = avogadro, digits = 4))
#> [1] "$\\small 602.2 \\times 10^{21}$"
# Arguments unnamed
y <- format_power(avogadro, 4)
# Implicit use of default argument
z <- format_power(avogadro)
# Demonstrate equivalence
all.equal(x, y)
#> [1] TRUE
all.equal(x, z)
#> [1] TRUE
Use with inline R code.
which, in an .Rmd
or .qmd
document, is
rendered as \(\small 602.2 \times
10^{21}\).
x <- c(
1.2222e-6, 2.3333e-5, 3.4444e-4, 4.1111e-3, 5.2222e-2, 6.3333e-1,
7.4444e+0, 8.1111e+1, 9.2222e+2, 1.3333e+3, 2.4444e+4, 3.1111e+5, 4.2222e+6
)
format_power(x)
#> [1] "$\\small 1.222 \\times 10^{-6}$" "$\\small 23.33 \\times 10^{-6}$"
#> [3] "$\\small 344.4 \\times 10^{-6}$" "$\\small 4.111 \\times 10^{-3}$"
#> [5] "$\\small 52.22 \\times 10^{-3}$" "$\\small 0.6333$"
#> [7] "$\\small 7.444$" "$\\small 81.11$"
#> [9] "$\\small 922.2$" "$\\small 1.333 \\times 10^{3}$"
#> [11] "$\\small 24.44 \\times 10^{3}$" "$\\small 311.1 \\times 10^{3}$"
#> [13] "$\\small 4.222 \\times 10^{6}$"
is rendered as \(\small 1.222 \times 10^{-6}\), \(\small 23.33 \times 10^{-6}\), \(\small 344.4 \times 10^{-6}\), \(\small 4.111 \times 10^{-3}\), etc.
Argument does not have to be named.
format_power(x[1], 3)
#> [1] "$\\small 1.22 \\times 10^{-6}$"
format_power(x[1], 4)
#> [1] "$\\small 1.222 \\times 10^{-6}$"
are rendered as
format
argument default value is “engr”. If included,
argument must be named.
format_power(x[3])
#> [1] "$\\small 344.4 \\times 10^{-6}$"
format_power(x[3], format = "sci")
#> [1] "$\\small 3.444 \\times 10^{-4}$"
are rendered as
To compare the effects across many orders of magnitude, we format the
example vector twice, placing the results side by side in a data frame
for comparison, rendered using knitr::kable()
,
# Compare two formats
DT <- data.table(
scientific = format_power(x, 3, format = "sci"),
engineering = format_power(x, 3)
)
knitr::kable(DT, align = "r", col.names = c("scientific notation", "engineering notation"))
scientific notation | engineering notation |
---|---|
\(\small 1.22 \times 10^{-6}\) | \(\small 1.22 \times 10^{-6}\) |
\(\small 2.33 \times 10^{-5}\) | \(\small 23.3 \times 10^{-6}\) |
\(\small 3.44 \times 10^{-4}\) | \(\small 344 \times 10^{-6}\) |
\(\small 4.11 \times 10^{-3}\) | \(\small 4.11 \times 10^{-3}\) |
\(\small 5.22 \times 10^{-2}\) | \(\small 52.2 \times 10^{-3}\) |
\(\small 0.633\) | \(\small 0.633\) |
\(\small 7.44\) | \(\small 7.44\) |
\(\small 81.1\) | \(\small 81.1\) |
\(\small 922\) | \(\small 922\) |
\(\small 1.33 \times 10^{3}\) | \(\small 1.33 \times 10^{3}\) |
\(\small 2.44 \times 10^{4}\) | \(\small 24.4 \times 10^{3}\) |
\(\small 3.11 \times 10^{5}\) | \(\small 311 \times 10^{3}\) |
\(\small 4.22 \times 10^{6}\) | \(\small 4.22 \times 10^{6}\) |
The values displayed without powers-of-ten notation are in the range
set by the omit_power
argument, described in the next
section.
Format the same column of text using each of the five possible
size
arguments for comparison.
x <- c(1.2E-6, 3.4E+0, 5.6E+13)
# Compare formats
DT <- data.table(
scriptsize = format_power(x, 2, size = "scriptsize"),
small = format_power(x, 2, size = "small"),
normalsize = format_power(x, 2, size = "normalsize"),
large = format_power(x, 2, size = "large"),
huge = format_power(x, 2, size = "huge")
)
knitr::kable(DT, align = "r")
scriptsize | small | normalsize | large | huge |
---|---|---|---|---|
\(\scriptsize 1.2 \times 10^{-6}\) | \(\small 1.2 \times 10^{-6}\) | \(\normalsize 1.2 \times 10^{-6}\) | \(\large 1.2 \times 10^{-6}\) | \(\huge 1.2 \times 10^{-6}\) |
\(\scriptsize 3.4\) | \(\small 3.4\) | \(\normalsize 3.4\) | \(\large 3.4\) | \(\huge 3.4\) |
\(\scriptsize 56 \times 10^{12}\) | \(\small 56 \times 10^{12}\) | \(\normalsize 56 \times 10^{12}\) | \(\large 56 \times 10^{12}\) | \(\huge 56 \times 10^{12}\) |
omit_power
argument, if included, must be named.
Suppose, for example, we want the value 0.0678 to be rendered without
powers-of-ten notation. The exponent for this number in scientific
notation is \(-2\), as in \(\small 6.780 \times 10^{-2}\). If this
exponent is within the range set by omit_power
, the number
is rendered without power-of-ten notation (overriding the
format
argument).
format_power(0.0678, 3, format = "sci", omit_power = c(-2, 2))
#> [1] "$\\small 0.0678$"
format_power(0.0678, 3, format = "sci", omit_power = c(-1, 2))
#> [1] "$\\small 6.78 \\times 10^{-2}$"
are rendered as
At the upper limit,
format_power(6789, 3, format = "sci", omit_power = c(-1, 2))
#> [1] "$\\small 6.79 \\times 10^{3}$"
format_power(6789, 3, format = "sci", omit_power = c(-1, 3))
#> [1] "$\\small 6790$"
are rendered as
omit_power = NULL
removes the exceptions and formats all
numbers in power-of-ten notation.
# Omit no values from power-of-ten notation
DT <- data.table(
scientific = format_power(x, 3, format = "sci", omit_power = NULL),
engineering = format_power(x, 3, omit_power = NULL)
)
knitr::kable(DT, align = "r", col.names = c("scientific notation", "engineering notation"))
scientific notation | engineering notation |
---|---|
\(\small 1.20 \times 10^{-6}\) | \(\small 1.20 \times 10^{-6}\) |
\(\small 3.40 \times 10^{0}\) | \(\small 3.40 \times 10^{0}\) |
\(\small 5.60 \times 10^{13}\) | \(\small 56.0 \times 10^{12}\) |
Suppose you wish to omit one specific power of ten only, for example,
let omit_power = c(-5, -5)
.
x <- c(3.2e-7, 4.5e-6, 4.5e-5, 6.7e-4, 3, 37800)
format_power(x, 3, format = "sci", omit_power = c(-5, -5))
#> [1] "$\\small 3.20 \\times 10^{-7}$" "$\\small 4.50 \\times 10^{-6}$"
#> [3] "$\\small 0.0000450$" "$\\small 6.70 \\times 10^{-4}$"
#> [5] "$\\small 3.00 \\times 10^{0}$" "$\\small 3.78 \\times 10^{4}$"
format_power(x, 3, format = "engr", omit_power = c(-5, -5))
#> [1] "$\\small 320 \\times 10^{-9}$" "$\\small 4.50 \\times 10^{-6}$"
#> [3] "$\\small 0.0000450$" "$\\small 670 \\times 10^{-6}$"
#> [5] "$\\small 3.00 \\times 10^{0}$" "$\\small 37.8 \\times 10^{3}$"
All the values except 4.5e-5
are formatted as powers of
ten.
set_power
argument, if included, must be named.
When values in a table column span a few orders of magnitude, an
audience is often better served by setting the notation to a constant
power of ten. In the example below, we show the tabulated values of the
viscosity of water as a function of temperature (from the
water
data set included with formatdown
).
# Copy to avoid by-reference changes
DT <- copy(water)
# Convert temperature from K to C
DT <- DT[, .(visc)]
# Create two columns to compare
DT[, ver1 := format_power(visc, 3)]
DT[, ver2 := format_power(visc, 3, set_power = -3)]
The first column is unformatted. The second column, in engineering format, includes exponents of \(\small -3\) and \(\small -6\). In the third column, we set the exponents to \(\small -3\) throughout.
knitr::kable(DT, align = "r", col.names = c(
"$\\small\\mu$ [Pa s]", "engr notation", "set power"
))
\(\small\mu\) [Pa s] | engr notation | set power |
---|---|---|
0.0017336 | \(\small 1.73 \times 10^{-3}\) | \(\small 1.73 \times 10^{-3}\) |
0.0013105 | \(\small 1.31 \times 10^{-3}\) | \(\small 1.31 \times 10^{-3}\) |
0.0010212 | \(\small 1.02 \times 10^{-3}\) | \(\small 1.02 \times 10^{-3}\) |
0.0008174 | \(\small 817 \times 10^{-6}\) | \(\small 0.817 \times 10^{-3}\) |
0.0006699 | \(\small 670 \times 10^{-6}\) | \(\small 0.670 \times 10^{-3}\) |
0.0005605 | \(\small 560 \times 10^{-6}\) | \(\small 0.560 \times 10^{-3}\) |
0.0004776 | \(\small 478 \times 10^{-6}\) | \(\small 0.478 \times 10^{-3}\) |
0.0004135 | \(\small 414 \times 10^{-6}\) | \(\small 0.414 \times 10^{-3}\) |
0.0003631 | \(\small 363 \times 10^{-6}\) | \(\small 0.363 \times 10^{-3}\) |
0.0003229 | \(\small 323 \times 10^{-6}\) | \(\small 0.323 \times 10^{-3}\) |
0.0002902 | \(\small 290 \times 10^{-6}\) | \(\small 0.290 \times 10^{-3}\) |
In the next example, we have density (that decreases with altitude) with additional columns of density values formatted three ways.
DT <- copy(atmos)
DT <- DT[, .(dens)]
DT[, ver1 := format_power(dens, 3)]
DT[, ver2 := format_power(dens, 3, set_power = -3)]
DT[, ver3 := format_power(dens, 3, set_power = -3, omit_power = NULL)]
kable(DT, align = "r", col.names = c(
"$\\small\\rho$ [kg/m$^3$]",
"engr notation",
"set power",
"set power, omit none"
))
\(\small\rho\) [kg/m\(^3\)] | engr notation | set power | set power, omit none |
---|---|---|---|
1.23e+00 | \(\small 1.23\) | \(\small 1.23\) | \(\small 1230 \times 10^{-3}\) |
4.14e-01 | \(\small 0.414\) | \(\small 0.414\) | \(\small 414 \times 10^{-3}\) |
8.89e-02 | \(\small 88.9 \times 10^{-3}\) | \(\small 88.9 \times 10^{-3}\) | \(\small 88.9 \times 10^{-3}\) |
1.84e-02 | \(\small 18.4 \times 10^{-3}\) | \(\small 18.4 \times 10^{-3}\) | \(\small 18.4 \times 10^{-3}\) |
4.00e-03 | \(\small 4.00 \times 10^{-3}\) | \(\small 4.00 \times 10^{-3}\) | \(\small 4.00 \times 10^{-3}\) |
1.03e-03 | \(\small 1.03 \times 10^{-3}\) | \(\small 1.03 \times 10^{-3}\) | \(\small 1.03 \times 10^{-3}\) |
3.10e-04 | \(\small 310 \times 10^{-6}\) | \(\small 0.310 \times 10^{-3}\) | \(\small 0.310 \times 10^{-3}\) |
8.28e-05 | \(\small 82.8 \times 10^{-6}\) | \(\small 0.0828 \times 10^{-3}\) | \(\small 0.0828 \times 10^{-3}\) |
1.85e-05 | \(\small 18.5 \times 10^{-6}\) | \(\small 0.0185 \times 10^{-3}\) | \(\small 0.0185 \times 10^{-3}\) |
delim
argument, if included, must be named.
# Equivalent usage
w <- format_power(x[1])
y <- format_power(x[1], delim = "$")
z <- format_power(x[1], delim = c("$", "$"))
all.equal(w, y)
#> [1] TRUE
all.equal(w, z)
#> [1] TRUE
all of which render as
I encountered one instance in which the $ ... $
delimiters did not produce the desired output—using
kableExtra::kbl()
to create a table in a .qmd
output document. Switching the delimiters to \\( ... \\)
fixed the problem, per the recommendation of the MathJax consortium
(Cervone, 2018).
format_power(x[1], delim = "\\(")
#> [1] "\\(\\small 320.0 \\times 10^{-9}\\)"
format_power(x[1], delim = c("\\(", "\\)"))
#> [1] "\\(\\small 320.0 \\times 10^{-9}\\)"
are both rendered as
Using air_meas
, a data frame included with
formatdown
that contains columns of class Date, character,
factor, numeric, and integer.
# Included with formatdown
air_meas
#> date trial humid temp pres sp_gas dens
#> <Date> <char> <fctr> <num> <num> <int> <num>
#> 1: 2018-06-12 a low 294.1 101100 287 1.198
#> 2: 2018-06-13 b high 294.1 101000 287 1.196
#> 3: 2018-06-14 c med 294.6 101100 287 1.196
#> 4: 2018-06-15 d low 293.4 101000 287 1.200
#> 5: 2018-06-16 e high 293.9 101100 287 1.199
# Render in document
knitr::kable(air_meas, align = "r")
date | trial | humid | temp | pres | sp_gas | dens |
---|---|---|---|---|---|---|
2018-06-12 | a | low | 294.1 | 101100 | 287 | 1.198 |
2018-06-13 | b | high | 294.1 | 101000 | 287 | 1.196 |
2018-06-14 | c | med | 294.6 | 101100 | 287 | 1.196 |
2018-06-15 | d | low | 293.4 | 101000 | 287 | 1.200 |
2018-06-16 | e | high | 293.9 | 101100 | 287 | 1.199 |
Treating a column as a vector,
# Copy to avoid "by reference" changes to air_meas
DT <- copy(air_meas)
# Format as a vector
format_power(DT$pres, digits = 4)
#> [1] "$\\small 101.1 \\times 10^{3}$" "$\\small 101.0 \\times 10^{3}$"
#> [3] "$\\small 101.1 \\times 10^{3}$" "$\\small 101.0 \\times 10^{3}$"
#> [5] "$\\small 101.1 \\times 10^{3}$"
is rendered as \(\small 101.1 \times 10^{3}\), \(\small 101.0 \times 10^{3}\), \(\small 101.1 \times 10^{3}\), \(\small 101.0 \times 10^{3}\), \(\small 101.1 \times 10^{3}\).
Treating a column within a data frame,
# Copy to avoid "by reference" changes to air_meas
DT <- copy(air_meas)
# Format one column, retain all columns
DT$pres <- format_power(DT$pres, digits = 4)
DT[]
#> date trial humid temp pres sp_gas dens
#> <Date> <char> <fctr> <num> <char> <int> <num>
#> 1: 2018-06-12 a low 294.1 $\\small 101.1 \\times 10^{3}$ 287 1.198
#> 2: 2018-06-13 b high 294.1 $\\small 101.0 \\times 10^{3}$ 287 1.196
#> 3: 2018-06-14 c med 294.6 $\\small 101.1 \\times 10^{3}$ 287 1.196
#> 4: 2018-06-15 d low 293.4 $\\small 101.0 \\times 10^{3}$ 287 1.200
#> 5: 2018-06-16 e high 293.9 $\\small 101.1 \\times 10^{3}$ 287 1.199
# Render in document
knitr::kable(DT, align = "r")
date | trial | humid | temp | pres | sp_gas | dens |
---|---|---|---|---|---|---|
2018-06-12 | a | low | 294.1 | \(\small 101.1 \times 10^{3}\) | 287 | 1.198 |
2018-06-13 | b | high | 294.1 | \(\small 101.0 \times 10^{3}\) | 287 | 1.196 |
2018-06-14 | c | med | 294.6 | \(\small 101.1 \times 10^{3}\) | 287 | 1.196 |
2018-06-15 | d | low | 293.4 | \(\small 101.0 \times 10^{3}\) | 287 | 1.200 |
2018-06-16 | e | high | 293.9 | \(\small 101.1 \times 10^{3}\) | 287 | 1.199 |
Using lapply()
to select and treat multiple columns from
a data frame,
# Copy to avoid "by reference" changes to air_meas
DT <- copy(air_meas)
# Identify columns to format
cols_we_want <- c("temp", "pres", "dens")
# Select and format.
DT <- DT[, lapply(.SD, function(x) format_power(x, 4)), .SDcols = cols_we_want]
DT[]
#> temp pres dens
#> <char> <char> <char>
#> 1: $\\small 294.1$ $\\small 101.1 \\times 10^{3}$ $\\small 1.198$
#> 2: $\\small 294.1$ $\\small 101.0 \\times 10^{3}$ $\\small 1.196$
#> 3: $\\small 294.6$ $\\small 101.1 \\times 10^{3}$ $\\small 1.196$
#> 4: $\\small 293.4$ $\\small 101.0 \\times 10^{3}$ $\\small 1.200$
#> 5: $\\small 293.9$ $\\small 101.1 \\times 10^{3}$ $\\small 1.199$
# Render in document
knitr::kable(DT, align = "r")
temp | pres | dens |
---|---|---|
\(\small 294.1\) | \(\small 101.1 \times 10^{3}\) | \(\small 1.198\) |
\(\small 294.1\) | \(\small 101.0 \times 10^{3}\) | \(\small 1.196\) |
\(\small 294.6\) | \(\small 101.1 \times 10^{3}\) | \(\small 1.196\) |
\(\small 293.4\) | \(\small 101.0 \times 10^{3}\) | \(\small 1.200\) |
\(\small 293.9\) | \(\small 101.1 \times 10^{3}\) | \(\small 1.199\) |
Repeat, but retain all columns,
# Copy to avoid "by reference" changes to air_meas
DT <- copy(air_meas)
# Identify columns to format
cols_we_want <- c("temp", "pres", "dens")
# Format selected columns, retain all columns
DT <- DT[, (cols_we_want) := lapply(.SD, function(x) format_power(x, 4)), .SDcols = cols_we_want]
# Treat the gas constant with 3 digits
DT$sp_gas <- format_power(DT$sp_gas, digits = 3)
DT[]
#> date trial humid temp pres
#> <Date> <char> <fctr> <char> <char>
#> 1: 2018-06-12 a low $\\small 294.1$ $\\small 101.1 \\times 10^{3}$
#> 2: 2018-06-13 b high $\\small 294.1$ $\\small 101.0 \\times 10^{3}$
#> 3: 2018-06-14 c med $\\small 294.6$ $\\small 101.1 \\times 10^{3}$
#> 4: 2018-06-15 d low $\\small 293.4$ $\\small 101.0 \\times 10^{3}$
#> 5: 2018-06-16 e high $\\small 293.9$ $\\small 101.1 \\times 10^{3}$
#> sp_gas dens
#> <char> <char>
#> 1: $\\small 287$ $\\small 1.198$
#> 2: $\\small 287$ $\\small 1.196$
#> 3: $\\small 287$ $\\small 1.196$
#> 4: $\\small 287$ $\\small 1.200$
#> 5: $\\small 287$ $\\small 1.199$
# Render in document
knitr::kable(DT, align = "r")
date | trial | humid | temp | pres | sp_gas | dens |
---|---|---|---|---|---|---|
2018-06-12 | a | low | \(\small 294.1\) | \(\small 101.1 \times 10^{3}\) | \(\small 287\) | \(\small 1.198\) |
2018-06-13 | b | high | \(\small 294.1\) | \(\small 101.0 \times 10^{3}\) | \(\small 287\) | \(\small 1.196\) |
2018-06-14 | c | med | \(\small 294.6\) | \(\small 101.1 \times 10^{3}\) | \(\small 287\) | \(\small 1.196\) |
2018-06-15 | d | low | \(\small 293.4\) | \(\small 101.0 \times 10^{3}\) | \(\small 287\) | \(\small 1.200\) |
2018-06-16 | e | high | \(\small 293.9\) | \(\small 101.1 \times 10^{3}\) | \(\small 287\) | \(\small 1.199\) |
The options()
function can be used to set global values
for two argument in format_power()
:
format
(default is “engr”)size
(default is “small”)For example,
# Assign arguments to be used from this point forward in the script
options(formatdown.power.format = "sci")
options(formatdown.font.size = "scriptsize")
# Copy to avoid "by reference" changes to air_meas
DT <- copy(air_meas)
# Format selected columns to 4 digits
cols_we_want <- c("temp", "pres", "dens")
DT <- DT[, (cols_we_want) := lapply(.SD, function(x) format_power(x, 4)), .SDcols = cols_we_want]
# Treat the gas constant with 3 digits
DT$sp_gas <- format_power(DT$sp_gas, digits = 3)
# Format selected columns as text
cols_we_want <- c("date", "trial", "humid")
DT <- DT[, (cols_we_want) := lapply(.SD, function(x) format_text(x)), .SDcols = cols_we_want]
# Render in document
knitr::kable(DT,
align = "r",
caption = "Table 1: Multiple readings for calculating air density",
col.names = c(
"Date",
"Trial",
"Humidity",
"$\\small\\theta$ [K]",
"$\\small P$ [Pa]",
"$\\small R_{sp}$ [J kg$^{-1}$K$^{-1}$]",
"$\\small\\rho$ [kg/m$^3$]"
)
)
Date | Trial | Humidity | \(\small\theta\) [K] | \(\small P\) [Pa] | \(\small R_{sp}\) [J kg\(^{-1}\)K\(^{-1}\)] | \(\small\rho\) [kg/m\(^3\)] |
---|---|---|---|---|---|---|
\(\scriptsize\mathrm{2018-06-12}\) | \(\scriptsize\mathrm{a}\) | \(\scriptsize\mathrm{low}\) | \(\scriptsize 294.1\) | \(\scriptsize 1.011 \times 10^{5}\) | \(\scriptsize 287\) | \(\scriptsize 1.198\) |
\(\scriptsize\mathrm{2018-06-13}\) | \(\scriptsize\mathrm{b}\) | \(\scriptsize\mathrm{high}\) | \(\scriptsize 294.1\) | \(\scriptsize 1.010 \times 10^{5}\) | \(\scriptsize 287\) | \(\scriptsize 1.196\) |
\(\scriptsize\mathrm{2018-06-14}\) | \(\scriptsize\mathrm{c}\) | \(\scriptsize\mathrm{med}\) | \(\scriptsize 294.6\) | \(\scriptsize 1.011 \times 10^{5}\) | \(\scriptsize 287\) | \(\scriptsize 1.196\) |
\(\scriptsize\mathrm{2018-06-15}\) | \(\scriptsize\mathrm{d}\) | \(\scriptsize\mathrm{low}\) | \(\scriptsize 293.4\) | \(\scriptsize 1.010 \times 10^{5}\) | \(\scriptsize 287\) | \(\scriptsize 1.200\) |
\(\scriptsize\mathrm{2018-06-16}\) | \(\scriptsize\mathrm{e}\) | \(\scriptsize\mathrm{high}\) | \(\scriptsize 293.9\) | \(\scriptsize 1.011 \times 10^{5}\) | \(\scriptsize 287\) | \(\scriptsize 1.199\) |
You can overwrite your global assignment using the
format
or size
argument in any individual
function call.
format
option affects arguments of
format_power()
only.size
option affects arguments of both
format_text()
and format_power()
.Davide Cervone (2018) MathJax: TeX and LaTeX math delimiters, https://docs.mathjax.org/en/v2.7-latest/tex.html#tex-and-latex-math-delimiters.
Morgan Chase (2021) Technical Mathematics, https://openoregon.pressbooks.pub/techmath/chapter/module-11-scientific-notation/.