Title: | Parse 'LaTeX' Code |
---|---|
Description: | Exports an enhanced version of the tools::parseLatex() function to handle 'LaTeX' syntax more accurately. Also includes numerous functions for searching and modifying 'LaTeX' source. |
Authors: | Duncan Murdoch [aut, cre], The R Core Team [ctb, cph] |
Maintainer: | Duncan Murdoch <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.3.12 |
Built: | 2025-03-19 22:16:22 UTC |
Source: | https://github.com/dmurdoch/parselatex |
Coerce to LaTeX2
as_LaTeX2(x) latex2(...)
as_LaTeX2(x) latex2(...)
x |
An object to convert to a LaTeX2 object. |
... |
Objects to concatenate. |
as_LaTeX2()
converts x
to a LaTeX2 object.
latex2()
converts the arguments to LaTeX2 objects
and concatenates them into a new LaTeX2 object.
The default "catcodes" used by parseLatex.
defaultCatcodes
defaultCatcodes
An object of class data.frame
with 13 rows and 2 columns.
defaultCatcodes
is a dataframe containing the
default catcode definitions. The numeric values of each
code are exported, e.g. LETTER
is 11.
# \makeatletter has no effect by default... unclass(parseLatex("\\makeatletter\\internal@macro")) # ... but the effect can be simulated atletter <- rbind(defaultCatcodes, data.frame(char="@", catcode=11)) unclass(parseLatex("\\makeatletter\\internal@macro", catcodes = atletter)) # These are the default codes: cbind(defaultCatcodes, name = c("ESCAPE", "LBRACE", "RBRACE", "MATH", "ALIGN", "NEWLINE","NEWLINE", "PARAM", "SUPER", "SUB", "SPACE", "SPACE", "COMMENT")) # The missing ones are # 9 - IGNORE # 11 - LETTER # 12 - OTHER # 13 - ACTIVE # 15 - INVALID
# \makeatletter has no effect by default... unclass(parseLatex("\\makeatletter\\internal@macro")) # ... but the effect can be simulated atletter <- rbind(defaultCatcodes, data.frame(char="@", catcode=11)) unclass(parseLatex("\\makeatletter\\internal@macro", catcodes = atletter)) # These are the default codes: cbind(defaultCatcodes, name = c("ESCAPE", "LBRACE", "RBRACE", "MATH", "ALIGN", "NEWLINE","NEWLINE", "PARAM", "SUPER", "SUB", "SPACE", "SPACE", "COMMENT")) # The missing ones are # 9 - IGNORE # 11 - LETTER # 12 - OTHER # 13 - ACTIVE # 15 - INVALID
Convert latex object into character vector
deparseLatex(x, dropBraces = FALSE)
deparseLatex(x, dropBraces = FALSE)
x |
A latex object. |
dropBraces |
Whether to drop unnecessary braces. |
deparseLatex
returns character vector corresponding
to the parsed Latex.
Find or drop captions
find_captions(items) drop_captions(items, idx = NULL) path_to_caption(items)
find_captions(items) drop_captions(items, idx = NULL) path_to_caption(items)
items |
A LaTeX2 or other list of LaTeX2items. |
idx |
|
find_captions()
returns the indices within the items of any caption
text, with an attribute extra
holding indices of
associated macros and whitespace.
drop_captions()
returns the items
with
captions dropped as a LaTeX2 object. It has an attribute
named idx
that is the idx
argument with corresponding
elements dropped.
path_to_caption()
returns a path
containing the location of the first caption
block within items
. It has an attribute idx
containing a LaTeX2range object for the
associated macros and whitespace.
parsed <- parseLatex("before \\caption{This is a caption} \\\\ after") idx <- find_captions(parsed) parsed[idx] parsed[attr(idx, "extra")[[1]]] drop_captions(parsed) path_to_caption(parsed)
parsed <- parseLatex("before \\caption{This is a caption} \\\\ after") idx <- find_captions(parsed) parsed[idx] parsed[attr(idx, "extra")[[1]]] drop_captions(parsed) path_to_caption(parsed)
Searches a LaTeX2 list for text using grepl()
on deparsed versions of parts of the code.
It attempts to find the narrowest match(es) that lie
within a single container.
find_pattern(items, pattern, ..., all = FALSE)
find_pattern(items, pattern, ..., all = FALSE)
items |
A list of latex items. |
pattern |
Pattern to use in |
... |
Additional parameters to pass to |
all |
Find all matching, or the first? |
find_pattern()
does a recursive search in the order
items appear in the deparse. If the pattern matches,
it attempts to narrow the match by recursing into
containers and dropping earlier and later items.
It should always return syntactically correct LaTeX
code in which the pattern appears.
find_pattern()
returns a LaTeX2range object
or (if all
is TRUE
) a list of them.
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex", caption = "Sample table") parsed <- parseLatex(latex) parsed loc <- find_pattern(parsed, "RX4 Wag", fixed = TRUE) loc print(loc, source = parsed)
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex", caption = "Sample table") parsed <- parseLatex(latex) parsed loc <- find_pattern(parsed, "RX4 Wag", fixed = TRUE) loc print(loc, source = parsed)
Find a code sequence
find_sequence(items, sequence, all = FALSE, ignore_whitespace = TRUE) items_are_equal(items1, items2)
find_sequence(items, sequence, all = FALSE, ignore_whitespace = TRUE) items_are_equal(items1, items2)
items , sequence
|
LaTeX2 objects or lists. |
all |
Whether to return all matches, or just the first. |
ignore_whitespace |
Whether to ignore whitespace in comparisons. |
items1 , items2
|
Two LaTeX2 or LaTeX2item objects. |
find_sequence()
returns a path or list of paths where sequence
occurs within items
.
items_are_equal
returns a logical indicator of equality after removing source references.
find_sequence(parseLatex("a & b & c"), "b & c")
find_sequence(parseLatex("a & b & c"), "b & c")
Functions relating to the data content of a table
find_tableContent(table) tableContent(table) tableContent(table, asis = FALSE) <- value
find_tableContent(table) tableContent(table) tableContent(table, asis = FALSE) <- value
table |
A tabular-like environment to work with. |
asis |
Should newlines be added around the value? |
value |
The content to be inserted into the cell. This can be a LaTeX2 object, or a character string that will be converted to one. |
Unless asis = TRUE
, tableContent(table) <- value
will add newlines
at the start end end if not present, to make the result
more readable.
find_tableContent()
returns the indices of the
entries corresponding to content of the table.
tableContent()
returns a LaTeX2 object containing
all of the table content after the options.
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] table tableContent(table) tableContent(table) <- "Mazda RX4 & 21 & 6\\\\" table tableContent(table, asis = TRUE) <- "Mazda RX4 & 21 & 6\\\\" table
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] table tableContent(table) tableContent(table) <- "Mazda RX4 & 21 & 6\\\\" table tableContent(table, asis = TRUE) <- "Mazda RX4 & 21 & 6\\\\" table
Functions to work with rows in tables
find_tableRow(table, row, withExtras = FALSE, withData = TRUE) tableRow(table, row, withExtras = FALSE, withData = TRUE) tableRow(table, row, asis = FALSE, withExtras = FALSE, withData = TRUE) <- value
find_tableRow(table, row, withExtras = FALSE, withData = TRUE) tableRow(table, row, withExtras = FALSE, withData = TRUE) tableRow(table, row, asis = FALSE, withExtras = FALSE, withData = TRUE) <- value
table |
A tabular-like environment to work with. |
row |
row in the table (1 is top row), including rows of labels. |
withExtras |
If |
withData |
If |
asis |
Should a linebreak and newline be added after the value? |
value |
The content to be inserted into the cell. This can be a LaTeX2 object, or a character string that will be converted to one. |
Unless asis = TRUE
, tableContent(table) <- value
will add "\" and a newline
at the end if not present.
If the row
value is higher than the number of rows
in the table, blank rows will be added to fill the
space between.
If withExtras = TRUE
and you want the
result to start on a new line, you need to add the
newline explicitly in value
when using the
assignment function.
find_tableRow()
returns the indices of the
entries corresponding to the content of row i of the table.
tableRow()
returns a LaTeX2 object containing
all of the table content in the row.
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] find_tableRow(table, 1) tableRow(table, 1) tableRow(table, 1, withExtras = TRUE) tableRow(table, 5) <- "a & b & c" table
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] find_tableRow(table, 1) tableRow(table, 1) tableRow(table, 1, withExtras = TRUE) tableRow(table, 5) <- "a & b & c" table
Miscellaneous low-level finders
find_whitespace(items, all = TRUE) find_env(items, envtypes, all = TRUE) find_macro(items, macros, all = TRUE) find_catcode(items, codes, all = TRUE) find_tags(items, tags, all = TRUE) find_char(items, char, all = TRUE) find_block(items, all = TRUE)
find_whitespace(items, all = TRUE) find_env(items, envtypes, all = TRUE) find_macro(items, macros, all = TRUE) find_catcode(items, codes, all = TRUE) find_tags(items, tags, all = TRUE) find_char(items, char, all = TRUE) find_block(items, all = TRUE)
items |
A list of latex items. |
all |
If |
envtypes |
Which types of environment to look for. |
macros |
Which types of macros to look for. |
codes |
Which codes to look for. |
tags |
Which tags to look for. |
char |
Which character to look for. |
find_whitespace()
returns the indices of
whitespace in items
.
find_env()
returns the indices within items
of environments in envtypes
.
find_macro()
returns the index within items
of instances in macros
.
find_catcode()
returns the index within items
.
of specials matching code
.
find_tags()
returns the index within items
.
of items with tags matching tags
.
find_char()
returns the index within items
of characters matching char
. Only characters
marked as SPECIAL by the parser will be found.
find_block()
returns the index within items
of blocks (i.e. sequences in )
Convenience functions to get or set contents of item
get_contents(item) set_contents(item, value)
get_contents(item) set_contents(item, value)
item |
An item from a Latex list (or a LaTeX2 list with one item). |
value |
An object that can be coerced to be a LaTeX2 object. |
get_contents
returns the contents of the item as a LaTeX2 list.
set_contents
returns the original item
with the contents
replaced by value
.
get_contents(parseLatex("{abc}")) set_contents(parseLatex("{abc}"), "def")
get_contents(parseLatex("{abc}")) set_contents(parseLatex("{abc}"), "def")
Retrieve source from beyond the end of the document.
get_leftovers(text, items = parseLatex(text))
get_leftovers(text, items = parseLatex(text))
text |
Character vector holding source. |
items |
Parsed version of |
The part of text
that follows \end{document}
other than a single newline, named according to the
original line numbers.
The line numbering in the output matches what a
text editor would see; embedded newlines in text
will
result in separate lines in the output.
# line: 1 2 3 text <- "\\begin{document}\n\\end{document}\nnotes" get_leftovers(text)
# line: 1 2 3 text <- "\\begin{document}\n\\end{document}\nnotes" get_leftovers(text)
Ranges within LaTeX2 lists.
LaTeX2range(path, range) ## S3 method for class 'LaTeX2range' print(x, source = NULL, ...)
LaTeX2range(path, range) ## S3 method for class 'LaTeX2range' print(x, source = NULL, ...)
path |
An integer vector to use as a path. |
range |
A range of values within the path. |
x |
Object to print. |
source |
Optional parsed list from which to extract the range. |
... |
Ignored. |
LaTeX2range objects are lists with path
and range
entries. path
is a recursive index
into a LaTeX2 list, and range
is a range of
entries in the result.
If path
is NULL
, the object refers to the entire
source object. If range
is NULL
, it refers
to the whole LaTeX2item given by the path
.
LaTeX2range()
returns a constructed LaTeX2range
object.
Utility functions finding names and types of objects
latexTag(item) catcode(item) envName(item) envName(item) <- value macroName(item)
latexTag(item) catcode(item) envName(item) envName(item) <- value macroName(item)
item |
A LaTeX2item which is an environment |
value |
A character string to set as the name |
latexTag()
returns the LaTeX2 tag for the item or NULL
.
catcode()
returns the TeX catcode for the item, or NULL
.
envName()
returns the Latex environment name for an item, or NULL
.
macroName()
returns the Latex macro, or NULL
.
Many Latex environments and macros take optional parameters
wrapped in square brackets. find_bracket_options
finds those,
assuming they come immediately after the macro.
Some Latex environments and macros take optional parameters
wrapped in curly brackets (braces). find_brace_options
finds those
if they immediately follow the environment or macro (and possibly
some bracketed options).
find_bracket_options(items, which = 1, start = 1) bracket_options(items, which = 1, start = 1) bracket_options(items, which = 1, start = 1, asis = FALSE) <- value find_brace_options(items, which = 1, start = 1) brace_options(items, which = 1, start = 1) brace_options(items, which = 1, start = 1, asis = FALSE) <- value
find_bracket_options(items, which = 1, start = 1) bracket_options(items, which = 1, start = 1) bracket_options(items, which = 1, start = 1, asis = FALSE) <- value find_brace_options(items, which = 1, start = 1) brace_options(items, which = 1, start = 1) brace_options(items, which = 1, start = 1, asis = FALSE) <- value
items |
A list of latex items. |
which |
Which options do you want? Some macros support more than one set. |
start |
Start looking at |
asis |
Should newlines be added around the value? |
value |
The content to be inserted into the cell. This can be a LaTeX2 object, or a character string that will be converted to one. |
find_bracket_options
returns indices into items
of the options (including the
brackets).
bracket_options
returns a LaTeX2 object containing
the specified options.
find_brace_options
returns the index of the block containing the options.
brace_options
returns a LaTeX2 object containing
the specified options.
parsed <- parseLatex("\\section[a]{b}") macro <- find_macro(parsed, "\\section") bracket_options(parsed, start = macro + 1) bracket_options(parsed, start = macro + 1) <- "Short Title" parsed brace_options(parsed, start = macro + 1) brace_options(parsed, start = macro + 1) <- "Long Title" parsed
parsed <- parseLatex("\\section[a]{b}") macro <- find_macro(parsed, "\\section") bracket_options(parsed, start = macro + 1) bracket_options(parsed, start = macro + 1) <- "Short Title" parsed brace_options(parsed, start = macro + 1) brace_options(parsed, start = macro + 1) <- "Long Title" parsed
The parseLatex
function parses LaTeX source, producing a structured object.
parseLatex( text, verbose = FALSE, verbatim = c("verbatim", "verbatim*", "Sinput", "Soutput"), verb = "\\Sexpr", defcmd = c("\\newcommand", "\\renewcommand", "\\providecommand", "\\def", "\\let"), defenv = c("\\newenvironment", "\\renewenvironment"), catcodes = defaultCatcodes, recover = FALSE, showErrors = recover, ... )
parseLatex( text, verbose = FALSE, verbatim = c("verbatim", "verbatim*", "Sinput", "Soutput"), verb = "\\Sexpr", defcmd = c("\\newcommand", "\\renewcommand", "\\providecommand", "\\def", "\\let"), defenv = c("\\newenvironment", "\\renewenvironment"), catcodes = defaultCatcodes, recover = FALSE, showErrors = recover, ... )
text |
A character vector containing LaTeX source code. |
verbose |
If |
verbatim |
A character vector containing the names of LaTeX environments holding verbatim text. |
verb |
A character vector containing LaTeX macros that should be assumed to hold verbatim text. |
defcmd , defenv
|
Character vectors of macros that are assumed to define new macro commands or environments respectively. See the note below about some limitations. |
catcodes |
A list or dataframe holding LaTeX "catcodes", such as defaultCatcodes. |
recover |
If |
showErrors |
If |
... |
Additional parameters to pass to showErrors. |
Some versions of LaTeX such as pdflatex
only handle ASCII
inputs, while others such as xelatex
allow Unicode input.
parseLatex
allows Unicode input.
During processing of LaTeX input, an interpreter can change
the handling of characters as it goes, using the \catcode
macro
or others such as \makeatletter
. However, parseLatex()
is purely
a parser, not an interpreter, so it can't do that, but
the user can change handling for the whole call using the
catcodes
argument.
catcodes
should be a list or dataframe
with at least two columns:
char
should be a column of single characters.
catcode
should be a column of integers in the range 0 to 15
giving the corresponding catcode.
During parsing, parseLatex
will check these values first.
If the input character doesn't match anything, then it will
be categorized:
as a letter (catcode 11) using the ICU function
u_hasBinaryProperty(c, UCHAR_ALPHABETIC)
(or iswalpha(c)
on
Windows),
as a control character (catcode 15) if its code point is less than 32,
as "other" (catcode 12) otherwise.
When recover = TRUE
, the parser will mark each error
in the output, and attempt to continue parsing. This
may lead to a cascade of errors, but will sometimes
help in locating the first error. The section of text
related to the error will be marked as an item with
tag ERROR
.
parseLatex
returns parsed Latex in a list with class "LaTeX2"
. Items in the list have class "LaTeX2item"
.
defcmd
limitationsThe LaTeX defining commands have fairly simple syntax, but
\def
and \let
from plain Tex have quite variable syntax
and parseLatex()
does not attempt to handle it all. Stick
with simple syntax like \def\bea{\begin{eqnarray}}
and
it should work.
LaTeX2, LaTeX2item
parsed <- parseLatex(r"(fran\c{c}ais)") parsed
parsed <- parseLatex(r"(fran\c{c}ais)") parsed
Exports an enhanced version of the tools::parseLatex() function to handle 'LaTeX' syntax more accurately. Also includes numerous functions for searching and modifying 'LaTeX' source.
Maintainer: Duncan Murdoch [email protected]
Other contributors:
The R Core Team [contributor, copyright holder]
Useful links:
Report bugs at https://github.com/dmurdoch/parseLatex/issues
Find path to a particular kind of item
path_to(items, is_fn, ..., all = FALSE) get_item(items, path) set_item(items, path, value) insert_values(items, path, values) get_container(items, path) get_which(path)
path_to(items, is_fn, ..., all = FALSE) get_item(items, path) set_item(items, path, value) insert_values(items, path, values) get_container(items, path) get_which(path)
items |
A list of latex items. |
is_fn |
Which test function to use. |
... |
Additional parameters to pass to |
all |
Return all paths, or just the first? |
path |
Integer vector of subitems |
value |
A LaTeX2item to set as a value. |
values |
A LaTeX2 list or a LaTeX2item. |
path_to()
does a recursive search in the order
items appear in the deparse.
path_to()
returns the recursive path to the
first example matching the is_fn
conditions,
or a list of paths to all matching items.
get_item()
returns the item at the given path.
set_item()
replaces the item at the given path, and returns the modified version of items
.
insert_values()
inserts the values
before the item mentioned in path
, and returns the modified version of items
.
get_container()
returns the item
containing the given path
get_which()
returns the index
of the item within its container.
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex", caption = "Sample table") parsed <- parseLatex(latex) parsed path <- path_to(parsed, is_fn = is_env, envtypes = "tabular") get_item(parsed, path)
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex", caption = "Sample table") parsed <- parseLatex(latex) parsed path <- path_to(parsed, is_fn = is_env, envtypes = "tabular") get_item(parsed, path)
Print methods
## S3 method for class 'LaTeX2item' print(x, ...) ## S3 method for class 'LaTeX2' print(x, tags = FALSE, ...)
## S3 method for class 'LaTeX2item' print(x, ...) ## S3 method for class 'LaTeX2' print(x, tags = FALSE, ...)
x |
Object to work on. |
... |
Extra parameters to pass to deparseLatex. |
tags |
Whether to display LaTeX2 tags. |
Remove excess whitespace recursively
reduce_whitespace(items, recursive = TRUE, all = FALSE)
reduce_whitespace(items, recursive = TRUE, all = FALSE)
items |
A LaTeX2 object. |
recursive |
Apply to all lists within |
all |
If |
items
with double spaces or double newlines set to single,
and trailing spaces removed (or all whitespace removed, if all
is TRUE
).
parsed <- parseLatex("a {b\n\nc}") parsed reduce_whitespace(parsed)
parsed <- parseLatex("a {b\n\nc}") parsed reduce_whitespace(parsed)
Set items in a LaTeX2 object
set_range(items, range, values) get_range(items, range)
set_range(items, range, values) get_range(items, range)
items |
A LaTeX2 object or other list of LaTeX2item objects. |
range |
A LaTeX2range object. |
values |
An object that can be coerced to
a LaTeX2 object or (if |
set_range()
replaces the item(s) at the given path, and returns the modified version of items
.
get_range()
extracts the
specified range and returns it as a LaTeX2 object.
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex", caption = "Sample table") parsed <- parseLatex(latex) tablepath <- path_to(parsed, is_env, envtypes = "tabular") range <- LaTeX2range(tablepath, 11) set_range(parsed, range, "The 11th item") get_range(parsed, range)
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex", caption = "Sample table") parsed <- parseLatex(latex) tablepath <- path_to(parsed, is_env, envtypes = "tabular") range <- LaTeX2range(tablepath, 11) set_range(parsed, range, "The 11th item") get_range(parsed, range)
Show errors in parsed Latex object
showErrors( x, repeatSrcline = FALSE, errorMsgTwice = FALSE, lineNumbers = TRUE, showAllLines = FALSE )
showErrors( x, repeatSrcline = FALSE, errorMsgTwice = FALSE, lineNumbers = TRUE, showAllLines = FALSE )
x |
A LaTeX2 object. |
repeatSrcline |
Repeat the source line when it has multiple errors? |
errorMsgTwice |
Show the error message at both the start and end of a multiline error? |
lineNumbers |
Show line numbers on output? |
showAllLines |
Show all lines whether they have errors or not? |
A list of paths to errors, invisibly.
parsed <- parseLatex("\\end{baz} \\begin{foo} \n \\begin{bar} $1+1\n4", recover = TRUE, showErrors = FALSE) showErrors(parsed)
parsed <- parseLatex("\\end{baz} \\begin{foo} \n \\begin{bar} $1+1\n4", recover = TRUE, showErrors = FALSE) showErrors(parsed)
Calculations on tables
tableNrow(table) tableNcol(table) tableDim(table)
tableNrow(table) tableNcol(table) tableDim(table)
table |
A known tabular-like environment object. |
tableNrow()
returns the number of rows in the table.
tableNcol()
returns the number of columns in the table.
tableDim()
returns the number of rows and columns in the table.
latex <- kableExtra::kbl(mtcars[1:2, 1:3], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] table tableNrow(table) tableNcol(table) tableDim(table)
latex <- kableExtra::kbl(mtcars[1:2, 1:3], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] table tableNrow(table) tableNcol(table) tableDim(table)
These functions work with the content of cells in tabular-like environments. Cells are numbered with the first row (typically column titles) being row 1. Rules (i.e. horizontal lines) are not considered part of a cell.
find_tableCell(table, row, col) tableCell(table, row, col) tableCell(table, row, col, asis = FALSE) <- value
find_tableCell(table, row, col) tableCell(table, row, col) tableCell(table, row, col, asis = FALSE) <- value
table |
A tabular-like environment to work with. |
row , col
|
row and column in the table. |
asis |
Should blanks be added around the value? |
value |
The content to be inserted into the cell. This can be a LaTeX2 object, or a character string that will be converted to one. |
find_tableCell()
returns NA
if the requested
cell is missing because an earlier cell covered multiple
columns. It signals an error if a request is made beyond
the bounds of the table.
Unless asis = TRUE
, tableContent(table) <- value
will add blanks
at the start end end if not present, to make the result
more readable.
If col
is higher than the current table width,
the assignment will fail with an error. If only row
is too high, blank lines will be added and it should
succeed.
find_tableCell()
returns the indices of the
entries corresponding to the content of the cell (row, col) of the table.
tableCell()
returns a LaTeX2 object containing
all of the table content in the cell (but not the &).
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] find_tableCell(table, 1, 2) tableCell(table, 1, 2) tableCell(table, 5, 2) <- " d " table
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] find_tableCell(table, 1, 2) tableCell(table, 1, 2) tableCell(table, 5, 2) <- " d " table
Functions related to table options.
find_posOption(table) posOption(table) posOption(table, asis = FALSE) <- value find_widthOption(table) widthOption(table) widthOption(table, asis = FALSE) <- value find_columnOptions(table) columnOptions(table) columnOption(table, column) columnOptions(table, asis = FALSE) <- value columnOption(table, column) <- value
find_posOption(table) posOption(table) posOption(table, asis = FALSE) <- value find_widthOption(table) widthOption(table) widthOption(table, asis = FALSE) <- value find_columnOptions(table) columnOptions(table) columnOption(table, column) columnOptions(table, asis = FALSE) <- value columnOption(table, column) <- value
table |
A known tabular-like environment object, or the contents of one. |
asis |
Whether to make small modifications in replacement functions. |
value |
A character string or LaTeX2 object. |
column |
For which column? |
Unless asis == TRUE
, the value for value
in posOption(table) <- value
can be specified with or without the enclosing brackets.
find_posOption()
returns the indices of the
entries corresponding to the "pos" option, including the
brackets, within the table.
posOption()
returns a LaTeX2 object containing the
"pos" option.
find_widthOption()
returns the index of the
block corresponding to the "width" option, if there is one.
Only some tabular-like environments have these.
widthOption()
returns a LaTeX2 object containing the
"width" option, if the table has one.
find_columnOptions()
returns the index of the
block corresponding to the column spec.
columnOptions()
returns a LaTeX2 object containing the
"column" options.
columnOption()
returns a LaTeX2 object
containing the requested column option. A "|"
divider
will not be included.
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] table find_posOption(table) posOption(table) posOption(table) <- "h" posOption(table) find_widthOption(table) widthOption(table) find_columnOptions(table) columnOptions(table) columnOption(table, 3) columnOptions(table) <- "lrr" table columnOption(table, 3) <- "p{1cm}" columnOptions(table)
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] table find_posOption(table) posOption(table) posOption(table) <- "h" posOption(table) find_widthOption(table) widthOption(table) find_columnOptions(table) columnOptions(table) columnOption(table, 3) columnOptions(table) <- "lrr" table columnOption(table, 3) <- "p{1cm}" columnOptions(table)
In LaTeX, "rules" are horizontal lines in a table. These functions let rules be extracted or modified.
find_rules(table) rules(table, idx = find_rules(table)) find_rule(table, row, idx = find_rules(table)) rule(table, row, idx = find_rules(table)) rule(table, row, asis = FALSE, idx = find_rules(table)) <- value
find_rules(table) rules(table, idx = find_rules(table)) find_rule(table, row, idx = find_rules(table)) rule(table, row, idx = find_rules(table)) rule(table, row, asis = FALSE, idx = find_rules(table)) <- value
table |
A tabular-like environment to work with. |
idx |
A list of indices as produced by |
row |
The rules will precede the contents of this row.
The rule after the final row uses |
asis |
Should a newline be added after the
value? If |
value |
The content to be inserted into the cell. This can be a LaTeX2 object, or a character string that will be converted to one. |
find_rules()
returns a list of the indices
of rules before each row, including the whitespace
following each one.
rules(table)
returns a list of the rules
before each row. The last entry will be the rule(s)
following the last row.
find_rule(table, row)
returns the indices
of the rule(s) before row
, not including the final whitespace.
rule(table, row)
returns the indices
rule(s) before row
.
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] table find_rules(table) rules(table) find_rule(table, 1) rule(table, 1) rule(table, 2) <- "\\midrule" table
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) table <- parsed[[find_tabular(parsed)]] table find_rules(table) rules(table) find_rule(table, 1) rule(table, 1) rule(table, 2) <- "\\midrule" table
Functions related to parsing LaTeX tables
is_Tabular(item) find_tabular(items, start = 1)
is_Tabular(item) find_tabular(items, start = 1)
item |
An item from a LaTeX2 list object. |
items |
A LaTeX2 list object. |
start |
Where to start looking. |
is_Tabular()
returns boolean indicating if this is
a tabular-like environment.
find_tabular()
returns the index of the first
tabular-like environment, or NA
if none is found.
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) is_Tabular(parsed[[2]]) find_tabular(parsed) table <- parsed[[find_tabular(parsed)]] table
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex") parsed <- parseLatex(latex) is_Tabular(parsed[[2]]) find_tabular(parsed) table <- parsed[[find_tabular(parsed)]] table
Test objects
is_env(item, envtypes = NULL) is_macro(item, macros = NULL) is_block(item) is_bracket(item, bracket) is_whitespace(item) is_text(item) is_error(item) is_char(item, char)
is_env(item, envtypes = NULL) is_macro(item, macros = NULL) is_block(item) is_bracket(item, bracket) is_whitespace(item) is_text(item) is_error(item) is_char(item, char)
item |
An object of class LaTeX2item to test. |
envtypes |
Types of Latex environment to check for,
e.g. |
macros |
Which macros to match, e.g. |
bracket |
Which bracket are we looking for? |
char |
A character to match |
is_env()
returns a boolean if the item matches.
is_macro()
returns a boolean indicating the match.
is_block()
returns a boolean indicating whether the item
is a block wrapped in curly braces.
is_bracket()
returns a boolean indicating that the item
is a bracket of the
specified type.
is_whitespace()
returns a boolean indicating if the
item
is a space, tab or newline.
is_text()
returns a boolean indicating if the
item
is text.
is_error()
returns a boolean indicating if the
item
is an error.
is_char()
returns a boolean indicating if the
item
is a SPECIAL
matching char
.
is_bracket(parseLatex("[]")[[1]], "[")
is_bracket(parseLatex("[]")[[1]], "[")
Miscellaneous utilities
drop_items(items, which) select_items(items, which) drop_whitespace(items) trim_whitespace(items) include_whitespace(items, which) split_list(items, splits) split_latex(items, splits) split_chars(item, split = "") new_block(...) new_env(name, ...)
drop_items(items, which) select_items(items, which) drop_whitespace(items) trim_whitespace(items) include_whitespace(items, which) split_list(items, splits) split_latex(items, splits) split_chars(item, split = "") new_block(...) new_env(name, ...)
items |
A LaTeX2 object or list of items, or a LaTeX2item which is a list. |
which |
Which items to operate on. |
splits |
Which items divide the parts? |
item |
A non-list LaTeX2item. |
split |
Where to split the characters. |
... |
Items to be passed to |
name |
The desired environment name. |
drop_items()
returns the list of items with specific items removed.
select_items()
returns the list of subsetted items.
drop_whitespace()
returns the items with
whitespace (blanks, tabs, newlines) removed.
trim_whitespace()
returns the items with
leading and trailing whitespace (blanks, tabs, newlines) removed.
include_whitespace()
returns which
with
following whitespace (blanks, tabs, newlines) included.
split_list()
returns a list of pieces
separated at the splits.
split_latex()
returns a list of pieces
separated at the splits. Each piece is marked as
a LaTeX2 object.
split_chars()
returns a LaTeX2
list containing the result of calling strsplit
on the text of the item
.
new_block()
returns a BLOCK
item containing the items.
new_env()
returns an environment item containing the other items.
drop_whitespace()
will drop the whitespace that separates text items, so deparsing will merge
them into a single item.
drop_whitespace()
does not act recursively; use reduce_whitespace for that.
parsed <- parseLatex("Hello") unclass(parsed) unclass(split_chars(parsed[[1]])) new_block(parseLatex("abc")) new_env("itemize", parseLatex("\\item An item"))
parsed <- parseLatex("Hello") unclass(parsed) unclass(split_chars(parsed[[1]])) new_block(parseLatex("abc")) new_env("itemize", parseLatex("\\item An item"))
Convert vector to items
vector_to_latex2(x)
vector_to_latex2(x)
x |
A list or vector to convert. |
A LaTeX2 object containing the entries
of x
concatenated.
print(vector_to_latex2(1:3), tags = TRUE)
print(vector_to_latex2(1:3), tags = TRUE)
Convert vector to table row and back
vector_to_row(cells, asis = FALSE, linebreak = TRUE) row_to_vector(row, asis = FALSE, deparse = TRUE)
vector_to_row(cells, asis = FALSE, linebreak = TRUE) row_to_vector(row, asis = FALSE, deparse = TRUE)
cells |
A list or vector of cell contents. |
asis |
If |
linebreak |
If |
row |
A row from a table |
deparse |
Should the result be deparsed? |
vector_to_row
returns a LaTeX2 object which could be a row
in a tabular object.
row_to_vector
returns a character vector of the
deparsed contents of the row, or if deparse
is FALSE
, a list of the contents.
vector_to_row(1:3) row_to_vector("1 & 2 & content \\\\") row_to_vector("1 & 2 & content \\\\", deparse = FALSE)
vector_to_row(1:3) row_to_vector("1 & 2 & content \\\\") row_to_vector("1 & 2 & content \\\\", deparse = FALSE)