R/tablecalcs.R

Defines functions tableDim tableNcol tableNrow

Documented in tableDim tableNcol tableNrow

#' Calculations on tables
#'
#' @name tablecalcs
#' @rdname tablecalcs
#' @param table A known tabular-like environment object.
#'
#' @returns `tableNrow()` returns the number of rows in the table.
#' @examples
#' latex <- kableExtra::kbl(mtcars[1:2, 1:3], format = "latex")
#' parsed <- parseLatex(latex)
#' table <- parsed[[find_tabular(parsed)]]
#' table
#' tableNrow(table)
#' @export
tableNrow <- function(table) {
  content <- tableContent(table)
  length(find_macro(content, "\\\\"))
}

#' @rdname tablecalcs
#' @returns `tableNcol()` returns the number of columns in the table.
#' @examples
#' tableNcol(table)
#' @export
tableNcol <- function(table) {
  opts <- drop_whitespace(get_contents(columnOptions(table)[[1]]))
  opts <- drop_items(opts, find_char(opts, "|"))
  sum(sapply(opts, function(x)
    if(latexTag(x) == "TEXT") nchar(x)
    else 0))
}

#' @rdname tablecalcs
#' @returns `tableDim()` returns the number of rows and columns in the table.
#'
#' @examples
#' tableDim(table)
#' @export
tableDim <- function(table) {
  c(tableNrow(table), tableNcol(table))
}

Try the parseLatex package in your browser

Any scripts or data that you put into this service are public.

parseLatex documentation built on April 3, 2025, 5:27 p.m.