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.
Arguments
- items
A list of latex items.
- pattern
Pattern to use in
grepl()
.- ...
Additional parameters to pass to
grepl
.- all
Find all matching, or the first?
Value
find_pattern()
returns a LaTeX2range object
or (if all
is TRUE
) a list of them.
Details
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.
Examples
latex <- kableExtra::kbl(mtcars[1:2, 1:2], format = "latex", caption = "Sample table")
parsed <- parseLatex(latex)
parsed
#> \begin{table}
#>
#> \caption{Sample table}
#> \centering
#> \begin{tabular}[t]{l|r|r}
#> \hline
#> & mpg & cyl\\
#> \hline
#> Mazda RX4 & 21 & 6\\
#> \hline
#> Mazda RX4 Wag & 21 & 6\\
#> \hline
#> \end{tabular}
#> \end{table}
loc <- find_pattern(parsed, "RX4 Wag", fixed = TRUE)
loc
#> path=1,8 range=40:43
print(loc, source = parsed)
#> RX4 Wag