View source: R/ltx_wrap_numbers_math.R
ltx_wrap_numbers_math | R Documentation |
This function takes a LaTeX table as input and wraps all numbers, both
positive and negative, in LaTeX math mode ($number$
). The function ensures
that numbers are not already wrapped in math mode by checking for existing dollar signs.
ltx_wrap_numbers_math(latex_table, print_tbl = FALSE)
latex_table |
A character vector or list containing the LaTeX table input. |
print_tbl |
A logical value indicating whether to print the table to the console after modification. Defaults to |
The function supports both integers and decimal numbers, optionally
including commas as thousand separators. Additionally, it can handle scientific notation
(e.g., 1.23e4
) and superscripts (e.g., 10^{2}
).
The function uses a regular expression to identify and wrap numbers in LaTeX math mode. It:
Ensures that numbers are not already wrapped in dollar signs.
Supports numbers with commas as thousand separators, decimal points, and scientific notation.
Optionally wraps numbers followed by LaTeX superscripts in math mode as well.
Explanation of Regular Expression:
(?<!\\$)-?\\b(\\d{1,3}(,\\d{3})*(\\.\\d+)?(e-?\\d+)?)(\\^\\{[^\\}]+\\}\\S*)?(\\S*\\b)?(?![^$]*\\$)
(?<!\\$)
A negative lookbehind that ensures no dollar sign ($
) precedes the current position.
This prevents matching numbers already enclosed in LaTeX math mode.
-?
Matches an optional negative sign, allowing the pattern to capture negative numbers.
\\b
A word boundary to ensure the number is a standalone entity.
(\\d{1,3}(,\\d{3})*)
Matches numbers, potentially separated by commas as thousand separators.
(\\.\\d+)?
Optionally matches decimal numbers.
(e-?\\d+)?
Optionally matches numbers in scientific notation (e.g., 1.23e4
).
(\\^\\{[^\\}]+\\}\\S*)?
Optionally matches LaTeX superscripts.
(?![^$]*\\$)
A negative lookahead that ensures no dollar sign ($
) follows the matched number.
A modified LaTeX table where all numbers are wrapped in math mode. The result is returned invisibly.
## Not run:
# Example LaTeX input with numbers
latex_input <- c("Value: 1234", "Temperature: 98.6", "Negative: -567.89")
# Wrap all numbers in math mode
result <- ltx_wrap_numbers_math(latex_input)
# Output: "Value: $1234$", "Temperature: $98.6$", "Negative: $-567.89$"
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.