💾 Archived View for republic.circumlunar.space › users › johngodlee › posts › 2020-01-07-r-latex.gmi captured on 2023-09-08 at 16:20:54. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-04)

-=-=-=-=-=-=-

R functions for creating LaTeX variables

DATE: 2020-01-07

AUTHOR: John L. Godlee

After writing some more for a manuscript I decided to formalise my workflow where I output numerical variables from R for use in LaTeX, so if the values change they update automatically in the manuscript. Below is a collection of functions which make the process easier:

Formatting a number with the correct number of decimal points or significant figures:

num_format <- function(x, digits = 2, method = "round"){
  sprintf(paste0("%.",digits,"f"),
    if(method == "round"){
      round(x, digits = digits)
    }else if(method == "signif"){
      signif(x, digits = digits)
    })
}

Formatting a p-value:

p_format <- function(p, digits = 2){
  dplyr::case_when(p < 0.01 ~ "p<0.01",
    p < 0.05 ~ "p<0.05",
    TRUE ~ paste0("p = ", as.character(num_format(p, digits = digits)))
  )
}

Extracting the slope and standard error from a linear regression:

lm_format <- function(x, digits = 2){
  paste0("F(",
    summary(x)$fstatistic[2],
    ",",
    summary(x)$fstatistic[3],
    ") = ",
    num_format(summary(x)$fstatistic[1], digits = digits),
    ", ",
    p_format(anova(x)


gemini - kennedy.gemi.dev




Pr(>F)`[1])
  )
}

mod1 <- lm(mtcars$mpg ~ mtcars$hp

Formatting two arbitrary numbers as x plus/minus y:

pm_format <- function(x, y, dx = 2, dy = dx + 1, pm = "$\\pm$"){
  paste0(num_format(x, digits = dx),
    pm,
    num_format(y, digits = dy)
  )
}

Output the value of a variable as a LaTeX variable:

command_output <- function(x, name){ 
  paste0("\\newcommand{\\",
    ifelse(missing(name), deparse(substitute(x)), name), 
    "}{", 
    x, 
    "}"
  )
}

x <- 0.4367

Notice that all the backslashes have to be duplicated to escape them when they are written to a file.

Write a list of commands to a .tex file:

latex_write <- function(list, path){
  fileConn <- file(path)
  writeLines(
    unlist(list, use.names = FALSE), fileConn)
  close(fileConn)
}

Full example:

val1 <- 0.55
val2 <- 0.2477
val3 <- 0.044

values_list <- list(val1 = val1, val2 = val2, val3 = val3)

command_list <- list()
for(i in 1:length(values_list)){
    command_list[[i]] <- command_output(
      num_format(
        values_list[[i]]
        ),
      names(values_list[i])
    )
}

latex_write(command_list, "test.tex")