Skip to contents

This function creates a row of a LaTeX table.

Usage

TexRow(
  value,
  cspan = rep(1, length(value)),
  position = "c",
  surround = "%s",
  space = 0,
  dec = 3,
  percentage = FALSE,
  dollar = FALSE,
  se = FALSE,
  pvalues = NULL
)

Arguments

value

The value(s) to be formatted. Must be a numeric or character vector.

cspan

(integer). If greater than 1, multicolumn{cspan}{position}{value} will be used. For example, cspan=c(1,2,1) means that the second entry of value should span 2 columns. Default is cspan = rep(1, length(value)).

position

(character). If cspan > 1, multicolumn{cspan}{position}{value} will be used. For example, position=c("l","c","r") means that the second entry of value should be centered. Default is "c".

surround

(character). This will be applied to the value as sprintf(surround, value), so surround must contain the "%s" placeholder. Default is "%s".

space

(numeric). The number of points (pt) of vertical space to append to the end of the row. Default is 0.

dec

(integer). Only relevant if value is numeric. Number of decimal places. If scalar, the same decimal will be used for each entry of value. If vector, must be the same length as value. Default is 3.

percentage

(logical). Only relevant if value is numeric. If TRUE, a percentage symbol "%" will be added to the end of each entry of value. If scalar, it will be used for all entries of value. If vector, must be the same length as value.

dollar

(logical). Only relevant if value is numeric. If TRUE, a dollar sign "$" will be added to the end of each entry of value. If scalar, it will be used for all entries of value. If vector, must be the same length as value.

se

(logical). Only relevant if value is numeric. If TRUE, value will be wrapped in parentheses. If scalar, it will be used for all entries of value. If vector, must be the same length as value.

pvalues

(numeric). Only relevant if value is numeric. If not NULL, must be numeric. If less than 0.1, a star will be added to the end of each entry of value. If less than 0.05, a second star will be appended. If less than 0.01, a third star will be appended. If scalar, the same p-value will be assumed for all entries of value. If vector, must be the same length as value.

Value

The output is a textab block.

Examples

# basic character row:
vec = c("hello", "world")
TexRow(vec)
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rr}
#> hello & world \\
#> \end{tabular}

# character row with LaTeX formatting:
vec = c('Hello','\\textbf{World}','$\\alpha$','$\\frac{1}{2}$')
TexRow(vec)
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rrrr}
#> Hello & \textbf{World} & $\alpha$ & $\frac{1}{2}$ \\
#> \end{tabular}

# basic numeric row:
vec <- c(1.0, 1.01, 1.001)
TexRow(vec)
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rrr}
#> 1.000 & 1.010 & 1.001 \\
#> \end{tabular}
TexRow(vec, dec = 2) # round to second decimal place
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rrr}
#> 1.00 & 1.01 & 1.00 \\
#> \end{tabular}

# custom formatting of numbers using surround argument:
vec = c(5.081, 2.345, 6.789)
TexRow(vec, dec = 1, surround = "{\\color{red} %s}")
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rrr}
#> {\color{red} 5.1} & {\color{red} 2.3} & {\color{red} 6.8} \\
#> \end{tabular}

# use cspan argument to merge the second and third rows:
vec = c("hello", "world")
TexRow(vec, cspan = c(1,2))
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rr}
#> hello & \multicolumn{2}{c}{world} \\
#> \end{tabular}
TexRow(vec, cspan = c(1,2), position = "c") # center merged columns
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rr}
#> hello & \multicolumn{2}{c}{world} \\
#> \end{tabular}

# concatenate blocks vertically or horizontally:
block1 = TexRow(c("hello","world","block"))
block2 = TexRow(c(5.081, 2.345, 6.789), dec=1)
block1 / block2 # horizontal
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rrrrrr}
#> hello & world & block & 5.1 & 2.3 & 6.8 \\
#> \end{tabular}
block1 + block2 # vertical
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rrr}
#> hello & world & block \\
#> 5.1 & 2.3 & 6.8 \\
#> \end{tabular}

# add 3pt of vertical space between two rows using the space argument:
TexRow(c("hello", "world"), space=3) + TexRow(c('$\\alpha$','$\\frac{1}{2}$'))
#> % created using textab on Wed Apr 26 17:42:42 2023
#> \begin{tabular}{rr}
#> hello & world \\[3pt]
#> $\alpha$ & $\frac{1}{2}$ \\
#> \end{tabular}