Learn R Programming

parseLatex

The goal of parseLatex is to provide a parser for a subset of LaTeX syntax that is more complete than what is handled by the tools::parseLatex() parser.

Perhaps some day it will handle all LaTeX inputs, but that’s not likely. For now, I’m aiming to handle anything that knitr::kable() and the kableExtra functions will produce, plus related code.

A website describing the current state is here: https://dmurdoch.github.io/parseLatex/ .

Installation

You can install the development version of parseLatex from GitHub with:

# install.packages("pak")
pak::pak("dmurdoch/parseLatex")

Example

This is a basic example.

library(parseLatex)
library(kableExtra)
latex <- kbl(mtcars[1:2, 1:2], format = "latex")
cat(latex)
#> 
#> \begin{tabular}[t]{l|r|r}
#> \hline
#>   & mpg & cyl\\
#> \hline
#> Mazda RX4 & 21 & 6\\
#> \hline
#> Mazda RX4 Wag & 21 & 6\\
#> \hline
#> \end{tabular}
parsed <- parseLatex(latex)
# This is a blank followed by a table; drop the blank
table <- parsed[[find_env(parsed, "tabular")]]
# Get the alignment options from the content
brace_options(get_contents(table))
#> {l|r|r}
tableCell(table, 2,2) # The title counts!
#>  21
tableCell(table, 2,2) <- "Changed!"
table
#> ENVIRONMENT: \begin{tabular}[t]{l|r|r}
#> \hline
#>   & mpg & cyl\\
#> \hline
#> Mazda RX4 & Changed! & 6\\
#> \hline
#> Mazda RX4 Wag & 21 & 6\\
#> \hline
#> \end{tabular}

To-do list

  • Possibly add class for table objects, and implement indexing and subsetting on that class.
  • See what kableExtra does with tables, and try to make that easier.
  • Do something with math environments?

Copy Link

Version

Install

install.packages('parseLatex')

Monthly Downloads

188

Version

0.4.1

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Duncan Murdoch

Last Published

June 6th, 2025

Functions in parseLatex (0.4.1)

names

Utility functions finding names and types of objects
options

Find or modify macro or environment options
showErrors

Show errors in parsed Latex object
print.LaTeX2item

Print methods
path_to

Find path to a particular kind of item
vector_to_row

Convert vector to table row and back
reduce_whitespace

Remove excess whitespace recursively
tablecalcs

Calculations on tables
splitting

Splitting lists of items
tableCell

Work with table cells
tables

Functions related to parsing LaTeX tables
find_tableContent

Functions relating to the data content of a table
tableOption

Functions related to table options.
get_leftovers

Retrieve source from beyond the end of the document.
itemlist

Lists of items
tests

Test objects
vector_to_latex2

Convert vector to items
parseLatex_pkg

The parseLatex package
rmSrcrefs

Remove srcrefs
tableRule

Work with rules in tables
find_tableRow

Functions to work with rows in tables
defaultCatcodes

The default "catcodes" used by parseLatex.
find_sequence

Find a code sequence
get_contents

Convenience functions to get or set contents of item
finders

Miscellaneous low-level finders
LaTeX2range

Ranges within LaTeX2 lists.
deparseLatex

Convert latex object into character vector
find_pattern

Find a pattern in deparsed items
Utilities

Miscellaneous utilities
as_LaTeX2

Coerce to LaTeX2
find_caption

Find or drop captions
prepare_table

Split up a table by rows
path_to_index

Convert between paths and indices
set_range

Set items in a LaTeX2 object
parseLatex_fn

Parse LaTeX code